Home > Net >  Do processes run until termination when using `fork()`
Do processes run until termination when using `fork()`

Time:10-14

Just had a question about using fork(). Let's say we decide to fork() 3 times, thus having a total of 8 processes.

int main() {
 fork();
 fork();
 fork();
 printf("First print statement.\n");
 printf("Second print statement.\n");
 return 0;
}

In this case, I know that we have 8 different processes, namely A, B, C, D, E, F, G, H. Now, I know they can run in any order and so A could run first, but then E could run, then B, and so on. My question is, without using wait(NULL), and just having the code like this, does a process HAVE to finish running before another one runs? So, does the output have to be

// Process A
First print statement.
Second print statement.

// Process E
First print statement.
Second print statement.

// Process B
First print statement.
Second print statement.

// etc...

Or can we have something like:

// Process A
First print statement.
Second print statement.

// Process B
First print statement.

// Process E
First print statement.

// Process D
First print statement.

// Process B
Second print statement.

// Process D
Second print statement.

// etc...

CodePudding user response:

The processes run concurrently.

wait() will wait for the next process or waitpid() for a particular process. It's a synchronization primitive and allows a parent process to obtain a status of the terminating child.

CodePudding user response:

No, it does not necessarily HAVE to finish running before another one takes the CPU. It depends on CPU scheduling.

CodePudding user response:

Now, I know they can run in any order and so A could run first, but then E could run, then B, and so on.

Couching it in terms of the processes running in any order is not wrong, exactly, but it seems to be giving you an incorrect impression. Immediately after a process successfully fork()s, the parent and child are both running, at the same time. What can vary is when the instructions in each process are scheduled for execution on a CPU, and thus, when any particular behavior of either process manifests relative to the behaviors of the other. Just like for any other concurrently running processes.

My question is, without using wait(NULL), and just having the code like this, does a process HAVE to finish running before another one runs?

No. Roughly speaking, all live processes are eligible to get CPU time, and the system tries to spread the available CPU time around to all of them as best it can. If you have multiple processing units available, as is common these days, two or more of the processes in your group might execute instructions at literally the same time. No matter how many processing units you have, any given process is usually limited to a certain amount of CPU time before it is preempted to allow other processes to run. wait()ing has little to do with any of that.

Output like your second example is possible.

  •  Tags:  
  • c
  • Related