-5

I'm revising for an operating systems exam and currently trying to understand this processes question:

int main() 
{
    int v=0;
    
    if(fork()) 
    {
        v++;
        
        if(!fork())
        {
            fork();
            v--; 
        }
    } 
}

So the question asks to

  1. Draw a tree reflecting the parent-child hierarchy of processes created when the program above is run.

  2. How many separate copies of the integer variable v are created? Discuss what is the value of v at the end of the program for each of the processes in the tree.

The main thing I'm having an issue with is the

if(fork())

line in the code. From looking at other stack overflows I realised that

if(fork()) = if(fork() != 0)

but I'm still having difficulty understanding whether the first if statement creates a child process? Because thats the only way that the second if statement:

(!fork())

can be executed?

This is how far I've got with my current understanding of the question. screenshot of attempt enter image description here

Hopefully the diagram is legible! I'd be really grateful for any hints or pointers with this.

Community
  • 1
  • 1
  • 1
    Note: You can break your question down into two completely independent questions: (1) Does calling `fork()` create a child process? and (2) Does `if (some_function()) { ... }` call `some_function()`? – Solomon Slow Mar 16 '18 at 17:11
  • 1
    Possible duplicate of [How does fork() work?](https://stackoverflow.com/questions/15102328/how-does-fork-work) – Jean-François Fabre Mar 16 '18 at 19:20

2 Answers2

2

Notice that if (fork()) /*etc*/ is exactly the same as

pid_t newtmp = fork();
if (newtmp != 0) /*etc*/

where newtmp is a fresh (new) variable name not occurring in your program (you could use x1, x2 etc.... provided it has no occurrence in your program), and pid_t is some integral type (probably int).

Once you rewrote your code with explicit and unique names given to result of fork you'll understand it better.

BTW, the code is poor taste. When you use fork you need to handle three cases:

  • the fork failed (e.g. because your system has not enough memory, or because you exceeded some limits) and gives -1

  • the fork succeeded in the child so gives 0

  • the fork succeeded in the parent, so gives the pid of the child.

But when you code if (fork()) you are forgetting -or handling incorrectly- the first case (failure). It can rarely happen.

Read carefully (and several times) the fork(2) man page. Notice that fork is difficult to understand.

Regarding limits, be aware of setrlimit(2) (and the ulimit bash builtin).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

The answer is

if (fork ())

may or may determine whether a child process was created.

Go to the man page:

http://man7.org/linux/man-pages/man2/fork.2.html

We find that fork () returns three types of values:

  • -1 => fork() failed
  • 0 => return value in the child process
  • a positive value => success and the PID of the child process.

Thus the test

if (fork ()) 

which is the same as

if (fork () != 0)

may succeed whether or not a child process was created. A competently written question would have said

if (fork () > 0)

Assuming everything works correctly:

int main() 
{
    int v=0;

    if(fork())  // Creates a child process that does nothing.
    {
        v++;

       if(!fork()) // Creates a child process that creates a child process (that then does nothing but decrement v).
       {
           fork();
          v--; 
       }
   }  
}
user3344003
  • 20,574
  • 3
  • 26
  • 62