3 21 The Collatz conjecture concerns what happens when we take any

Content type
User Generated
Rating
Showing Page:
1/5
3.21 The Collatz conjecture concerns what happens when
we take any
positive integer n and apply the following algorithm:
n =
n/2, if n is even
3 × n + 1, if n is 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
Write a C program using the fork() system call that
generates this
sequence in the child process. The starting number will be
provided
from the command line. For example, if 8 is passed as a
parameter on
the command line, the child process will output 8, 4, 2, 1.
Because the
parent and child processes have their own copies of the
data, it will be
necessary for the child to output the sequence. Have the
parent invoke
the wait() call to wait for the child process to complete
before exiting
the program. Perform necessary error checking to ensure
that a positive
integer is passed on the command line.
3.22 In Exercise 3.21, the child process must output the
sequence of numbers
generated from the algorithm specified by the Collatz
conjecture because
the parent and child have their own copies of the data.
Another

Showing Page:
2/5
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 3.5.1. 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 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.

Showing Page:
3/5

End of Preview - Want to read all 5 pages?
Access Now
Unformatted Attachment Preview
3.21 The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm: n = n/2, if n is even 3 × n + 1, if n is 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 Write a C program using the fork() system call that generates this sequence in the child process. The starting number will be provided from the command line. For example, if 8 is passed as a parameter on the command line, the child process will output 8, 4, 2, 1. Because the parent and child processes have their own copies of the data, it will be necessary for the child to output the sequence. Have the parent invoke the wait() call to wait for the child process to complete before exiting the program. Perform necessary error checking to ensure that a positive integer is passed on the command line. 3.22 In Exercise 3.21, the child process must output the sequence of numbers generated from the algorithm specified by the Collatz conjecture because the parent and child have their own copies of the data. 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 sh ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Review

Anonymous
Great study resource, helped me a lot.

Studypool
4.7
Indeed
4.5
Sitejabber
4.4