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 having an issue with trying to make my code to have a required output...
I am having an issue with trying to make my code to have a required output ////////////Input////////// With computer science, you can work in any industry. 0 //////////Output////////// Required Output Enter some text to encrypt\n Enter a key\n Error: Key is divisible by 26. That's a bad key!\n Useless key: 0\n ///////////Cipher.java///////// ------------ My code. public class Cipher { private String plainText; private int key; public Cipher(String text, int key) throws EmptyPlainText, UselessKeyException { if (text == null || text.length()...
Using Java. The following is a constructor I need to implement but I am not sure...
Using Java. The following is a constructor I need to implement but I am not sure how. It also must pass the Junit test. Please give reasoning while answering. public class LZWDictionary { // FIELDS // map (for ease of checking existence of entries) // list (ease of recall of an entry) LinkedHashMap<String, Integer> map; List<String> list; /** * Initializes the LZWDictionary to have an initial set of entries taken from * the set of unique characters in a provided...
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 using NetBeans IDE Java to code and I am seeking comment to the code...
I am using NetBeans IDE Java to code and I am seeking comment to the code as well (for better understanding). Magic squares. An n x n matrix that is filled with the numbers 1, 2, 3, …, n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. Write a program that reads in 16 values from the keyboard, displays them in a 4...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT