Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question 1 Create a C program ( collatz _ sequence.c ) that generates a collatz sequence for a given initial start number. The
Question
Create a C program collatzsequence.c that generates a collatz sequence for a given initial start number. The
Collatz conjecture concerns what happens when we take any positive integer and apply the following algorithm:
even
odd
The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach
For example, if the sequence is
The program will use parent and child processes to create this sequence. The parent process will read a list of
starting numbers from the file "startnumbers.txt and store them in an array. For each number, the parent will
create a collatz sequence and create a sharedmemory object between the parent and child processes to pass the
sequence to the child. The child will open the sharedmemory object, outputprint the sequence, and complete
itself. Because the memory is shared, any changes the parent makes will also be reflected in the child process.
The parent process will progress through the following steps for each number read from the "startnumbers.txt
file:
a Establish the sharedmemory object shmopen ftruncate and mmap
b Create a collatz sequence for the read number.
c Save the sequence to a sharedmemory object.
d Create the child process and wait for it to terminate.
The parent will spawn multiple child processes equal to the start numbers provided in the "startnumbers.txt
and pass different collatz sequence numbers to each child.
The child will progress through the following steps:
a Establish the sharedmemory object shmopen ftruncate and mmap
b Output the contents of shared memory.
c Remove the sharedmemory object.
The program exits when all sequences equal to startnumbers are printed on the console.
Use makefile to compile the program written above. The instructions to use and contents of the makefile have
been provided on the MyLS course page. The other implementation details are at your discretion, and you are free
to explore.
To invoke the program, use the command: collatzsequence startnumbers.txt in the terminal OR use the
command: make runq via makefile.
The expected output for Question :
$ make runq
collatzsequence startnumbers.txt
Parent Process: The positive integer read from file is
Child Process: The generated collatz sequence is
Parent Process: The positive integer read from file is
Child Process: The generated collatz sequence is
Parent Process: The positive integer read from file is
Child Process: The generated collatz sequence is
Note: When submitting the source code files for Question name them like:
collatzsequence.c
# Makefile for collatzsequence.c
# Compiler
CC gcc
# Compiler Flags
CFLAGS Wall Wextra stdc
# Executable Name
TARGET collatzsequence
# Source Files
SRC collatzsequence.c
# Default Target
all: $TARGET
# Build the target executable
$TARGET: $SRC
$CC $CFLAGSo $TARGET $SRClrt
# Clean up build files
clean:
rm f $TARGETo
# Run the program
run:
$TARGET startnumbers.txt
# Phony targets
PHONY: all clean run
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started