Computer Science 432/563
Operating Systems
Spring 2016, The College of Saint Rose
Lab 2: Programming Unix Processes and Threads in C
Due: 11:59 PM, Wednesday, February 10, 2016
In this lab, you will gain experience creating and managing processes
and threads in our Unix environments.
All of the programs here are designated as practice programs for
grading purposes.
You may work individually or with a partner on this lab assignment.
Running Class Examples
Run the class examples that used fork and POSIX threads in the
following three environments to answer the questions that follow in a
file answers.txt.
- The Linux system mogul.strose.edu.
- The FreeBSD system ascg.strose.edu.
- The text's virtual Linux environment you set up as part of the
first lab. (If you were unable to do this, there is a way to run it
from the AH 400 suite Windows machines.)
Question 1: Show the output of ps on each system showing the
both the parent and child processes of the forking program,
along with the output of the program on each system. (2 points)
Question 2: Using the virtualized Linux system that you can run under
VirtualBox, compile and run the forkbomb program. How many
processes can you create before you either get an error message (or
the system crashes)? (For me, the system crashed before an error
message was produced, running in VirtualBox hosted on my Mac. If
this happens, you can simply reset the VM!) We won't try this
experiment now on the other systems because we could trigger a
system crash, but I was able to create over 5000 processes on the
FreeBSD system in my office (winterstorm, an old PC repurposed
for me) before running into trouble and well over 23,000 (!) on a
high-end Linux server with a lot of memory. (1 point)
Question 3: Draw a memory diagram showing all of the variables in
existence in the pthreadhello example when both child threads
are executing. (2 points)
The what_shared program when executed on my MacBook Pro (Mac OS
X 10.9.5) is as follows:
First, we create two threads to see better what context they share...
Set this_is_global=1000
Thread 1, pid 75371, addresses: &global: 10C9DB050, &local: 10CA8FEDC
In Thread 1, incremented this_is_global=1001
Thread 2, pid 75371, addresses: &global: 10C9DB050, &local: 10CB12EDC
In Thread 2, incremented this_is_global=1002
After threads, this_is_global=1002
Now that the threads are done, let's call fork..
Before fork(), local_main=17, this_is_global=17
In parent, pid 75371: &global: 10C9DB050, &local: 7FFF53225538
In child, pid 75372: &global: 10C9DB050, &local: 7FFF53225538
Child set local_main=13, this_is_global=23
In parent, local_main=17, this_is_global=17
Question 4: Run the what_shared example in each of the
environments and paste in your output. What important differences
do you see in the output and what do these differences mean? (3 points)
On my Mac, wthe proctree_threads program can create a thread
tree of height 10 without encountering any thread creation errors, but
did encounter errors at height 11.
Question 5: What is the tallest tree you can create on each of the
three environments before thread creation errors are reported? (1 point)
fork() Practice
Practice Program: Write the program for SG&G Programming
Problem 3.21 on p. 155. Call your program forksyr.c. Include
a Makefile that compiles this program into an executable called
forksyr. (10 points)
Notes:
- Do some error checking. If your program is run without a
command-line argument, print a "Usage" line. If your program is
given a negative starting value for the sequence, print an
appropriate error message.
- If you store the entire sequence in an array as you generate it,
allocate the array of an appropriately large size using
malloc(3). Reminder to Java programmers: C has no garbage
collection, so any memory you allocate with malloc() must be
returned to the system with free().
- If you store the values in the sequence with int values,
you will notice they could overflow the storage capabilities of the
data type. You can delay this somewhat by using long values.
Even then, you could overflow the values with certain starting values.
In this case, also print an error message.
Adding POSIX Shared Memory
Practice Program: Read Section 3.5.1 of THE PREVIOUS EDITION OF
SG&G (see your email) to learn about POSIX shared memory.
Then write the program for Exercise 3.22 on p. 155-156 of
SG&G. Call your program forksyrshared.c and
include a Makefile that compiles this program into
forksyrshared. (10 points)
Once your program is working, add a call to sleep(2) (recall that
"2" here refers to the manual section, not the argument to pass) to
your program before you detach and free the shared memory segment. In
a separate window, use the ipcs command to see that your shared
memory segment is listed, and that it goes away when your program
terminates. Save this output and include it in a file ipcs.out
to be included in your submission.
Output Capture: ipcs.out for 2 point(s)
Using POSIX Threads
Practice Program: Write the program for Exercise 4.26 on p. 168-169 of
SG&G. Call your program pthreadfib.c and include a
Makefile that compiles this program into pthreadfib. (10
points)
Submission and Evaluation
This lab is graded out of 40 points.
To submit this lab, place all of the files that you are to turn in
(and nothing else) into a directory called lab2, change to that
directory, and create a "tar file" to submit using a command similar
to:
tar cvf lab2.tar lab2
This will create a file lab2.tar in your directory. Send this
tar file as an attachment to terescoj AT strose.edu by 11:59 PM, Wednesday, February 10, 2016.
Please include a meaningful subject line (something like
"CS432 Lab 2 Submission") and use the exact
filenames specified (for this lab and all semester) to make my job
easier when gathering your submissions together for grading. You
don't want to annoy your grader with misnamed or missing files just
before he grades your assignment. Please do not include any additional files, such as emacs backup files, object files, or executable programs.
Grading Breakdown |
answers.txt responses | 9 points |
forksyr.c correctness | 8 points |
forksyrshared.c correctness | 10 points |
ipcs.out shows shared memory segment | 2 points |
pthreadfib.c correctness | 10 points |
Makefile(s) to build executables | 1 point |
|