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
True or False? At any point in time, an open file has a current file pointer indicating the place where the next read or write o
mr_godi [17]

Answer:

True is the correct answer for the above question.

Explanation:

  • When any document file is opened then every point has some particular address. so there is a pointer which states that where the read operation and the write operation is going on.
  • When any person writes any program to read a file or write a file then there is a need for some variable that is pointed for the reading and the write operation.
  • The document which is used for the write data or read data is also designed and maintained by some software.
  • Hence we can say that there are needs of some variable that point the operation of the file and it is also stated from the above question. Hence the above question statement is a true statement.
5 0
3 years ago
Evaluate the arithmetic expression 234 + 567​
Anton [14]

Answer:

801

Explanation:

234 + 567 = 801

6 0
2 years ago
Help , i couldn’t find this answer
Aleksandr-060686 [28]

Answer:

Internet Explorer

Explanation:

4 0
3 years ago
A customer has several small branches and wants to deploy a Wi-Fi solution with local management using CAPWAP. Which deployment
mash [69]

Answer:

Mobility express

Explanation:

Mobility express is defined as the ability to use the access point or the AP as a controller. It related to the lightweight network which is without a controller box. The access point of the mobility express uses CAPWAP images.

The CAPWAP stands for Control And Provisioning of Wireless Access Points. It is standard protocol which allows a central wireless of LAN access controller to use and manage the wireless access points.

Thus a customer who has various small branches and wishes to deploy a Wi-fi solution with the local management using CAPWAP should use the Mobility express as the deployment model.

6 0
2 years ago
Write a program that asks the user for three names, then prints the names in reverse order. Sample Run: Please enter three names
Sauron [17]

Solution:

Since no language was specified, this will be written in Python.

n1, n2, n3 = input ("Enter three names: ").split()

print(n3)

print(n2)

print(n1)

Cheers.

4 0
3 years ago
Other questions:
  • What are some of the advantages of using the F measure (weighted harmonic mean) over using the Precision &amp; Recall when evalu
    15·1 answer
  • Access Control can be defined as putting controls (or countermeasures) in place to help achieve information security goals. Whic
    13·2 answers
  • The idea generating technique most often used in writing business letter is ?
    9·2 answers
  • str1=”Good” str2=”Evening” Does the concatenation of the above two strings give back a new string or does it concatenate on the
    14·1 answer
  • ¿por que la toria de lamarck sobre el alargamiento del cuello de las jirafas por el esfuerzo continuado no pasara a sus descendi
    10·1 answer
  • Which of the following is the definition of registration authority ( RA)?
    10·1 answer
  • Robyn needs to ensure that a command she frequently uses is added to the Quick Access toolbar. This command is not found in the
    9·1 answer
  • According to programming best practices, how should you handle code that needs to be repeated several times in a program?
    6·1 answer
  • Create a set of functions that compute the mean, median, and mode of a set of
    8·2 answers
  • 15. Question
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!