Answered step by step
Verified Expert Solution
Link Copied!

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 1
Create a C program ("collatz_sequence.c") that generates a collatz sequence for a given initial start number. The
Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm:
n=2n,ifnis even
n=3n+1n=3n+1,ifnis 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 program will use parent and child processes to create this sequence. The parent process will read a list of
starting numbers from the file "start_numbers.txt" and store them in an array. For each number, the parent will
create a collatz sequence and create a shared-memory object between the parent and child processes to pass the
sequence to the child. The child will open the shared-memory object, output/print 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 "start_numbers.txt"
file:
a. Establish the shared-memory object (shm_open(), ftruncate(), and mmap()).
b. Create a collatz sequence for the read number.
c. Save the sequence to a shared-memory 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 "start_numbers.txt")
and pass different collatz sequence numbers to each child.
The child will progress through the following steps:
a. Establish the shared-memory object (shm_open(), ftruncate(), and mmap()).
b. Output the contents of shared memory.
c. Remove the shared-memory object.
The program exits when all sequences equal to start_numbers 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: ./collatz_sequence start_numbers.txt in the terminal OR use the
command: make runq1 via makefile.
The expected output for Question 2:
$ make runq1
./collatz_sequence start_numbers.txt
Parent Process: The positive integer read from file is 10
Child Process: The generated collatz sequence is 105168421
Parent Process: The positive integer read from file is 53
Child Process: The generated collatz sequence is 53160804020105168421
Parent Process: The positive integer read from file is 99
Child Process: The generated collatz sequence is 992981494482241125628147221134175226134020105168421
Note: When submitting the source code files for Question 1, name them like:
collatz_sequence.c
# Makefile for collatz_sequence.c
# Compiler
CC = gcc
# Compiler Flags
CFLAGS =-Wall -Wextra -std=c99
# Executable Name
TARGET = collatz_sequence
# Source Files
SRC = collatz_sequence.c
# Default Target
all: $(TARGET)
# Build the target executable
$(TARGET): $(SRC)
$(CC) $(CFLAGS)-o $(TARGET) $(SRC)-lrt
# Clean up build files
clean:
rm -f $(TARGET)*.o
# Run the program
run:
./$(TARGET) start_numbers.txt
# Phony targets
.PHONY: all clean run
image text in transcribed

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_2

Step: 3

blur-text-image_3

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

Discuss whether self-actualization should be everyones goal.

Answered: 1 week ago

Question

=+What does this say for the future of the business case for CSR?

Answered: 1 week ago