Question
Do it in LINUX ONLY ) Implement a process (C program) to do the following: (a) initialize an integer x with 19530 (b) create a
Do it in LINUX ONLY
) Implement a process (C program) to do the following:
(a) initialize an integer x with 19530
(b) create a child process
(c) decrease the variable x by 5
(d) send the value of x value to the child process
(e) the child process will
i. divide x by 5
ii. pass the updated value of x back to the parent process
Code for above instruction is
#include
#include
#include
#include
int main(void)
{
int x,i;
int status;
int parent_id,parent_id1,parent_id2,parent_id3,parent_id4,parent_id5;
x=19530;
parent_id=fork();
if (parent_id==0)
{
printf("x = %d ",x);
exit(EXIT_SUCCESS);
}
else
{
x=x-5;
wait(&status);
}
parent_id1=fork();
if (parent_id1 == 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);
}
parent_id2=fork();
if (parent_id2 == 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);
}
parent_id3=fork();
if (parent_id3 == 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);
}
parent_id4=fork();
if (parent_id4 == 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);
}
parent_id5=fork();
if (parent_id5 == 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;
}
Redo ^above code, but this time only using the signal mechanism system call (don't use wait() or pipe(), you may still need sleep()). You can still write or read to disk.
Output should look like
Thank You :)
X-19530 ITERATION 1 Parent : x 19525 Child : x = 3905 TERATION 2 Parent : x = 3900 Child : x 780 TERATION 3 Parent : x 775 Child : x= 155 ITERATION 4 Parent x-150 Child : x = 30 ITERATION 5 Parent x-25 Child : x = 5 X-19530 ITERATION 1 Parent : x 19525 Child : x = 3905 TERATION 2 Parent : x = 3900 Child : x 780 TERATION 3 Parent : x 775 Child : x= 155 ITERATION 4 Parent x-150 Child : x = 30 ITERATION 5 Parent x-25 Child : x = 5Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started