Eric Tang
Eric Tang

Reputation: 207

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

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.

Upvotes: 12

Views: 35796

Answers (4)

glglgl
glglgl

Reputation: 91017

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.

Upvotes: 2

Per Johansson
Per Johansson

Reputation: 6877

With that expected output, most likely your homework should first fork off all processes and then call wait.

Simply skip the wait call in the loop and do a separate one below that should loop until wait returns -1 and errno set to ECHILD. Note that the output order of the children will be random, or at least not completely in order, so not necessarily 2 1 3 4 5.

This is only a guess though, you should provide more info if you want a more specific answer.

Upvotes: 1

ClemPi
ClemPi

Reputation: 326

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.

Upvotes: 5

Eregrith
Eregrith

Reputation: 4366

The system takes free PIDs to assign to processes. You can fork the process ID'd 4000 and have a child ID'd 3900. You homework paper should not even put numbers because the first process ID is never the same anyway.

Upvotes: 1

Related Questions