To
get started, copy everything from $cs444/hw4 to your hw4 dir, including subdirs queue and intqueue You can do this in one recursive copy: cp r $cs444/hw4/* .
hw4:
almost the same as hw3 solutions, with an intqueue
subdirectory, like queue, but handles a queue of ints
instead of chars. This is useful for the prodcons
program and perhaps the semaphore service code.
Also it has the user programs for testing semaphores--
User
programs for testing semaphores: testmutex[123].c,
prodcons[123].c.
Note
that although testmutex builds, it doesnt run
properly because it cant yet do real semaphore calls. You need to add new syscalls to ulib.s, headers, and
the kernel, and then uncomment the calls in testmutex.c.
main1:
creates a semaphore of initial count 1 and puts its id in mutex,
a shared variable
exits
main2:
first sleeps for 50 ms, to make sure semaphore is created first
prints A requests mutex (using write)
down(mutex);
prints A has mutex
sleeps for 500 ms, i.e. a half second
prints "After .5
sec sleep, A is releasing mutex"
up(mutex);
exits
main3:
first sleeps for 50 ms, to make sure semaphore is created first
prints B requests mutex
down(mutex);
prints B has mutex
sleeps for 500 ms, i.e. a half second
prints "After .5
sec sleep, B is releasing mutex"
up(mutex);\
exits
Here
is how the testmutex.lnx runs before you implement the semaphore calls, plus
stars for ticks.
Tutor> go 100100
A requests mutex
A has mutex after
this, A blocks in sleep
B requests mutex
B has mutex but A has not released the mutex
yet!
After .5 sec sleep, A is releasing mutex
After .5 sec sleep, B is releasing mutex
Here
is the output from a run with working semaphores.
Tutor> go 100100
A requests mutex
A has mutex after this, A blocks
in sleep
B requests mutex
B starts running, but then blocks on the
semaphore down
After .5 sec sleep, A is releasing mutex
B has mutex finally B gets the mutex after A releases it.
After .5 sec sleep, B is releasing mutex
Comparable
UNIX program:
main1:
creates a semaphore of initial count 1 and puts its id in mutex,
an external variable
calls fork to create child 1
calls
fork to create child 2
waits on child processes termination (to be a good parent)
exits
main2: child
1: can access mutex variable, copied from parent,
to get semaphore id
prints A requests mutex (using write)
down(mutex);
prints A has mutex
sleeps for 500 ms, i.e. a half second
prints "After .5
sec sleep, A is releasing mutex"
up(mutex);
exits
main3:
child2: can access mutex variable, copied from
parent, to get semaphore id
prints B requests mutex
down(mutex);
prints B has mutex
sleeps for 500 ms, i.e. a half second
prints "After .5
sec sleep, B is releasing mutex"
up(mutex);
exits