Austin
Austin

Reputation: 3080

Understanding how fork() and wait() work together

This isn't in code review because I do not understand the full concept of the code to start with. If it should still be moved just let me know.

I have some code and I would like to explain my thoughts on it, and I am hoping someone can tell me where I am going wrong or getting confused at because I am still not fully sure what is occurring.

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
   {
   pid_t pid, pid1;
   pid = fork();

   if (pid < 0) {
      fprintf (stderr, “fork() failed\n”);
      return(1);
   }
   else if (pid == 0) {
      pid1 = getpid();
      printf (“pid = %d\n”, pid); // A
      printf (“pid1 = %d\n”, pid1); // B
   }
   else {
      pid1 = getpid();
      printf (“pid = %d\n”, pid); // C
      printf (“pid1 = %d\n”, pid1); // D
      wait (NULL);
   }
   return 0;
}

From what I understand, we have two process ID's, the parent (pid) and the child (pid1). Once I call pid = fork(), I believe that the child is initiated and is given the id of 0, while the parent get's the ID of the child, let say 1337. So pid = 1337 and pid1 = 0.

So we skip the first if as no error has occurred (pid < 0), we skip the second if as well since pid does not equal 0, and then we enter the final if where C will print 1337 and D will pint out 0.

This then waits until the child is done, I think.

After that, I assume that the copied process (fork()) will then run the else if (pid == 0) but I am confused on why, because the pid is still 1337..

TLDR: If the third if should be executing first, then how do I get to the second if, but if this logic is all wrong please correct me.

Upvotes: 1

Views: 1711

Answers (3)

chepner
chepner

Reputation: 532238

pid is 0 in the child and the process ID of the child in the parent.

pid1 is set to the process ID of the current process. The value in the child's copy of pid1 is identical to the value of the parent's copy of pid.

Upvotes: 1

Haris
Haris

Reputation: 12272

Everything you said is not correct

after the fork() call is executed, the child and the parent process runs in parallel, both executing the code of the program that is there after fork(). The only difference would be the pid. The child will run the same program with the pid = 0, and the parent will run the same program with the pid = (pid of child). They seperate out, both having a copy of all the variables of the program, but a different copy of the pid variable.

Upvotes: 4

John Hascall
John Hascall

Reputation: 9416

A fork creates makes a (near-perfect) copy of the running process. One difference, as you surmised is the return value of fork() itself. So, assuming the fork works you have two processes executing the same code. One, the child, takes the if (pid == 0) ... path, while the parent takes the else... path. You have no information about the order in which the two processes do their work. Maybe the child goes first, maybe the parent, maybe half way through they take turns, maybe you have two processors and they run together...

Imagine you have this program written on a piece of paper and you are following along with your finger, sliding it down the page. When you get to the fork, take the paper to a copier, make a copy, put both pieces of paper on the table, put a your index finger from each hand on one of the pieces, move both your fingers, each sliding down their own sheet of paper.

Upvotes: 5

Related Questions