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.
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;
}
Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any...
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...
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;
}
1. Write program codes for 3.21 a and...
*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...
c++ The Collatz Conjecture is a conjecture in mathematics that concerns a sequence sometimes known as hailstone numbers. Given any positive integer n, the following term, n+1 is calculated as follows: If n is even, then n+1 is defined as n/2. If n is odd, then n+1 is defined as 3n + 1 The Collatz Conjecture states that, for any value of n, the sequence will always reach 1. Once the pattern reaches 1, it repeats indefinitely (3 * 1...
Write a program using the fork() system call to do thefollowing. The parent process (main program) forks a process(CHILD 1) to compute and print the sum of first n integers where n is a variable shared between the parent and CHILD 1. Have the parent invoke the wait () call to wait for both the child processesto complete before exiting the program. IN A C program
Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process (CHILD 1) to compute and print the sum of first n integers where n is a variable shared between the parent and CHILD 1. It also forks another process (CHILD 2) that finds the sum of squares of the first n numbers where n is a variable it shares with the parent. Let CHILD 1 print “The sum...
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... It is defined by the following mathematical expression, with X0 & X1 being 0 and 1, respectively: Xn = Xn-1 + Xn-2 Write a C program using the fork() system call that generates and prints the Fibonacci sequence in the child process. The number of members in the sequence will be determined by a user provided as a user prompted input. Make the parent...
Question 3: [2+2]a) What, to whom and how many values a fork system call returns? b) Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in which Terminal login must be your arid number.int main(){ pid_t fork_return; fork_return = fork(); if (fork_return == 0) { execlp("/bin/ls", "ls", NULL); printf("Child process ID: %d\n", getpid()); exit(0); } else { wait (NULL); printf("Parent process ID: %d\n", getpid()); } return 0;}
Arid No is 19-arid-898Question 3: [2+2]a) What, to whom and how many values a fork system call returns? b) Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in which Terminal login must be your arid number.int main(){ pid_t fork_return; fork_return = fork(); if (fork_return == 0) { execlp("/bin/ls", "ls", NULL); printf("Child process ID: %d\n", getpid()); exit(0); } else { wait (NULL); printf("Parent process ID: %d\n",...
1. Process control system calls: The demonstration of fork, execve and wait system calls along with zombie and orphan states.a. Implement the C program in which main program accepts the integers to besorted. Main program uses the fork system call to create a new process called a child process. Parent process sorts the integers using merge sort and waits for child process using wait system call to sort the integers using quick sort. Also demonstrate zombie and orphan states.b. Implement...