CS 110 Intro to Computer Systems
Semaphores Project
Version 1.00
Due 28 September, 9 PM
20 Points
Introduction
You are to expand on your fork program to have the
Parent controlled by the Child.
After forking the Child the Parent:
-
prints out its PID
-
prints out the Child's PID
-
sleeps for 2 seconds
-
once awaken the Parent waits on an event (semaphore).
-
Upon receiving the event, the Parent prints out the time
and updates a semaphore indicating that it is exiting,
and then dies.
Once started the Child:
-
sleeps for 1 second
-
prints out its PID
-
prints out the Parent PID
-
signals the event
-
sleeps for 3 seconds
-
once awaken, the Child prints out the time,
waits for the parent to die, and then the child dies.
Stuff you NEED to Know/Do
-
You
cannot
use busy waiting.
-
You are to use Semaphores
(note, be sure to use the ones described in class),
see below about
wrapping these calls in a
class
or
wrapper.
-
You are to collaborate in groups of
two,
We will NOT assign partners,
find someone who is willing to discuss the project,
your code, etc.
Of courses,
each of you
is to write your own code.
Use your partner to help debug and
test your solution.
This means that you MUST contact your partner first prior to
sending help requests to the graders or tutors or cs staff.
Indicate your partner in your code comments.
Stuff you need to Turn-in and Submit
-
Turn-in (bin outside mike's office):
-
a copy of your commented program
-
and a script file of its execution.
We do NOT need your Makefiles.
-
Submit
-
Submit your source code by running 'cs110submit'
This is Project 03.
Again, we do NOT need your Makefiles.
Implementation Notes:
-
You are to create a C/C++ wrapper/class to handle the management of semaphores.
This wrapper/class should have at a minimum operations to
create,
set,
delete,
up
and
down
a semaphore.
You can view this wrapper/class as a reasonable interface to the unreasonable
interface provided by the OS system calls.
You can of course create additional routines (and should),
e.g.,
print.
-
Note the word class is used here as an abstraction.
You do NOT have to create true C++ Classes.
Rather you can create your own library,
each routine of which, makes the actual system calls.
Semaphore Notes:
Turing
has only a certain number of semaphores.
They get eaten up unless they are freed when your
program terminates.
You can use
ipcs to see what semaphores are available, but it will
only show ones which are yours, or are group or world
readable.
Thus using
0644 | IPC_CREAT for the third arg of
semget
helps in determining who has all the semaphores.
Using 0 for the key means that the semaphore will be private
to a single process and will not be seen by other processes,
so don't do that.
If you end up with hidden semaphores which the CS Staff must
remove, you will lose points.
Returning Semaphores to the System:
-
From inside a program,
you can use
semctl(semid, 0, IPC_RMID, 0);
to free all semaphores in
the group identified by
semid.
This is the correct way to do it,
but semaphores tend to stay alive during debugging.
-
At the command level
you can free semaphores
by using
ipcrm .
-
A short shell script to remove all your semaphores:
#/bin/sh
ipcs | egrep YourUserName | awk '{print $2}' | xargs -n1 ipcrm -s;
ipcs | egrep YourUserName | awk '{print $1}' | xargs -n1 ipcrm -s
-
A short perl script to remove all your semaphores
and shared memory
(save it in an executable file,
your uid is an argument):
#!/usr/local/bin/perl
$username = shift(@ARGV);
@output = `ipcs | grep $username | cut -c1-12`;
foreach (@output)
{
chop;
print `ipcrm -$_`;
}
-
try /usr/local/share/bin/removeipc
It will remove all shared memory, semaphores,
and
message queues that you currently own.
Last modified September 26, 01 by mike@cs.hmc.edu