CS 444 Intro to hw4: A Semaphore Service for Tiny UNIX

 

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/* . NOTE: This hw4 directory will be available on Friday, Nov. 29, along with hw3 solutions. The delay is to honor late days still in play for hw3.

 

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 doesn’t run properly because it can’t 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