Thanks for at least one person picking up on the forking reference though. nice to see someone with a sense of humour, and possessed of C.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main( int argc , char** argv ){
int count = 0 ;
int status = -1 ; /* Status for wait() */
pid_t pid ; /* pid of child process */
pid_t my_pid = getpid(); /* get current pid */
if( sscanf( argv[1] , "%d" , &count ) == 0){
fprintf( stderr , "Error reading int from parameter\n");
return EXIT_FAILURE ;
}
if( count > 10 || count < 0 ){
fprintf( stderr , "Error, number not in range 0-9. \n");
return EXIT_FAILURE ;
}
/* Create 'count' number of processes */
while( count-- > 0 ){
pid = fork();
if( pid == 0 ){
/* In child, so report, and break so not to fork() */
my_pid = getpid();
printf("CHILD: %d\n", my_pid);
break ;
}
}
/* Keep looping untill evey child process had terminated */
while (pid != -1 ){
pid = wait(&status);
if( pid != -1 )
printf("PARENT: I am %d. The child is %d\n", my_pid, pid);
}
return EXIT_SUCCESS ;
}