05-19-07 12:17 AM
sail0r@creepjoint.net wrote:
> Rainer Weikusat wrote:
> [code snipped]
> Why do you say that? I copied and pasted that code and, after adding the
> necessary header of course!, it created exactly five processes.
Of course, the OP *does* have the printf's all being done by the parent
for some reason...
Anyway, here is some code that does the trick I believe
-------------------------
#define NUMBER_PROCESS 5
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
int main(int argc, char *argv[])
{
pid_t pid = getpid();
int i=0;
pid=fork();
while(i<NUMBER_PROCESS && pid>0){
printf("PID:%d i:%d\n",pid,i);
pid=fork();
i++;
}
}
-------------
[user@host~]$ ./a.out
PID:23950 i:0
PID:23951 i:1
PID:23952 i:2
PID:23953 i:3
PID:23954 i:4
ok, I am not sure if this is *exactly* the OPs problem...
The parent isn't spawning all the child processes and I printed 0
through 4 and not 1 through 5 ;)
[ Post a follow-up to this message ]
|