
CECS 326 โ Lab Exercise 10/18/05 (20 points) โ Due 10/27/05
When there are concurrent processes trying to send output to the same monitor, their outputs are likely to be mixed up in
an unpredictable manner, rendering the output to be unreadable. In this lab exercise we will learn to use semaphore, a
mechanism available on Linux, to provide control of the access to shared resources (such as the monitor) by concurrent
processes. To help you learn about this mechanism, two sample program named semaphore.cpp and semdemo1.cpp are
provided for your reference. To understand this mechanism please reference manual pages for semget, semctl, and
semop.
Do the following for this assignment:
1. Create a subdirectory named ex-10-18-05 within your htdocs/cecs326 subdirectory. Set its permission to world
readable and world executable.
2. Copy the sample C++ programs mixedup_p.cpp, mixedup_c.cpp, semaphore.cpp and semdemo1.cpp from
~lam/htdocs/cecs326/samples into this subdirectory. Compile the programs semaphore.cpp and semdemo1.cpp into
semaphore and semdemo1 respectively, execute them and try to understand what the code in each program does.
When executing semdemo1, run one foreground and one background process of the same program. Then add a prolog
comment section and other comments throughout the programs as needed in these programs to explain what each of
the programs does.
3. Modifiy the two programs mixedup_p.cpp and mixedup_c.cpp to add the control using the semaphore mechanism so
that output from the loop in each of the three processes will appear on one line (i.e., no interleaving of output
generated in the loops in the three processes.)
4. Hand in a hardcopy of the revised mixedup_p.cpp and mixedup_c.cpp, the annotated semaphore.cpp and
semdemo1.cpp, and printouts of two different runs of the mixedup_p program.