retagged by
1,124 views
1 votes
1 votes

Given the following piece of code :

Main()

{

Int child = fork();

Int c = 5;

if (child == 5)

{

C + = 5;

}

Else

{

Child = fork();

C + = 5;

}

}

What are the different values of the variable c?

First of all checking ‘child == 5’ seems to be wrong as the value returned by fork() for parent process can be any non-zero value. Also I guess they gave a pseudocode as some of the syntaxes are wrong.

Also I am getting the answer as 10,10,10. Given answer is 20,10,15.

Can someone please check?

retagged by

3 Answers

Best answer
2 votes
2 votes

Main(){

Int child = fork();

Int c = 5;

if (child == 5){ // Point:1

      C + = 5;

}

Else{

        Child = fork();

C + = 5;

}}

Answer will be 10,10,10,10  as four process are created.

Note : if at Point:1 we had “ if (child == 0) “ then only 3 process would have been created but answer would still be 10,10,10.

 

https://www.cs.utexas.edu/~lorenzo/corsi/cs372/06F/hw/4sol.html refer problem 4

edited by
1 votes
1 votes

See approach the question like this

main() {
    int child = fork(); 
    /* → At this point a new process having exactly the same properties will be forked
    In simple words with the exact copy of this code a new process is generated
    fork() will return 
           0 to the child process 
           the ProcessID of the child to the parent*/
    
    /*One good point to note here is that child will execute code from the next line
    It will not execute already executed code*/

    int c = 5; /* this variable will have a different copy at parent and the child*/
    
    /*From this part it gets interesting*/
    // for the child this if is always false as 0 is never equal to 5
    /* for the parent it may or maynot be false if child's pid is 5 then it
    evaluates to true, if if child's pid is not 5 then it evaluates to false*/
    
    if (child == 5)  
    { 
        C + = 5;
    }
    else {
        Child = fork();
        C + = 5;
    }
}

Now if both parent(Lets call it as P1) and the child(Lets call it as P2) goes to else code

Both will fork two more processes which will execute C+=5

Final value of C in this case in all 4 processes = 5+5=10

 

Now if both parent(Lets call it as P1) goes to if and the child(Lets call it as P2) goes to else code

Child will fork one more processes which will execute C+=5

Final value of C in this case in all 3 processes = 5+5=10

so C in any case will have only 1 value 5+5=10

0 votes
0 votes
From the  line int child equals fork(), the child may have 2 possible set of values {0 and 5} or {0 and greater than zero}.

Therefore when 1st tuple will be in action, the parent will get into the "if" clause and child in the else clause. Hence total 3 time the c variable will be incremented.

 

But when 2nd case in action, therefore both three parent and child process will get into else clause and total four times c will be incremented.

Answer 20 and 25.

Related questions

0 votes
0 votes
1 answer
2
Erwin Smith asked Apr 11, 2023
782 views
void main() { int n = 1; if(fork()==0) { n = n<<1; printf(“%d, “, n); n = n <<1; } if(fork()==0) n=n+700; printf(“%d, “,n); }Which of the following output is not ...
1 votes
1 votes
2 answers
3
♥_Less asked Jan 12, 2018
1,803 views
What is the number of child process created ?Answer given was 63, BUT i am getting 9 !