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

const char *SH_NAME = "/collatz_shared"; const size_t SIZE = 4096; static size_t s_size = 0; static int shm_allocate(size_t size, int *pshmFD, void **pptr) { if (!pshmFD || !pptr) return -1; *pptr = MAP_FAILED; *pshmFD = shm_open(SH_NAME, O_CREAT | O_RDWR, 0666); if (*pshmFD < 0) return -2; if (ftruncate(*pshmFD, size) < 0) return -3; *pptr = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED, *pshmFD, 0); if (*pptr == MAP_FAILED) return -4; return 0; } static void shm_release(size_t size, int shmFD,void *ptr) { if (ptr != MAP_FAILED) munmap(ptr, size); if (shmFD >= 0) shm_unlink(SH_NAME); } int main(int argc, char **argv) { int n; int status; if (argc != 2) { fprintf(stderr, "%s [+ve integer] ", *argv); return 1; } n = atoi(argv[1]); if (n <= 0) { fprintf(stderr, "%s [+ve integer] ", *argv); return 2; } do { s_size += SIZE; int shmFD; void *ptr; if (shm_allocate(s_size, &shmFD, &ptr) < 0) { fprintf(stderr, "Couldn't allocate " "shared memory of size %lu " "bytes ", s_size); return 3; } pid_t pid = fork(); if (pid > 0) { wait(&status); } else if (pid == 0) { int out; while (n > 1) { out = snprintf(ptr, s_size, "%d ", n); if (out >= s_size) return 1; s_size -= out; ptr += out; n = (n % 2) ? 3 * n + 1 : n / 2; } out = snprintf(ptr, s_size, "%d ", n); return (out >= s_size) ? 1 : 0; } else if (pid < 0) { fprintf(stderr, "fork blew %d ", errno); shm_release(s_size, shmFD, ptr); return 4; } if (!WIFEXITED(status)) { fputs("Child did not exit normally", stderr); if (WIFSIGNALED(status)) fprintf(stderr, ", it was terminated by " "signal %d", WTERMSIG(status)); fputc(' ', stderr); shm_release(s_size, shmFD, ptr); return 5; } else if (WEXITSTATUS(status) == 0) fputs(ptr, stdout); shm_release(s_size, shmFD, ptr); } while (WEXITSTATUS(status) > 0); return 0; } This code gives output for given collatz as ./collatz15 35 35 106 53 160 80 40 20 10 5 16 8 4 2 1 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,

#include stdio.h

#include sys/mman.h

#include sys/stat.h

#include sys/types.h

#include sys/wait.h

#include fcntl.h

#include unistd.h

#include stdlib.h

#include errno.h

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

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions