Question

1. Write program codes for 3.21 a and shared memory program b for producer and consumer as shown in the following. You can st

n= integer is passed on the command line. Programming Problems 155 3.21 The Collatz conjecture concerns what happens when we

Source code to modify:

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

int main()

{

pid_t pid;

/*fork a child process*/

pid = fork();

if (pid<0){ /*error occured*/

fprintf(stderr,"Fork failed\n");

return 1;

}

else if (pid == 0) { /*child process */

printf("I am the child %d\n",pid);

execlp("/bin/ls","ls",NULL);

}

else { /*parent process */

/*parent will wait for the child to complete*/

printf("I am the parent %d\n",pid);

wait(NULL);

printf("Child Complete\n");

}

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Working Code :-

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int n=35;
int main(){
pid_t pid;
pid = fork();
if (pid<0){ 
fprintf(stderr,"Fork failed\n");
return 1;
}
else if (pid == 0) { 
    execv("child",NULL);
printf("Now executing child \n ");
while(1){
    printf("%d ",n);
    if(n==1)
    break;
    else
    {
        if(n%2==0)
        n=n/2;
        else
        n = (3*n)+1;
    }
}
printf("\n");
exit(1);
}
else { 
printf("first execution by parent class \n");
wait();
printf("Child Complete\n");}
return 0;
}


//Output ;-

/tmp/6KSqV79rez.o first execution by parent class Now executing child 35 106 53 160 80 40 20 10 5 16 8 4 2 1 Child Complete #

//Comments :- keep parent process in wait() state till child process get complete , child process notifies with exit(1) code that it is done then parent process will continue , and also in above code while(1) is written , insense think contrary to above collatz conjecture, if its false then it the code should run infinite times , but when you run you will see it runs for definite time and stope when n value is 1, for simplicity n is predeclared as global variable can change if one wants to

Add a comment
Know the answer?
Add Answer to:
Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • With explanation Please. #include #include <sys/types.h> <unistd.h> int main void ) fork fork fork execip (...

    With explanation Please. #include #include <sys/types.h> <unistd.h> int main void ) fork fork fork execip ( "/bin/ls", return 0 /* Line A /* Line B/ /*Line C/ /. Line D ./ "ls", NULL); Answer the following: (a) Including the initial parent process, how many processes are created by the program? (b) Answer (a) assuming that lines C and D are interchanged in the program (c) Answer (a) assuming that lines B and D are interchanged (instead of C and D)...

  • Problem la Points (20) Draw the process tree for the program shown below. #include<sys/types.h> #include<stdio.h> #include<unistd.h>...

    Problem la Points (20) Draw the process tree for the program shown below. #include<sys/types.h> #include<stdio.h> #include<unistd.h> int value = 5; int main() pid_t pid; pid = forko; if(pid ==0) { */ Child Process */ forkO; value += 15; return 0; else if (pid > 0) {/* Parent process */ forkO; forkO; wait(NULL); printf("Parent: value = %d", value); /* LINE A */ return 0;

  • OPERATING SYSTEMS QUESTION: Given the code below: int main() { pid_t pid; int value = 10;...

    OPERATING SYSTEMS QUESTION: Given the code below: int main() { pid_t pid; int value = 10; pid = fork(); if (pid == 0) { /* child */ value += 8; printf(“CHILD: value = %d\n”, value); /* Line A */ exit(0); } else { /* parent */ value += 5; wait(NULL); \ printf(“PARENT: value = %d\n”, value); /* Line B */ exit(0); } } What is printed at Line B? Explain why in a few sentences.

  • Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any...

    Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorthm: n={n / 2 , if n is even 3 * n + 1 , if n is odd The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n = 35, the sequence is:  35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2,...

  • What is the functionality of the following code? #include #include mainO <stdio.h> <unistd.h> int i,j; j-0:...

    What is the functionality of the following code? #include #include mainO <stdio.h> <unistd.h> int i,j; j-0: printf ("Ready to fork.n) i-fork; if 0) this code.\n"); printf "The child executes for (i-0; i?5; i++) printf("Child j-dn".j); else j-vaito printf("The parent executes this code. Ln" printf ("Parent j-dn",j);

  • CSIT 345 Lab 2 Process Programming Your lab requirement is to write program codes for 3.21 and sh...

    *Help Please with the code** CSIT 345 Lab 2 Process Programming Your lab requirement is to write program codes for 3.21 and shared memory program for producer and consumer as shown in the following. You can start with the code provided in the virtual machine in the virtual box you installed. The code can be found in /home/oscreader/osc9e-src/ch3 a. For 3.21, you can start with the newprocposix.c and modify the code to meet your requirement. Then type: gcc neypCOCROSİS.c to...

  • What is the output? #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main()...

    What is the output? #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int x = 5; int y = 2; int z = 30; x = fork(); y = fork(); if (x != 0) printf("Type 1\n"); if (y != 0) printf("Type 2\n"); z = fork(); if (x > 0 || y > 0 || z > 0) printf("Type 3\n"); if (x == 0 && y == 0 && z != 0) printf("Type 4\n"); if (x...

  • The Collatz conjecture concerns what happens when we take any positive integer n and apply the...

    The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm: if n is even 3 xn+1, if n is odd The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n- 35, the sequence 1S 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4,2,'1 Write a C program using the fork) system call that generates this sequence in the...

  • Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also,...

    Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also, include a screen shot of the output and terminal window of each command you used. Read carefully to do this task please. shell.c code given below. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <string.h> int main() { int PID; char lineGot[256]; char *cmd; while (1){ printf("cmd: "); fgets(lineGot, 256, stdin); // Get a string from user (includes \n) cmd = strtok(lineGot, "\n");...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT