Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: start with any positive integer n. Then each term is

The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half of the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1. with the following changes. You will fork a process to start the sequence for the Collatz conjecture. It will produce the sequence which is indicated by the number on the command line. The sequence will be given back to the forking process using shared memory. The process (likely main) that did the fork will print the output from the shared memory. Example output is:collatz 11 From Child 1 init n=11, From Child 1 n=34, From Child 1 n=17, From Child 1 n=52, From Child 1 n=26, From Child 1 n=13, From Child 1 n=40, From Child 1 n=20, From Child 1 n=10, From Child 1 n=5, From Child 1 n=16, From Child 1 n=8, From Child 1 n=4, From Child 1 n=2, From Child 1 n=1, The number entered on the command line must be greater than zero and less than 40. Please put the function code in your file. You will need to use stdlib.h if you want to use atoi to translate a character string into an integer. Use sprintf to put values into strings. sprintf returns the number of characters you create and you will need to move a pointer along as you incrementally update the string to go in shared memory or shared memory. You will need to do wait so that the main program finishes after the child (no cascading termination). You will need to use argc and argv to get command line arguments. In your main program you will create the shared memory. Make it at least 4096 in size. For Linux you need to do the following and compile with -lrt. Note in the forked process you will not need ftruncate fd=shm open(name, O CREAT | O RDWR,0666); ftruncate(fd, SIZE); mainptr = (char *) mmap(0,SIZE, PROT READ | PROT WRITE, MAP SHARED, fd, 0); Your main program MUST release shared memory with the command below. If you do not, that is -20 points. shm unlink(name); To find out more about the various system commands try: man function (e.g. man mmap) Be extremely careful that a child process does not itself fork a process or you can fill the process table and lock up the machine. To transfer files, sftp. If you are not familiar with Linux here is some useful information. pwd tells you the directory you are in, cd - changes directories, mkdir creates a new directory, ls lists the files in a directory, rm removes a file, emacs and nano (and vim) are available editors with nano easiest to use. The compiler is gcc. To compile your program: gcc myprogram.c -lrt . You will hand in the source code (.c). The debugger is gdb and you call gdb compiled code name after compiling with gcc using the -g option.

I worked on this code it is working fine.

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

int main(int argc, char *argv[]) { fprintf(stdout, "Starting parent %d ", getpid());

int wstatus, shm_fd; char shm_name[] = "collatz"; struct stat sb; pid_t pid; unsigned int displacement = 0, offset = 0; size_t shm_size = getpagesize() * (argc - 1);

if(argc < 2) { fprintf(stderr, "At least one argument must be passed!"); exit(EXIT_FAILURE); }

int *pid_children = (int*)malloc((argc - 1) * sizeof(int)); if(pid_children == NULL) { fprintf(stderr, "Memory not allocated!"); exit(1); }

/* Create shared memory object */ shm_fd = shm_open(shm_name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); if(shm_fd < 0) { perror(NULL); return errno; }

/* Define size */ if(ftruncate(shm_fd, shm_size) == -1) { perror(NULL); shm_unlink(shm_name); return errno; }

for(int i = 1; i < argc; i ++) { int n = atoi(argv[i]), aux = atoi(argv[i]); if(n < 0) { fprintf(stderr, "Only positive numbers! "); i = i + 1; }

pid_children[i - 1] = fork(); if(pid_children[i - 1] < 0) return errno; if(pid_children[i - 1] == 0) { /* Child instructions */ char *shm_ptr_child = mmap(0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, (i - 1) * getpagesize()); if(shm_ptr_child == MAP_FAILED) { perror(NULL); shm_unlink(shm_name); return errno; }

int count = 0; int *series = (int*)malloc(count * sizeof(int)); if(series == NULL) { fprintf(stderr, "Memory allocation error!"); exit(1); }

*(series + count) = n; while(n != 1) { count = count + 1; series = (int*)realloc(series, (count + 1) * sizeof(int)); if(series == NULL) { fprintf(stderr, "Memory allocation failure!"); free(series); exit(EXIT_FAILURE); }

if(n % 2== 0) n = n / 2; else n = 3 * n + 1;

*(series + count) = n; } if(n == 1){ offset = sprintf(shm_ptr_child, "%d : ", aux); shm_ptr_child += offset; for(int i = 0; i < count + 1; i++) { offset = sprintf(shm_ptr_child, "%d ", series[i]); shm_ptr_child += offset; }

free(series); munmap(shm_ptr_child, getpagesize());

fprintf(stdout, "Done Parent: %d, Me: %d ", getppid(), getpid()); exit(EXIT_SUCCESS); } else exit(EXIT_FAILURE); } else{ /* Parent instructions */ do{ pid = waitpid(pid_children[i - 1], &wstatus, WUNTRACED | WCONTINUED); if(pid == -1) { perror("waitpid"); return -1; }

if(WIFEXITED(wstatus)){ fprintf(stdout, "exited, status = %d ", WEXITSTATUS(wstatus)); }else if(WIFSIGNALED(wstatus)) { fprintf(stdout, "killed by the signal: %d ", WTERMSIG(wstatus)); }else if(WIFSTOPPED(wstatus)) { fprintf(stdout, "stopped by the signal %d ", WSTOPSIG(wstatus)); }else if(WIFCONTINUED(wstatus)) { fprintf(stdout, "continued "); }

}while(!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));

} }

for(int i = 0; i < argc - 1; i++) { char *shm_ptr = mmap(0, getpagesize(), PROT_READ, MAP_SHARED, shm_fd, i * getpagesize()); fprintf(stdout, "%s ", shm_ptr); munmap(shm_ptr, getpagesize()); } shm_unlink(shm_name);

printf(" Done Parent: %d, Children: %d ", getppid(), getpid());

return 0; }

The output is like this:

Starting parent 174 Done Parent: 174, Me: 175 exited, status = 0 35 : 35 106 53 160 80 40 20 10 5 16 8 4 2 1

Final Output requirement :

But I require it in this form From Child 1 init n=35, From Child 1 n=106, From Child 1 n=53, From Child 1 n=160, From Child 1 n=80, From Child 1 n=40, From Child 1 n=20, From Child 1 n=10, From Child 1 n=5, From Child 1 n=16, From Child 1 n=8, From Child 1 n=4, From Child 1 n=2, From Child 1 n=1,

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

Concepts Of Database Management

Authors: Joy L. Starks, Philip J. Pratt, Mary Z. Last

9th Edition

1337093424, 978-1337093422

More Books

Students also viewed these Databases questions

Question

Explain the various methods of job evaluation

Answered: 1 week ago

Question

Differentiate Personnel Management and Human Resource Management

Answered: 1 week ago

Question

Describe the functions of Human resource management

Answered: 1 week ago