//------------------------------------------------------------------- // children.cpp // // This program forks a number of separate child-processes and // provides to each of them a 4-digit string of random decimal // numerals. Each child-process executes our 'showarg1' using // the string of random numerals as a command-line argument. // // compile using: $ make showarg1 // $ make children // execute using: $ children // // This is an initial 'prototype' for one possible solution to // our suggested in-class exercise (on lottery and gamblers). // // programmer: ALLAN CRUSE // written on: 13 OCT 2004 //------------------------------------------------------------------- #include // for printf(), perror() #include // for exit() #include // for fork() #include // for wait() #define N_TASKS 8 // number of separate child-processes int main( int argc, char **argv ) { for (int i = 0; i < N_TASKS; i++) { char mybet[ 5 ] = {0}; for (int j = 0; j < 4; j++) mybet[j] = (rand()%10)|'0'; int pid = fork(); if ( pid == 0 ) execl( "./showarg1", "showarg1", mybet, NULL ); } for (int i = 0; i < N_TASKS; i++) wait( NULL ); }