1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ipatiy [6.2K]
4 years ago
5

Write a program that calls fork(). Before calling fork(), have the main process access a variable (e.g., x) and set its value to

some-thing (e.g., 100). What value is the variable in the child process? What happens to the variable when both the child and parent change the value of x? Write out the code to your program. Run the program and show your result. What value is the variable in the child process? What happens to the variable when both the child and parent change the value of x?

Computers and Technology
1 answer:
vesna_86 [32]4 years ago
7 0

Answer:

Here is the program:

#include <stdio.h>  //to use input output functions

#include <unistd.h> //provides access to the POSIX OS API

int main() { //start of main function

   int x;  //declare an int type variable x

   x = 100;  // sets the value of x to 100

   int r = fork();  //calls fork() method to create a new process. r is used to store the value returned by fork()

   if(r<0){  //if fork() returns -1 this means no child process is created

           fprintf(stderr, "Error");}     //prints the error message using standard error stream

  else if (r == 0)     {  //if fork returns 0 this means child process is created

       printf("This is child process\n");  //prints this message

       x = 150;  //changes value of x to 150

       printf("Value of x in child process: %d\n", x);     }  //prints the new value of x in child process

   else      {

       printf("This is parent process\n");  //if r>0

       x = 200;  //changes value of x to 200

       printf("Value of x in parent process: %d\n", x);     }  } //prints the new value of x in parent process

Explanation:

The program is well explained in the comments added to each line of the code. The answers to the rest of the questions are as follows:

What value is the variable in the child process?

The variable x is set to 150 in child process. So the printf() statement displays 150 on the screen.

What happens to the variable when both the child and parent change the value of x?

fork() creates a copy of parent process. The child and parent processes have their own private address space. Therefore none of the both processes cannot interfere in each other's memory. The both maintain their own copy of variables. So, when parent process prints the value 200 and child process prints the value 150.

In order to see which is the final value of x, lets write a statement outside all of the if else statements:

  printf("Final value of x: %d\n", x);

This line is displayed after the value of parent class i.e. 200 is printed on the screen and it shows:

Final value of x: 200

Note this is the value of x in parent process

However the same line displayed after the value of child class i.e. 150 is printed on the screen and it shows:

Final value of x: 150

The screenshot of the program along with the output is attached.

You might be interested in
Use the __________ property to configure bold text using css
tatuchka [14]
Use the 'font-weight' property.

To put it in perspective, 'font-weight: 400' is the same boldness as normal text, whereas 'font-weight' 700 is the same boldness as a regular bold font.
5 0
4 years ago
What are some difficulties in synchronizing audio and video during telecine transfer? (Select all that apply.)
Oksi-84 [34.3K]
Telecine<span> is the process of </span>transferring<span> motion picture film into </span>video<span> and is performed in a color ... The most complex part of </span>telecine<span> is the </span>synchronization of the mechanical <span>film motion and the electronic video signal.</span><span> not noticed in the picture (but may be more noticeable </span>during<span> action speed. 

Telecine transfer can speed up the film, and Audio recordings are shorter than film. </span>
3 0
3 years ago
Please help. Will give brainliest
Sergeeva-Olga [200]
I don’t understand it sorry :(.
4 0
3 years ago
A box contains two types of colored pencils: red and blue. it also has a few pens. you can pick only one item at a time. if you
kipiarov [429]
Xor because in the XOR gate, if both inputs are the same, the output is zero but if they're different, the output is one
3 0
4 years ago
Read 2 more answers
Marsha receive an email saying that she won a free cruise to Alaska. What Marsha didn’t realize is that the email was fake and c
Gre4nikov [31]

Answer:

I think the answer is B

Explanation:She sent it to her friends not knowing it would give there computers a virus too.

4 0
3 years ago
Read 2 more answers
Other questions:
  • How are information systems used at the industry level to achieve strategic advantage? By enforcing standards that reduce the di
    8·1 answer
  • Which statement about information published on the internet is true?
    9·2 answers
  • When operating a computer, a user interacts with
    14·2 answers
  • A(n ________ cpu has two processing paths, allowing it to process more than one instruction at a time.
    15·2 answers
  • Is there anyone willing to sign into my google classroom account and help with my work :( i had surgery on monday and have about
    14·2 answers
  • Write a short Python function, is_multiple(n, m), that takes two integer values and returns True is n is a multiple of m, that i
    9·1 answer
  • Identify a data type applied in an input element to create slider controls.
    13·1 answer
  • Write a java program that accepts the ingredients for a recipe in cups and converts to ounces
    10·1 answer
  • CPT (Current Procedural Terminology) codes consist of 3-4 numbers representing a unique service. True False
    14·1 answer
  • In which career field, would the computing technology industry Associates CompTIAA+ certification be useful?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!