0

How do i create a number of child processes given from command line ?

Something like this , where n is given from command line :

for (i = 0; i < n; i++) {
             pids[i] = fork();
}
Angelo
  • 65
  • 1
  • 1
  • 10
  • 1
    C does not know about processes. `fork` is not standardized in C99 or C11, but in POSIX. You probably are missing a Linux or a POSIX tag – Basile Starynkevitch Dec 14 '14 at 17:33
  • 2
    Please have a look at the manpage of the functions you use (http://linux.die.net/man/3/fork). Especially the sections _Description_ and _Return Value_ contain valuable information oftentimes. Especially when you don't _exactly_ know what the function does. – moooeeeep Dec 14 '14 at 17:36
  • The for loop needs to be written so ONLY the parent performs a fork() otherwise, the child is executing that same loop, so a (more or less) infinite number of children and childrens children and childrens childrens children ... processses are generated. Lookup the return code for fork() so as to avoid the children also fork()ing children of their own. – user3629249 Dec 15 '14 at 04:34

1 Answers1

6

No, this will not work because then the child processes will create more children and this will not be what you wanted. For a better idea of how that happens, go take a look at fork() branches more than expected?. So you have to break out of the loop if the current process is a child like so:

for (i = 0; i < n; i++) {
    if (!(pid[i] = fork()))
        break;
}

In order to see this in action, lets look at minimally complete example

file.c:

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

int main(int argc, char *argv[]) {
    int i, n = atoi(argv[1]);
    pid_t *pid = calloc(n, sizeof *pid);
    for (i = 0; i < n; i++)
        if (!(pid[i] = fork()))
            break;

    puts("hello world");

    return 0;
}

Then compile and run it

$ gcc -o file file.c
$ ./file 3
hello world
hello world
hello world
hello world

Note that there are 4 messages because there 3 children plus the parent process.

Community
  • 1
  • 1
randomusername
  • 7,927
  • 23
  • 50
  • The question was , how do i execute this code from command line , giving the 'n' – Angelo Dec 14 '14 at 17:12
  • @Angelo Ah, so your interested mainly in how to use a terminal to run this. I was confused by your tags. In that case I'll edit in an example of how to do that. – randomusername Dec 14 '14 at 17:14
  • Thanks. And also ,why would it create more childs with my code ? does it create n childs ? I don't understand . – Angelo Dec 14 '14 at 17:20
  • @Angelo See my edit for a link to another question explaining why your method won't work. – randomusername Dec 14 '14 at 17:31
  • 1
    Thank you . One last question ... what does this mean ?if (!(pid[i] = fork())) i really can't figure it out . – Angelo Dec 14 '14 at 17:39
  • 1
    @Angelo It executes the call to `fork`, stores its return value in `pid[i]`, and then tests if that same return value (which is now stored in `pid[i]`) is 0. If it is zero, that means that the current process is the parent process and it stops making children. If it is not zero, then either `fork` failed or the current process is the child process, so we make another process. – randomusername Dec 14 '14 at 17:58