the code below in detail. What is going to be done? What is the order of Lines L1,L2,L3,L4 ouput on the screen? (echo is the command to write the parameter (ex: echo merhaba writes merhaba on screen) (Suppose that value_1 = 3, value_2=5, value_3=2) | int main(int argc, char *argv[] ){ | pid_t pid, w; | int status, tim; | srand((unsigned) getpid()); | if ( argc < 4 ) { L1| cerr << "Usage " << *argv << " value_1 value_2 value_3\n"; | return 1; | } | for (int i = 1; i < 4; ++i) | if ((pid = fork( )) == 0)
Trace the code below in detail. What is going to be done? What is the order of Lines L1,L2,L3,L4 ouput on the screen? (echo is the command to write the parameter (ex: echo merhaba writes merhaba on screen) (Suppose that value_1 = 3, value_2=5, value_3=2)
| int main(int argc, char *argv[] ){
| pid_t pid, w;
| int status, tim;
| srand((unsigned) getpid());
| if ( argc < 4 ) {
L1| cerr << "Usage " << *argv << " value_1 value_2 value_3\n";
| return 1;
| }
| for (int i = 1; i < 4; ++i)
| if ((pid = fork( )) == 0)
| {
| tim = int (rand() %5);
L2| execl("echo", "echo", tim, (char *) 0);
| alarm(tim);
| sleep(4);
L3| cout << “After ” << tim << “second return to child” << endl;
| }
| else
L4| cout << "Forked child " << pid << endl;
|
| while ((w=wait(&status)) && w != -1)
| cout << "Wait on PID: " << dec << w << " returns status of "
| << setw(4) << setfill('0') << hex
| << setiosflags(ios::uppercase) << status << endl;
| return 0;
| }
approaches (i.e. write the pseudo code for
Step by step
Solved in 2 steps