in Operating System edited by
608 views
1 vote
1 vote

Given the following piece of code:

main(int argc, char ** argv)
{
    int child = fork();
    int c = 5;
    if(child == 0)
    {
        c += 5;
    }
    else
    {
        child = fork();
        c += 10;
        if(child)
            c += 5;
    }
}

How many different copies of the variable $\textsf{c}$ are there?

  1. $2$
  2. $3$
  3. $4$
  4. $1$
in Operating System edited by
608 views

1 Answer

3 votes
3 votes
The piece of code shown creates two processes. Therefore, we have a total of three processes, the parent, the first and second child. Each of these has its own private copy of the variable $\textsf{c}.$ For the parent, the variable $\textsf{c}$ be $20$ before the end of the program. For the first child (the one created in the first program statement), the variable $\textsf{c}$ will contain the value $10$ before the end of the program. For the second child (the one created in the else clause), the variable $\textsf{c}$ will contain the value $15$ before the end of the program.

1 comment

Flow of executionimage

0
0
Answer:

Related questions