0% found this document useful (0 votes)
18 views7 pages

Os Lab 9 M

Uploaded by

haleemayasin294
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Os Lab 9 M

Uploaded by

haleemayasin294
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Bahria University

Karachi Campus

LAB EXPERIMENT NO.


09
LIST OF TASKS
TASK NO OBJECTIVE

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.

02 Implement the algorithm of the Producer-Consumer problem given above,


in C language.

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:

1. Mutex (Mutual Exclusion Object)

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:

pthread_mutex_lock(&mutex); // Lock the mutex

// Critical section

pthread_mutex_unlock(&mutex); // Unlock the mutex

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.

MINHAJ UR RIYAN 02-131222-080


[LAB 09] [INTER PROCESS COMMUNICATION I]
[OPERATING SYSTEM]

Example:

while (__sync_lock_test_and_set(&lock, 1)) {

// Wait (spin) until the lock is free

// Critical section

__sync_lock_release(&lock); // Unlock the spinlock

3. Read-Write Locks

Protection: Allow multiple threads to read a resource concurrently but give exclusive access to
a single thread for writing.

Implementation:

- lock_read(): Locks the resource for reading.


- unlock_read(): Unlocks the resource after reading.
- lock_write(): Locks the resource for writing.
- unlock_write(): Unlocks the resource after writing.

Example:

pthread_rwlock_rdlock(&rwlock); // Lock for reading

// Critical section for reading

pthread_rwlock_unlock(&rwlock); // Unlock read lock

pthread_rwlock_wrlock(&rwlock); // Lock for writing

// Critical section for writing

pthread_rwlock_unlock(&rwlock); // Unlock write lock

4. Condition Variables

MINHAJ UR RIYAN 02-131222-080


[LAB 09] [INTER PROCESS COMMUNICATION I]
[OPERATING SYSTEM]

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:

pthread_mutex_lock(&mutex); // Lock the mutex

while (!condition) {

pthread_cond_wait(&cond, &mutex); // Wait for the condition

// Critical section

pthread_mutex_unlock(&mutex); // Unlock the mutex

pthread_cond_signal(&cond); // Signal one waiting thread

5. Monitors

Protection: High-level concurrency construct combining mutexes and condition variables.

Implementation:

Automatically handles mutual exclusion and synchronization.

- wait(): Releases the lock and waits for a condition.


- notify(): Wakes up one waiting thread.

Example:

synchronized(lock) {

while (!condition) {

lock.wait(); // Wait for the condition

MINHAJ UR RIYAN 02-131222-080


[LAB 09] [INTER PROCESS COMMUNICATION I]
[OPERATING SYSTEM]

// Critical section

lock.notify(); // Notify one waiting thread

TASK NO. 02: Implement the algorithm of Producer-Consumer problem given above, in C
language.

SOLUTION:

MINHAJ UR RIYAN 02-131222-080


[LAB 09] [INTER PROCESS COMMUNICATION I]
[OPERATING SYSTEM]

MINHAJ UR RIYAN 02-131222-080


[LAB 09] [INTER PROCESS COMMUNICATION I]
[OPERATING SYSTEM]

OUTPUT:

MINHAJ UR RIYAN 02-131222-080

You might also like