The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Assignment 4
The purpose of this assignment is to learn how to use fork() and wait() to create processes. It requires only a small change to collatz.c, but you need to be careful when making this change, or you will lock up your virtual machine. So it is important to understand how process creation works. This has been thoroughly explained in the programming examples provided in the previous modules.
The assignment description and sample output are available here: a4.pdf
The assignment is worth 15 points + 5 extra credit points.
You must upload your source code here. Name your source code file a4.c, and make sure it compiles using gcc a4.c on your linux system. To test your code, we will use gcc a4.c to compile and ./a.out ## to run, where ## is a number between 1 and 35.
Operating Systems – Assignment 4 Your assignment is to do problem 3.21 from the textbook (copied in the last page of this
document) in C, with the following changes.
•
• •
• •
•
•
•
• Your program (which is the parent process) will fork two processes to print their
respective sequence for the Collatz conjecture.
The first child process will produce the sequence which is indicated by the number on the
command line and the second child process will produce the sequence from the command
line number plus 3.
The number entered on the command line will be an integer between 1 and 35.
The variable n will hold the number entered in the command line (see the solution to 3.21
posted in module 7). You must increment this variable by 3 (n = n + 3;) in the parent
process – not in a child process. You should not create another variable to hold this new
value (such as in: m = n + 3;).
The main program (parent process) should print out the value of the argument passed to
each child.
The children should print the child’s “name” (1 or 2), with each number output, - see
sample output.
Each process should also print its own process ID (use getpid()) twice: as soon as it starts
and just before it ends execution (see sample output).
Make sure your program allows the forked processes to run concurrently. If you do the
extra credit (below), you will likely see concurrency illustrated in your ouput.
You will need to call wait twice so that the main program finishes after the children (no
cascading termination). The child processes will not always finish in the order in which
they are forked. 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. Testing of this work must only be done on your own Linux
machine. If you lock up another machine (such as circe or a lab machine) trying this assignment
out, it is a 0 for this assignment! Make sure you know how to terminate a program that is stuck in
an infinite loop: ctrl+c is one way.
You must upload your source code on Canvas. Name your source code file a4.c, and make sure it
compiles using gcc a4.c on your linux machine. To test your code, we will use gcc a4.c to
compile and ./a.out ## to run, where ## is a number between 1 and 35. Extra Credit (5 points)
Make the above program work with any number of child processes.
Allow the user to enter the number of child processes (between 2 and 100). You may get this
number as a command line argument or prompt the user as soon as your program starts. Before
forking each child process (except the first), increment n by 3. Each child will print the Collatz
sequence starting at the new n, for example: child 1 starts the sequence from n, child 2 starts
from (n + 3), child 3 starts from (n + 6), child 4 starts from (n + 9)….
Page 1 of 3 Operating Systems – Assignment 4 Sample Output (with 2 children)
oscreader@OSC:~/test$ gcc a4.c
oscreader@OSC:~/test$ ./a.out 20
Main program's process ID: 1921
Parent says: About to fork child, starting value = 20
Parent says: About to fork child, starting value = 23
Child 2 (ID: 1923) Start sequence at: 23
(Child 2) 70
(Child 2) 35
(Child 2) 106
(Child 2) 53
(Child 2) 160
(Child 2) 80
(Child 2) 40
(Child 2) 20
(Child 2) 10
(Child 2) 5
(Child 2) 16
(Child 2) 8
(Child 2) 4
(Child 2) 2
(Child 2) 1
About to end execution (I'm process 1923).
Parent says: Done waiting for a child.
Child 1 (ID: 1922) Start sequence at: 20
(Child 1) 10
(Child 1) 5
(Child 1) 16
(Child 1) 8
(Child 1) 4
(Child 1) 2
(Child 1) 1
About to end execution (I'm process 1922).
Parent says: Done waiting for a child.
About to end execution (I'm process 1921).
oscreader@OSC:~/test$ Page 2 of 3 Operating Systems – Assignment 4 Page 3 of 3