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
The data-linking feature that allows Internet users to skip directly from a highlighted word to a related file in another comput
tamaranim1 [39]

Answer:

Hypertext

Explanation:

According to my experience on information technology, I can say that based on the information provided within the question the term being mentioned is called Hypertext. Like described in the question this term refers to a software system which shows as a highlighted piece of text that allows a user to immediately be taken to another system with information associated with the highlighted text that was clicked.

If you have any more questions feel free to ask away at Brainly.

5 0
3 years ago
Read 2 more answers
What should every Software Engineer know about Software Architecture?
Alenkinab [10]
<span>!UML (all of them)
2.Flowchart (more for understanding a real world process of some kind; like a business process)
3.Data model including Bachman (if you don't need to at least understand your data, how it is stored versus a model, i.e., Bachman then you are doing it wrong and your schema could be simplistic)
This is 3 different examples</span>
6 0
3 years ago
Can I have some help?
Ad libitum [116K]
I believe its A as the image provided says plain message
6 0
1 year ago
: Describe the type of gameplay seen during early video games (ex. Spacewar!, Pong).
Alexeev081 [22]

Answer:

Computer scientists began building rudimentary games and simulations on mainframe computers in the 1950s and 1960s, with MIT's Spacewar! in 1962 being one of the first such games to be played with a video display. The first consumer-ready video game hardware arrived in the early 1970s, with the Magnavox Odyssey, the first home video game system, and the first arcade video games from Atari, Computer Space and Pong, the latter of which was later transformed into a home console version. Pong's success in arcades and at home prompted numerous firms to create clones of the game, resulting in a market contraction in 1978 owing to oversaturation and a lack of innovation.

8 0
2 years ago
PLS HELP) Early word processors ran on devices that looked like digital _______?
stealth61 [152]
I looked it up it just says true
8 0
3 years ago
Read 2 more answers
Other questions:
  • "what is the name of the ipsec configuration file"
    9·1 answer
  • Computer hardware had been designed to run a single operating system and a single app, leaving computers vastly underutilized. o
    15·1 answer
  • Jenny is working on a laptop computer and has notices that the computer is not running very fast. She looks and realizes that th
    6·2 answers
  • Which is an advantage that electronic scheduling tools have over paper calendars?
    13·2 answers
  • This assignment requires you to write a program to analyze a web page HTML file. Your program will read one character at a time
    13·1 answer
  • What operating system is an open source program
    15·1 answer
  • Murray is a database technician. He has created a single enterprise database system. How is this better than multiple databases
    15·1 answer
  • A(n) ____ investigation tracks all elements of an attack, including how the attack began, what intermediate devices were used du
    5·1 answer
  • What is the positional weigh of the digit 7 in the octal number 7642 ?​
    15·1 answer
  • What is an aspect ratio?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!