Question

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, 1

The idea is to 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.

Collatz Program Implementation

The collatz program (collatz.c) is a simple text-based program that takes one argument from the command line, again no prompting the user from within the program.

1. To start the collatz program

./collatz <start number>

where<start number> is the starting number (n) for the collatz conjecture.

Error Handling

Perform the necessary error checking to ensure that a positive integer is passed on the command line.

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

Copyable Code:

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main()

{

//initializes the number num = 0

     int num=0;

     //declare the parent id

     pid_t p;

     do

     {

     //get the number

          printf("Please enter a number greater than 0 for given program: ");

          scanf("%d", &num);

     }while (num <= 0);

     //initialize the parent id to fork function

     p = fork();

     //if parent id is equal to 0

     if (p== 0)

     {

          printf("Child is working...\n");

          printf("%d\n",num);

          /*this is for check and print all positive

          numbers will finally reach 1*/

          while (num!=1)

          {

              if (num%2 == 0)

              {

                   num = num/2;

              }

              else if (num%2 == 1)

              {

                   num = 3 * (num) + 1;

              }   

              printf("%d\n",num);

          }

          printf("Child process is completed.\n");

     }

     else

     {

          printf("Parents process is waiting on child process...\n");

          //call the waiting function

          wait();

          printf("Parent process is completed.\n");

     }

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any...
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
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
ADVERTISEMENT