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]
3 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]3 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
Please help with attached file
Paul [167]
Where is the attached file?
8 0
3 years ago
Read 2 more answers
How did Bill Gates benefit from free enterprise? Need a paragraph please. Will give the branliest!!
olchik [2.2K]

Answer:

Bill Gates used his skills and brains to build a business. In conclusion, Bill Gates was very successful with his industry thanks to free enterprise. He, along with his employees and friends, created and almost perfected the software known as Microsoft. The free enterprise system provides the right to private enterprise, which allowed him to choose his own business and to run it without governmental influence. Because he could create his own business and design his own products, Gates was able to specialize in technology.

Explanation:

8 0
2 years ago
What must you do to enable the members of the rome backup group to perform backup operations on the local system?
steposvetlana [31]
Add Rome Backup group to a membership list of a group that already has access to perform backup operations on the local system. Thank you for posting your question here at brainly. I hope the answer will help you. Feel free to ask more questions here.
5 0
3 years ago
Window is a very popular operating system becouse of its _ environment .​
Inga [223]
Window is a vary popular operating system because of its runtime and compatible environment .
hope it help
4 0
2 years ago
Read 2 more answers
Kathleen has written this paragraph:
yarga [219]

Answer:

Positive media attention can transform communities in unexpected ways.

Explanation:

According to the given excerpt, it is narrated that Kathleen wrote about a town called Abbston that was recently overwhelmed by tourists as a result of the news article by a TV travel editor who wrote about the town.

Therefore, the best concluding sentence for the paragraph would be that positive media attention can transform communities in unexpected ways.

8 0
3 years ago
Read 2 more answers
Other questions:
  • wHAT ARE THE 5 LAYERS in the internet protocol stack andwhat are the principal responsibilities of eack of theselayers.?
    12·1 answer
  • Which PlayStation was the first to allow connection between it and computer network
    12·1 answer
  • Describe how communication strengthens relationship at work and, as a result, increases your productivity
    9·1 answer
  • Select the correct answer. Vlad wants to include his goals and target in his résumé. He also wants to add how he can be benefici
    12·1 answer
  • In relation to data science,advances in technology has made it more feasible to do what
    5·1 answer
  • HELPPPP KOKICHI IS OUT TO KILL ME
    11·1 answer
  • If Anime Characters were real , Who Would Your Anime Wife Would Be (Tell Who And Why)
    10·1 answer
  • According to Okun's law, if the unemployment rate goes from 7% to 4%, what
    15·1 answer
  • How can we search person in brainly for sending request of friend
    10·1 answer
  • What is the purpose of the GETPIVOTDATA function?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!