4

If we look at the example

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

void main(){
  int pi_d ;
  int pid ;
  pi_d = fork();
  if(pi_d == 0){
    printf("Child Process B:\npid :%d\nppid:%d\n",getpid(),getppid());
  }
  if(pi_d > 0){
    pid = fork();
    if(pid > 0){
      printf("\nParent Process:\npid:%d\nppid :%d\n",getpid(),getppid());
    }
    else if(pid == 0){
      printf("Child Process A:\npid :%d\nppid:%d\n",getpid(),getppid());
    }
  }
}

For me, this looks like it would create processes indefinitely because, when we fork a process, a copy of the parent is made. So the program code is cloned.

This means that every new process runs the same code; thus, it calls pi_d = fork(), and so on.

What I'm missing here?

1
  • The forked process continues on from the statement after the fork() call, not from the beginning.
    – user207421
    Commented Mar 14, 2022 at 3:08

1 Answer 1

7

Quoting from POSIX fork definition (bold emphasis mine):

RETURN VALUE

Upon successful completion, fork() shall return 0 to the child process and shall return the process ID of the child process to the parent process. Both processes shall continue to execute from the fork() function. Otherwise, -1 shall be returned to the parent process, no child process shall be created, and errno shall be set to indicate the error.

OP wrote:

which means, for every new process, it runs the same code

Upon successful completion of fork() and return from it the parent and the child resume right after fork(): none will execute again the first fork(), then later none will execute again the 1st or 2nd fork() because there is no loop in this code to allow this to happen.

Assuming no error happens (they aren't checked):

  • parent forks
    • If it's the child display Child Process B.
    • else, if it's the parent, fork again
      • If it's (again) the parent, display Parent Process
      • If it's parent's second child, display Child Process A

As there's no order guaranteed between which of child or parent will beat the other in exact execution sequence, the 3 outputs can happen in any order or mingled (but on a given specific OS, one display order should happen more often than others, and Child Process B having a head start would probably be displayed first).

2
  • 6
    I think a key misunderstanding of the OP is that they seem to think fork acts like exec (i.e. starts a fresh process) with the current executable, whereas it actually "copies" the current state of the program (stack, memory, file handles) with the only difference being the one stated in this answer: one process gets a "0" returned from the current call, and the other gets a (positive) PID returned.
    – Dave
    Commented Mar 14, 2022 at 1:10
  • 1
    One of the better ways of explaining it that I've heard is that it's a function that returns twice: once in the parent, once in the child. (The same way of thinking can help to understand setjmp: the 2nd and later time it returns, it's from a longjmp to that saved context.) Commented Mar 14, 2022 at 3:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .