Question

When using a fork() function in a program which is running in a Unix like operating system (i.e MINIX 3) does it create a number of separate processes which are handled independently and can therefore finish ahead of each other (which is actually what I want) or will it create a series of sequential processes which only finish in the order they were created.

Here is the code I'm using to fork()

    for(j = 0; j < num_fork_loops;) {
       if (fork() < 0) {
          printf("Fork has failed\n");
          exit(EXIT_FAILURE);
       }
       j++;
     }

Thanks for your time

Was it helpful?

Solution

fork() returns immediately, twice. All processes are normal, running processes, which are scheduled separately by the OS. Typically they will all run simultaneously.

OTHER TIPS

See the POSIX rationale for fork, particularly this text:

IEEE Std 1003.1-1988 neglected to require concurrent execution of the parent and child of fork(). A system that single-threads processes was clearly not intended and is considered an unacceptable "toy implementation" of this volume of POSIX.1-2008. The only objection anticipated to the phrase "executing independently" is testability, but this assertion should be testable. Such tests require that both the parent and child can block on a detectable action of the other, such as a write to a pipe or a signal. An interactive exchange of such actions should be possible for the system to conform to the intent of this volume of POSIX.1-2008.

All historical implementations of fork have provided concurrent execution, and this was always the intent. It's spelled out explicitly in modern versions of the standard, as

After fork(), both the parent and the child processes shall be capable of executing independently before either one terminates.

You should just have a look at the man page for example. When you fork() the created processes are identic, and you don't know in which order they are executed.

See the fork man page:

fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent, except for the following points:

The new process runs in parallel with the parent, both continuing execution upon return from the fork() call.

IMHO your code is correct:

 Upon successful completion, fork() returns a value of 0 to the child
 process and returns the process ID of the child process to the parent
 process.  Otherwise, a value of -1 is returned to the parent process, no
 child process is created, and the global variable errno is set to indi-
 cate the error.

But probably it doesn't do what you expect, it creates 2^num_fork_loops if all forks have success.If the fork fails it returns -1 and you are right to check for the return value, but in case of success it returns 0 for the child and the PID (always positive) for the father, so in this case also the child process will start to create other processes.
Correct the code by making the child process execute something else (unless you want 2^N processes):

for(j = 0; j < num_fork_loops;j++) {
   pid_t pid=fork();
   if (pid < 0) 
   {
      printf("Fork has failed\n");
      exit(EXIT_FAILURE);
   }
   else if(pid==0)
   {
       j=num_fork_loops; // avoid to continue the loop for the child
       child_routine();
   }
   else
   {
       // This is the father, in case of success of the fork
       j++;
   }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top