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
What three files do you need to perform a mail merge?<br><br> HELP ASAP PLEASE!!
Burka [1]

The 3 files you need to have for a successful mail merge are:

  • An Excel spreadsheet works
  • Outlook Contact List.
  • Apple Contacts List or Text file, etc.

<h3>What is Mail Merge?</h3>

This is known to be the act of carrying out  a Mail Merge and it is one where a person will need to use a Word document and a recipient list, that is an Excel workbook.

Files needed are:

  • Text file,
  • address files, etc.

The 3 files you need to have for a successful mail merge are:

An Excel spreadsheet worksOutlook Contact List.

Apple Contacts List or Text file, etc.

Learn more about mail merge from

brainly.com/question/20904639

#SPJ1

4 0
1 year ago
Should spreadsheets be used to keep an address list for holiday cards?<br> YES<br> ONO<br> Hurry pls
kodGreya [7K]

Answer:

No

because spreadsheet is used for calculation not for writing address

5 0
2 years ago
What are the oop concept of java
mash [69]

Answer: OOP concepts in Java are the main ideas behind Java’s Object Oriented Programming. They are abstraction, encapsulation, inheritance, and polymorphism. Grasping them is key to understanding how Java works. Basically, Java OOP concepts let us create working methods and variables, then re-use all or part of them without compromising security.

HOPE THIS HELPED IS NOT SORRY.

4 0
2 years ago
Read 2 more answers
ARP only permits address resolution to occur on a single network.could ARP send a request to a remote server in an IP datagram?w
romanna [79]

Answer:

The answer to this question is Yes it is possible

Explanation:

It can be done In the case where a special server is required in each network that would forward the request to the  remote ARP(Address Resolution Protocol) server and will receive the response from the server and send it to the requesting host.So we conclude that we can do that hence the answer is Yes.

5 0
3 years ago
You are studying at the library and someone at your table has some great study material. both of you have an ipad with you. you
myrzilka [38]
C.apple domain is to use the personal sharing files from one computer to another computer
3 0
3 years ago
Other questions:
  • What branch of science helps avoid or minimize stress-related injuries at workplace?
    6·1 answer
  • What natural boundary separated british territory from louisiana?
    7·1 answer
  • The state licensing bureau recently conducted a survey at Community Hospital. During this review, the surveyors determined that
    13·1 answer
  • What is something that can be done to help stop teen teen crashes
    8·1 answer
  • A ________ database state is one in which all data integrity constraints are satisfied
    15·2 answers
  • Which fund classification would a focus on only stocks of technology companies match?
    10·1 answer
  • What do people do when they navigate using GPS
    6·2 answers
  • Limitations of systems analysis and design​
    13·1 answer
  • To make the most informed purchasing decision, you should:
    11·1 answer
  • How does your ability to correctly count change affect the impression the customer has of you?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!