Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please answer this question correctly and put the screenshot Write a program nam

ID: 3593048 • Letter: P

Question

Please answer this question correctly and put the screenshot

Write a program named <prseq.c>, which should take one command-line argument called maxnum. prseq should fork two child processes. Together, these three processes will print the sequence of integers from 0 up to maxnum (inclusive), one integer on each line.

The parent process will print out 0, 3, 6, 9, etc.

One of the two child processes will print out 1, 4, 7, etc.

The other child process will print out 2, 5, 8, etc.

Obviously, unless we control the execution order of these three processes carefully, different outcomes will be observed (for example, when maxnum=4, 1,4,2,0,3 and 2,1,4,0,3 are two of the possible outcomes). You will be exploring ways to manufacture such different outcomes.

Use any kind of techniques (sleep(), excessive computation, etc.) to slow down one or two of the three processes to come up with as many different outputs as possible.

All of your executions should be fed with a maxnum=4.

Do not use semaphores or any standard solution that we have discussed or will discuss in Chapter 6.

Take snapshots

How many different outcomes have you generated?

Explanation / Answer

#include <stdio.h>


int main(int argc,char **argv[])
{
int pid1,pid2,parent,maxnum,i;
if(argc < 2)
{
printf("Enter the maxnum as command line arg and then execute ");
return -1;
}
//convert command line arg to integer
maxnum = atoi(argv[1]);
parent = getpid();
pid1 = fork();
if(pid1 == 0) //child process
{
printf("Child1 executing with pid = %d ",getpid());
//sleep till parent finishes
//sleep(maxnum);
//now print the next sequence

for(i = 1; i <= maxnum;i+=3)
{
printf("%d ",i);
}
printf(" ");
//now fork one more child
pid2= fork();
if(pid2 == 0)
{
//now print next sequence
printf("Child2 executing with pid = %d ",getpid());
for(i = 2; i <= maxnum;i+=3)
{
printf("%d ",i);
}
printf(" ");
  
}
else
{
sleep(10);
//sleep(maxnum/2);
}
}
else
{
printf("Parent process with pid = %d ",parent);
//now print the global_var with increament of 3
for(i = 0; i <= maxnum;i+=3)
{
printf("%d ",i);
}
printf(" ");
sleep(10); //wait for child to finish
  
}
return 0;
}

------------------------------------------------------

//one output squence with command line arg as 4(maxnum)

Parent process with pid = 3                                                                                                 

0 3                                                                                                                         

Child1 executing with pid = 4                                                                                               

1 4                                                                                                                         

Child2 executing with pid = 5                                                                                               

2   

---------------------

//run with maxnum 10 as command line arg

Parent process with pid = 3                                                                                                 

0 3 6 9                                                                                                                     

Child1 executing with pid = 4                                                                                               

1 4 7 10                                                                                                                    

Child2 executing with pid = 5                                                                                               

2 5 8

---------------------------------------------------

//run as maxnm 100 as command line arg

Parent process with pid = 3                                                                                                 

0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99                           

Child1 executing with pid = 4                                                                                               

1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100                         

Child2 executing with pid = 5                                                                                               

2 5 8 11 14 17 20 23 26 29 32 35 38 41 44 47 50 53 56 59 62 65 68 71 74 77 80 83 86 89 92 95 98