Question: #include #include #include #include #include #include /*The Main Function Start*/ void main(int argc, char *argv[]) { /*Storing The Process Id*/ pid_t pid; int j; int

 #include #include #include #include #include #include /*The Main Function Start*/ void

#include #include #include #include #include #include

/*The Main Function Start*/ void main(int argc, char *argv[]) { /*Storing The Process Id*/ pid_t pid; int j; int status = 0;

/*process of forking*/#

if (argc == 1){ fprintf(stderr,"Usage: ./hw1 "); } int n = atoi(argv[1]); pid=fork(); if (pid == -1){ printf("Error in forking.... "); exit(0); } /*Child process*/ if (pid == 0) { printf("Child PID: %d ",getpid());

while (n != 1){ printf("%d ",n); if (n % 2 == 0){ n = n/2; } else { n = 3*n + 1; } } printf("1 "); } else{ printf("Parent PID: %d ",getpid()); /*Waiting for the child to finish*/ wait(0); } exit(0); }

So the code output is this

main(int argc, char *argv[]) { /*Storing The Process Id*/ pid_t pid; intf

But i want the code to print two parent id and child like this, need help correcting my code

j; int status = 0; /*process of forking*/# if (argc == 1){lfprintf(stderr,"Usage: ./hw1 "); } int n = atoi(argv[1]); pid=fork(); if (pid ==-1){ printf("Error in forking.... "); exit(0); } /*Child process*/ if (pid ==

Programming Problems 155 3.21 The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm if n is even if n is odd | 3X11+I, 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,1 Write a C program using the fork() system call that generates this sequence in the child process. The starting number will be provided from the command line. For example, if 8 is passed as a parameter on the command line, the child process will output 8, 4, 2, 1. Because the parent and child processes have their own copies of the data, it will be necessary for the child to output the sequence. Have the parent invoke the wait) call to wait for the child process to complete before exiting the program. Perform necessary error checking to ensure that a positive integer is passed on the command line

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!