Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fork() n child processes correctly in C?

Tags:

c

fork

That is my code.

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

int main ( int argc, char *argv[] )
{
    int i, pid;

for(i = 0; i < atoi(argv[1]); i++) {
    pid = fork();
    if(pid < 0) {
        printf("Error");
        exit(1);
    } else if (pid == 0) {
        printf("Child (%d): %d\n", i + 1, getpid());
        exit(0); 
    } else  {
        wait(NULL);
    }
}

}

The output is like that.

Child (1): 5676
Child (2): 4624
Child (3): 4800
Child (4): 5596
Child (5): 5580

However that is not the expect output in the my homework. It should be like that. What's wrong with code? Can someone help me?

Child (2): 4625
Child (1): 4624
Child (3): 4626
Child (4): 4627
Child (5): 4628

Thank You for your help. Now I will try it out.

P.S. Sorry my English is bad. I hope you can understand what I said.

like image 397
Eric Tang Avatar asked Feb 05 '12 10:02

Eric Tang


People also ask

Can you fork in child process C?

fork() in CFork system call is used for creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call (parent process). After a new child process is created, both processes will execute the next instruction following the fork() system call.

What does the fork () return to the child process?

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

How do you make a fork with two child processes?

If you simply call fork() twice in a row, then both the parent and the first child will execute the second fork() , and so you'll get two more processes created.

How do I make 5 processes with a fork?

When you create a fork, both the parent and the newly created child process continue from the point of forking. So, if you have a loop running three times with fork, the parent creates three children, the first child creates two children, and so on. So, your example will result in more than five processes.


2 Answers

The reason why you get a unordered output is that you cannot exactly forecast which child becomes active when. So it might happen that execution of your 1st child is delayed until your 2nd one is fork()ed and started.

Your childs normally get sequential PIDs, although this is OS dependent.

Both issues should not be a problem with your scheduled task - neither the absolute PIDs are really of importance (as said, every OS can do its own stuff, assigning PIDs sequentially or at random), nor the order in which the childs do their stuff: each part of the childs can have different execution times, resulting in unordered output. This counts as long as the data are transferred correctly - which is the case if the parent generates the sequence and then forks. In this case, the child process's memory layout is the same as the parent's at the time of fork. So the parent can modify its "data transfer array" without affecting already running children.

For reducing confusion, you might remove the outputting of PIDs in every line. Maybe they can be output at the respective start of the child process, but after that, it should be enough to say e.g. Child 3: straight length 6 <S6,H5,C4,S3,H2,SA> without repeating the PID.

like image 113
glglgl Avatar answered Sep 29 '22 06:09

glglgl


Your code work perfectly on my computer. It can be os dependant.

however you should check if argc is not equal to 1 to avoid segmentation fault if no arguments are given to your program.

like image 34
ClemPi Avatar answered Sep 29 '22 06:09

ClemPi