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
Ray owns a gaming cafe where users can play video games on computers. These games involve rich graphics and need high processing
tankabanditka [31]

Answer:

thick

Explanation:

i took the test and got it right

6 0
2 years ago
When scanning the road, glance away from the road ahead for ___. A. At least one second B. No more than one second C. At least t
Marrrta [24]
A.At least one second
5 0
3 years ago
Is the adoption rate of communication technology increasing ?
Neko [114]
More people adopt the technology during any period, leading to an increasing rate of adoption. So, yes
3 0
3 years ago
Read 2 more answers
17. What is something an employer might look for in person when hiring? (3
inessss [21]
D. all of the above
6 0
3 years ago
Read 2 more answers
Your database was damaged due to hardware failure. What can you use to restore it?
vesna_86 [32]
The only way is if you have backed up everything! Computers are amazing but they don't know that you need to back up your info. You should back up everything regularly! 
Hope this helped and have a nice day!

8 0
3 years ago
Other questions:
  • Cyber security includes which of the following?
    7·1 answer
  • Casual or informal group meetings are common. Here youcasually chat over tea, meet after work, or get together for purelysocial
    5·1 answer
  • Will somebody help me???? ANYBODY?? PLZZZZZZZZZZZZ
    8·1 answer
  • You use the _____ sheet in the format cells dialog box to position data in a cell by centering it, for example.​
    8·1 answer
  • Which password is an ideal and secure password?
    6·2 answers
  • One of the first feature films to use 3D animation was called ________. Water World Futureworld Wayne’s World Cool Hand Luke
    7·2 answers
  • Given an integer n and an array a of length n, your task is to apply the following mutation to an: Array a mutates into a new ar
    5·1 answer
  • It is unlawful in the State of Florida for any person, ______________________, to be a passenger in the front seat of a motor ve
    7·1 answer
  • Write a program. in QBAsSIC
    13·1 answer
  • You are attempting to print a document from your workstation to the network printer in your office. The print job failed when yo
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!