Question: Change the code below so that the if-else are inside a loop to loop 5 times and has a wait() and exit() -------------------------------------------------------------------------------------------------------------- #include #include
Change the code below so that the if-else are inside a loop to loop 5 times and has a wait() and exit()
--------------------------------------------------------------------------------------------------------------
#include
int main(void) { int x,i; int status; int pid,pid1,pid2,pid3,pid4,pid5; //process id for events x=19530; pid=fork(); if (pid==0) { printf("x = %d ",x); exit(EXIT_SUCCESS); } else { x=x-5; wait(&status); }
pid1=fork(); if (pid1 == 0) { printf ("ITERATION 1 "); printf ("Parent : x = %d ",x); x=x/5; printf ("Child : x = %d ",x); exit(EXIT_SUCCESS); } else { x=x/5; x=x-5; wait(&status); } pid2=fork(); if (pid2 == 0) { printf ("ITERATION 2 "); printf ("Parent : x = %d ",x); x=x/5; printf ("Child : x = %d ",x); exit(EXIT_SUCCESS); } else { x=x/5; x=x-5; wait(&status); } pid3=fork(); if (pid3 == 0) { printf ("ITERATION 3 "); printf ("Parent : x = %d ",x); x=x/5; printf ("Child : x = %d ",x); exit(EXIT_SUCCESS); } else { x=x/5; x=x-5; wait(&status); } pid4=fork(); if (pid4 == 0) { printf ("ITERATION 4 "); printf ("Parent : x = %d ",x); x=x/5; printf ("Child : x = %d ",x); exit(EXIT_SUCCESS); } else { x=x/5; x=x-5; wait(&status); } pid5=fork(); if (pid5 == 0) { printf ("ITERATION 5 "); printf ("Parent : x = %d ",x); x=x/5; printf ("Child : x = %d ",x); exit(EXIT_SUCCESS); } else { x=x/5; x=x-5; wait(&status); }
printf("Finished with all the processes. "); exit(EXIT_SUCCESS); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
