I want one parent and 4 childs and after creating them I print something like:
[PID] (parent) with child processes: [PID1] [PID2] [PID3] [PID4]
and the parent waits for all of them to finish.
Could I use this code ( How to use Fork() to create only 2 child processes? ) in a loop or something like that?
I managed to do this:
main()
{
int pid, state, loop1=4, loop2, i=1, j;
printf(" Parent before fork()\n");
if ( ( pid=fork() ) !=0)
{
ParentPID=pid;
wait( &state);
}
else
{
while(loop1!=0)
{
execl("child", 0);
a[i]=pid;
loop1--;
i++;
}
}
printf("Parent after fork()\n");
for ( j=1; j<i; ++j )
{
printf ("PARENT_PID:"%d" CHILD_PID[" %d "]= "%d,ParentPID, j, a[i]);
}
//printf("\tId process child=%d; finished with %d=%x\n",pid,state,state);
}
main()
{
int pid;
printf("Child: the execution starts \n");
pid=getpid();
printf("Child: %d execution finished\n", pid);
exit( pid);
}