Os Lab 9 M
Os Lab 9 M
Karachi Campus
Submitted On:
Date: 6/6/2024
1
[LAB 09] [INTER PROCESS COMMUNICATION I]
[OPERATING SYSTEM]
TASK NO. 01: Semaphore is one of the concurrency mechanisms available. Find out about more
concurrency mechanisms. How do these mechanisms protect critical sections? Compare their
implementations with wait() and signal() operations of semaphores.
SOLUTION:
Concurrency Mechanisms:
Protection: Ensures that only one thread can access a critical section at a time, providing
exclusive access to the resource.
- Implementation:
- lock(): The thread locks the mutex, blocking other threads from entering the critical
section.
- unlock(): The thread unlocks the mutex, allowing other threads to lock it.
Example:
// Critical section
2. Spinlocks
Protection: Ensure mutual exclusion by having threads repeatedly check if they can acquire the
lock, without going to sleep.
Implementation:
Uses busy-waiting (`while (lock is not free)`) which can be efficient for short critical sections.
Example:
// Critical section
3. Read-Write Locks
Protection: Allow multiple threads to read a resource concurrently but give exclusive access to
a single thread for writing.
Implementation:
Example:
4. Condition Variables
Protection: Block a thread until a specific condition is met, used with mutexes to manage
synchronization.
Implementation:
- wait(): Releases the mutex and blocks the thread until the condition variable is signaled.
- signal(): Wakes up one waiting thread.
- broadcast(): Wakes up all waiting threads.
-Example:
while (!condition) {
// Critical section
5. Monitors
Implementation:
Example:
synchronized(lock) {
while (!condition) {
// Critical section
TASK NO. 02: Implement the algorithm of Producer-Consumer problem given above, in C
language.
SOLUTION:
OUTPUT: