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

Programming Assignment 2 Objective: The objective of this assignment is to famil

ID: 3586104 • Letter: P

Question

Programming Assignment 2

Objective: The objective of this assignment is to familiarize yourself with the notion of a process and the system call fork( ) used to create a new process.

Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm:

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 where 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.

Grading:The program will be graded on the basic functionality, error handling and how well the implementation description was followed. Be sure to name your program collatz.c (no extra characters, capitals) Note that documentation and style are worth 10% of the assignment's grade!

n = 1 n/2, if n is even 3 x n+1, ifn is odd

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>

/*Start the main program*/
int main(int argc, char *argv[])
{

    pid_t   pid;              //process IDs
    int val = 0;
    if(argc < 2 || argc > 2)
    {
        printf("Enter the right usage ./collatg <val> ");
        exit(1);
    }
    if(0 > atoi(argv[1]))
    {
        printf("Negative value entered, Please enter a positive value ");
        exit(1);
    }

    if ((pid = fork()) < 0) /* attempt to create child / parent process */

    {
        printf("fork error");
    }


    /* parent process */
    else if (pid > 0) {
        printf("In parent process ");
        wait(0); // invoke the wait() call to wait for the child process to complete before exiting the program

    } else {

        /* child Process */
        printf("In child process ");
        val = atoi(argv[1]); // convert ascii value entered from command line to integer using atoi()
        while(val >1)
        {

if((val % 2) == 0) // value is an even number
            {
                printf("%d ",val);
                val/=2;
            }
            else   // value is an odd number
            {
                printf("%d ",val);
                val = 3*val + 1;
            }
            if(val == 1) //when val becomes 1, print and exit the loop
            {
                printf("%d ", val);
                break;
            }
        }

    }

    exit(0);

}

         

Direction : Please store the above program in file collaz.c   and run   gcc -o collaz collaz.c

Few sample outputs :

bash-4.1$ ./collaz 76
In parent process
In child process
76      38      19      58      29      88      44      22      11      34      17      52      26      13      40      20 10       5       16      8       4       2       1
bash-4.1$ ./collaz 8
In parent process
In child process
8       4       2       1

bash-4.1$ ./collaz 8 7
Enter the right usage ./collatg <val>

bash-4.1$ ./collaz -5
Negative value entered, Please enter a positive value

bash-4.1$