You are on page 1of 1

Programming Lab – I

System Programming Assignment Week # 10

1. Write a program using Semaphore for mutual exclusion between processes sharing memory. In
this program, a parent process creates a shared memory segment, attaches the segment in its address
space, and write 1000 in a variable in the shared memory before creating 2 children processes A and
B. A fter that, the parent waits for children termination and destroys the shared memory segment.
Each child process performs 5 iterations. Every iteration process A adds 200 to the shared variable,
and process B adds 100 to the shared variable. To help race conditions appear, both processes sleep
for a random amount of time between the time they read the shared variable and the time they write
back the shared variable after modification.

2. The goal of this problem is to solve a producer/consumer problem using semaphores. You will
use the pthread package to create 4 producer threads and 4 consumer threads. Each producer thread
attempts to insert character ’X’ into a circular buffer of size 3,000,000 characters. Each consumer
thread tries to removes a character from the buffer.

3. Consider an orchestra conductor and a group of musicians. The musicians play independently but
are coordinated by the conductor. For each measure of music indicated by the conductor’s baton,
the musicians play a set of notes. The musicians do not move onto the next measure until indicated
by the conductor’s baton. The Orchestra is called the Narcolepsy Symphony Orchestra because
occasionally a musician falls asleep. When this happens the rest of the musicians continue playing.
When the sleeping musician wakes up, the musician begins playing again with the orchestra. Here
are some guidelines:
• Simulate 100 measures
• Simulate 3 to 10 musicians
• use one thread per musician. The thread doesn't die at the end of the measure... it
lives for theduration of the piece.
• use semaphores as the synchronization mechanism not pthread_join (since musician
threads liveacross measures)
• for each measure, there is a 25% chance that the musician will fall asleep
• when a musician sleeps, it is for a random number of measures (say 1-5)... this is
sleep amount isunknowable to the conductor.. the musician simply "wakes up" when
it is ready to start playingagain.
• use the sleep method sleep() method to simulate sleep, sleeping players do a
sleep(random 1-5)seconds to simulate sleeping.
• do not to use sleep as a synchronization mechanism
• in addition to outputting that it is playing, the musician thread should sleep(1) to
simulate playing a measure...
• provide feedback for each measure that looks something like this:

Sample Output:

MEA SURE 1: Musician 3 playing Musician 4 playing Musician 1 playing Musician 2 playing

MEA SURE 2: Musician 1 playing Musician 3 playing Musician 4 playing

MEA SURE 3: Musician 4 playing Musician 1 playing Musician 3 playing

MEA SURE 4: Musician 3 playing Musician 2 playing

Note: Musician 2 is sleeping in Measure 2 & 3 and Musicians 3 & 1 are sleeping in Measure 4.

You might also like