Semaphore; http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code34.c code34.c
- A common strategy to avoid race conditions is to use semaphores.
- The use of semaphores is important to prevent simultaneous access to system resources by separate processes or separate threads inside the same process.
- Three system calls to create, use, and release semaphores:
- semget - Returns an integer semaphore index that is assigned by the kernel
- semop - Performs operations on the semaphore set
- semctl - Performs control operations on the semaphore set
- The program shows how to create a semaphore set and how to access the elements of that set. Does the followings:
- Creates a unique key and creates a semaphore
- Checks to make sure that the semaphore is created OK
- Prints out the value of the semaphore at index 0 (should be 1)
- Sets the semaphore (decrements the value of semaphore at index 0 to 0)
- Prints out the value of the semaphore at index 0 (should be 0)
- Unsets the semaphore (increments the value of semaphore at index 0 back to 1)
- Prints out the value of the semaphore at index 0 (should be 1)
- Removes the semaphore
- Study the code.
- Execute several times and observe that how the output changes.
- Is there any possible race conditions? Explain.
Mutex; http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code32.c code32.c
- Several threads and shared data.
- Mutex mechanism (pthread_mutex_lock) is used for concurrent executing.
- Execute code several times and observe that how the execution order of the threads changes.
Cem Ozdogan
2010-04-10