• edited by
5,432 views
2 2 votes

I always face problem in fork questions  please explain in detail (with theory).  Thanks 

Assume that main () calls the following function test () exactly once Void test (void)
\{
If (fork ( ) == 0)
1
printf("Gate ");
exit (0);
\}
printf ("2016");
\}

What will be the possible output?

  1. Gate 2016
  2. 2016 Gate
  3. Both (a) and (b)
  4. None of these

3 Answers

Best answer
5 5 votes

Same was the case with me a long ago. But I suggest you to go through all fork questions in GO, you will surely get it. Coming to the question here when if(fork() == 0) statement executes, fork() executes and a child process is created. So, At this point of time $2$ processes are in the system (one is child process and other is the parent process). Return type of child process is $0$, but Process ID of child is returned to parent.(don't worry if you don't get this line)

int a = fork();

Here $a = 0$ for child process and $a \ne 0$ for parent process.

So, When child process execures, fork() == 0 is true and Gate  is printed. And for parent process fork() == 0 is false and  2016 is printed. And note that both of these processes will execute in some order, but that order is not known.

So, when child executes first and then parent Gate2016 will be printed and When Parent executes first and then child, 2016Gate will be printed. Hence both orders are possible and thus (C) is correct answer.

• selected by
0 0 votes

Some clarification first, when call fork  it creates exact copy of the current program which runs on CPU independent of parent.

Both parent and child after the execution continues the execution from the same point, which is checking the value returned by the fork call. The only difference between the parent and the child will be the returned value of fork. For parent it will be the pid of the child (and not 0 as said in the question) and for child it will be 0.

Hence in this case, the if condition will evaluate TRUE for child, it will go inside the the if print the line Gate and then exit, the parent will see a non zero value, will print the 2016 and then exit. One thing to notice is that order of printing can differ as both parent and child are executing independent of each other (parent can issue a join call on the child as it knows the pid of child, which basically means that it will wait for the child to finish its operation).

In thie case, the output can be GATE2016 or 2016GATE or some garbled version of this, like GA20TE16.

However, if the code was like this:

int main(){
    int pid = fork();
    if(pid == 0){
        printf("GATE");
        exit(0);
    }
    join(pid); // not sure about the syntax
    printf("2016");
}

Then the output will always be GATE2016.

Hope this helps.

Position:
Show:

Related questions

0 0 votes
1 answers 1 answer
1.1k
1.1k views
nikkey123 asked Nov 19, 2017
1,085 views
how to make concurrency diagram plz explain?
4 4 votes
2 2 answers
428
428 views
GO Classes asked Jul 15
428 views
Consider the following C program. Assume that every $\texttt{fork()}$ call succeeds.int main(void) { unsigned long int i; for (i = 0; i < 6; i++) if (fork() >= 0) continu...
2 2 votes
1 1 answer
288
288 views
GO Classes asked Jul 15
288 views
Assume that every $\texttt{fork()}$ call succeeds.int main() { int a = 0; int rc = fork(); a++; if (rc == 0) { rc = fork(); a++; } else { a++; } printf("a is %d\n", a); }...
3 3 votes
1 1 answer
283
283 views
GO Classes asked Jul 15
283 views
Assume that every $\texttt{fork()}$ call succeeds.int main() { int pid; pid = fork() && fork(); if (!pid) printf("A\n"); else printf("B\n"); return 0; }How many times are...