2

I'm practising with parallel programming by using fork(). I expect that parent id of every process is id of former process but in my output shows same id. Why? The second question is about termination. Sometimes the output shows all processes, sometimes just two or three, sometimes only one. Why? I know parent process should wait its children, but what if not as in my question. My confusion is when fork() is called, both processes are executed without knowledge of their orders, aren't they?, maybe the parent process terminates its own execution. But its child can go on running to rest of program or terminated or something else? (As it can be seen on the output not always termination, not always fully correct neither) I don't comprehend the output showing only one but sometimes two or three or not all. I hope I could explain my problems. Seemingly, I couldn't.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main (int argc, char *argv[]) {
   pid_t childpid = 0;
   int i;

   for (i = 0; i < 3; i++)
      if (childpid = fork())
         break;

   fprintf(stderr, "i:%d  process ID:%ld  parent ID:%ld  child ID:%ld\n",
           i, (long)getpid(), (long)getppid(), (long)childpid);
   return 0;
}

Output(s):

i:0  process ID:2783  parent ID:1954  child ID:2784
i:1  process ID:2784  parent ID:1  child ID:2785
i:2  process ID:2785  parent ID:1  child ID:2786
i:3  process ID:2786  parent ID:1  child ID:0

or
//how??
i:0  process ID:3016  parent ID:1954  child ID:3017  
i:1  process ID:3017  parent ID:1  child ID:3018
i:2  process ID:3018  parent ID:1  child ID:3019

or
//how??
i:0  process ID:4079  parent ID:1954  child ID:4080
i:1  process ID:4080  parent ID:1  child ID:4081

or
//how??
i:0  process ID:3038  parent ID:1954  child ID:3039

Expected output:

i:0  process ID:2783  parent ID:1954  child ID:2784
i:1  process ID:2784  parent ID:2783  child ID:2785
i:2  process ID:2785  parent ID:2784  child ID:2786
i:3  process ID:2786  parent ID:2785  child ID:0

enter image description here

  • You can get more information here: http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html – vtha Jun 23 '17 at 13:08
  • "I know parent process should wait its children." it doesn't automatically you have to call `wait()` or `waitpid()` – Ingo Leonhardt Jun 23 '17 at 13:38
  • You don't wait your child there are nothing to add. – Stargateur Jun 26 '17 at 09:22
  • I know it, if you read my questions you can answer in detail I hope. @Stargateur – Soner from The Ottoman Empire Jun 26 '17 at 09:35
  • 2
    by the way, I always have 4 lines output with your [mcve]. And I don't see how this is possible to have less... can you prove the you get this kind of output ? – Stargateur Jun 26 '17 at 09:53
  • You don't check for `childpid < 0` - this is the case if fork fails. Normally, it shouldn't, but if so, you don't detect it... – Aconcagua Jun 27 '17 at 09:17
  • ppid == 1: see tgregory's answer. Not all lines printed: stdin/stdout/stderr are shared between parent and child processes. I assume they are closed by the parent before the children can write to them, so you don't get the output all the time... – Aconcagua Jun 27 '17 at 09:36
  • In the cases where you're getting less output lines than you expect, are you sure you don't see those missing lines *after* the shell prints the next prompt? – dbush Jun 27 '17 at 12:36

4 Answers4

10

Your output is non deterministic since it depends on the OS scheduler. Your parents are exiting after fork faster than child starts hence they get 1 as a parent.

See this two answers for some additional details:

  1. https://stackoverflow.com/a/395883/8199273
  2. https://stackoverflow.com/a/23697850/8199273
tgregory
  • 554
  • 3
  • 9
1

1) For 2nd question, When the parent for a child process dies before it, the child process is known as Orphan process, in that case process init, becomes the parent for all such, that's why you get 1 as parent id.

2) For 2nd question, generally this should not happen, so please check your fork() output value, to check whether fork() fails or not.

anil
  • 158
  • 1
  • 12
0

At the line where fork executes, child process is created, child gets same process context as parent, in your code parent process terminates after printing and child process continues the loop and launches another child process(till i becomes 3), itself now being parent process executes print statement. I tried to explain it below:

     parent      child
     -----------------------------------------------
prints(i=0)     parent        child
     -----------------------------------------------
            prints(i=1)       parent           child
     -----------------------------------------------      
                              prints(i=2)      Loop Terminates(fork dont get executed)
                                               prints(i=3)
Pras
  • 4,047
  • 10
  • 20
0

The reason this is happening is the parent process's are exiting before the getppid() call in the child. This causes the child to be re-parented under the init process.

Depending on operating system, your results will vary, as there are different rules as to whether to schedule the parent or child after a fork first.

jschmerge
  • 320
  • 1
  • 4