Question

In: Computer Science

The output of the following code is "11 0" But I am not sure why, can...

The output of the following code is "11 0" But I am not sure why, can someone please explain to me clearly and concisely why this is the case?

main(){
int n=1;
if(fork())
wait(&n);
else
n=n+10;
printf("%d\n", n);
exit(0);
}

Solutions

Expert Solution

main(){
int n=1;
if(fork())
wait(&n);
else
n=n+10;
printf("%d\n", n);
exit(0);
}

fork() is a system call to create a process. When the fork() method is called a duplicate copy of itself is created called as child process. In the above code when fork() method is called a duplicate is created and this child process starts executing the lines return after calling fork method.

fork() method returns a negative value if no child process is created. It returns a positive pid number to the parent process if a child process is created. And it returns 0 to the child process if child process is created.

So in short if a child process is created then 0 is returned to child process and pid number of child is returned to parent process.

Now the above code after fork() is executed the code runs two times(one by parent process and another by child process).

  • For child process the fork() method has returned 0 and hence it moves to else part and computes n = n+10 and n becomes 11.
  • For the parent process the fork() method has returned a pid number of child and hence it enters the if condition and wait(&n) is executed.

There is a difference between wait() and wait(&n).

  • wait() or wait(null) - This will only wait for any child to terminate.
  • wait(&n) or wait(&status) - This also will wait for any child to terminate but with additional information that is the status code of the termination which is the exit code. Since its exit(0) its printing 0 as n becomes 0 since n is used in wait(&n).

Summary of the output 11 and 0 :

  • 11 is printed by the child process which enters else condition and n = n+10 is executed. and n becomes 11. And in the next print statement n is executed as 11.
  • 0 is printed by the parent process as wait(&status) gets the status code from the exit(0) as 0 and again executes the print status as n which is now 0. If you change the exit(2) then the output will be 11 and 512(status code of exit(2)).
  • Else if wait(&n) is changed to different variable such a s, then the output would be 11 printed by child process and 1 as n would not be changed and would remain as 1, printed by parent process.

Please leave a comment if u need more explanation.


Related Solutions

Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
Python: I am not sure where to begin with this question, and I hope I can...
Python: I am not sure where to begin with this question, and I hope I can get an input on it. This is in regards to Downey's program, and we are asked to make two changes. Downey prints time as they do in the Army: 17:30:00 hours. We want to print that as 5:30 PM. Downey lets you define the time 25:00:00 - we want to turn over at 23:59:59 to 00:00:00. (I am asked to identify my changes with...
This is the code I have. My problem is my output includes ", 0" at the...
This is the code I have. My problem is my output includes ", 0" at the end and I want to exclude that. // File: main.cpp /*---------- BEGIN - DO NOT EDIT CODE ----------*/ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> using namespace std; using index_t = int; using num_count_t = int; using isConnected_t = bool; using sum_t = int; const int MAX_SIZE = 100; // Global variable to be used to count the recursive calls. int recursiveCount =...
I am stuck on this problem and I am not sure what the solution is. In...
I am stuck on this problem and I am not sure what the solution is. In C Write item.h and item.c. In item.h, typedef a struct (of type t_item) which contains the following information: t_item: char name[MAX_ITEM_NAME_STRING]; char description[MAX_ITEM_DESCRIPTION_STRING]; Make sure that MAX_ITEM_NAME_STRING and MAX_ITEM_DESCRIPTION_STRING are defined with suitable sizes in your item.h. Typical values are, 25 and 80, respectively. Add the following interface definition to item.h: int item_load_items(t_item items[], int max_items, char *filename); Returns the number of objects loaded...
I am working on this problem for the company AT & T and am not sure...
I am working on this problem for the company AT & T and am not sure how to start it. Draw a chart of the main inter-organizational linkage mechanisms (e.g., long -term contacts, strategic alliances, mergers) that your organization uses to manage its symbiotic resource interdependencies. Using resource dependence theory and transaction cost theory, discuss why the organization to manage its interdependencies in this way. Do you think the organization has selected the most appropriate linkage mechanisms? Why or why...
What is the output of the following code: x = 0 a = 1 b =...
What is the output of the following code: x = 0 a = 1 b = -3 if a > 0:    if b < 0:       x = x + 5    elif a > 5:       x = x + 4    else:       x = x + 3 else:     x = x + 2 print(x) 4 2 5 3 Consider the following code segment:    sum = 0.0    while True:       number = input(“Enter a...
I am calculating the expected cash flow for a project. However, I am not sure that...
I am calculating the expected cash flow for a project. However, I am not sure that I should use the net cash flow (after tax) or cash flows (before tax) to identify the payback period and the discounted payback period? Thank you
(Be sure to paste the R Console Output and code!!!) Using the following data and R,...
(Be sure to paste the R Console Output and code!!!) Using the following data and R, write a brief paragraph about whether the in-home treatment is equally effective as the out-of-home treatment for two separate groups. Here are the data. The outcome variable is level of anxiety after treatment on a scale from 1 to 10. In-Home Treatment Out-of-Home Treatment 3 7 4 6 1 7 1 8 1 7 3 6 3 5 6 6 5 4 1 2...
I just need 3 and 5. I am not sure what I am doing wrong. I...
I just need 3 and 5. I am not sure what I am doing wrong. I get different numbers every time. Superior Markets, Inc., operates three stores in a large metropolitan area. A segmented absorption costing income statement for the company for the last quarter is given below: Superior Markets, Inc. Income Statement For the Quarter Ended September 30 Total North Store South Store East Store Sales $ 4,800,000 $ 960,000 $ 1,920,000 $ 1,920,000 Cost of goods sold 2,640,000...
I am given this starter code and I am suppose to debug it and all of...
I am given this starter code and I am suppose to debug it and all of that and it will not work after I change ">" to "<=" and then i'm not sure after that what else I have to do. Could someone help me solve this? // Start with a penny // double it every day // how much do you have in a 30-day month? public class DebugSix1 {    public static void main(String args[])    {      ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT