Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

using c programming The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm: n={n2 if n is

using c programming

The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm:

n={n2 if n is even 3n+1 if n is odd

The conjecture states that when this algorithm is continually applied all positive integers will eventually reach 1. Write a C program using fork() system call that generates this sequence in the child process. The starting number will be provided from the command line. Because the parent and the child processes have their own copies of the data, it will be necessary for the child to output the sequence in Exercise 3.21. Another approach to designing this program is to establish a shared-memory object between the parent and child processes. This technique allows the child to write the contents of the sequence to the shared-memory object. The parent can then output the sequence when the child completes. Because the memory is shared, any changes the child makes will be reflected in the parent process as well.

This program will be structured using POSIX shared memory as described in Section 2.51. The parent process will progress through the following steps:

a. Establish the shared memory object (shm_open(), ftruncate(), and mmap())

b. Create the child process and wait for it to terminate.

c. Output the contents of the shared memory.

d. Remove the shared-memory object.

One area of concern with cooperating processes involves synchronization issues. In this exercise, the parent and child processes must be coordinated so that the parent does not output the sequence until the child finishes execution. These two processes will be synchronized using the wait() system call: the parent process will invoke wait(), which will suspend it until the child process exits. My program from 3.21 is below. If you could please build off of this.

#include

#include

#include

#include

int main(){

int n;

printf(" Enter a positive number: ");

scanf("%d", &n);

if(n>0){

collatz(n);

}

else{

printf("please enter a positive number. ");

}

return 0;

}

int collatz(int i){

pid_t child_pid, wpid;

int waiting = 0;

if ((child_pid = fork())==0){

while(i != 1){

printf("%d ", i);

if(i%2==0){

i/=2;

}

else{

i = 3*i+1;

}

}

printf("1 ");

exit(1);

}

while((wpid = wait(&waiting))>0){

printf("child end ");

}

return;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions