GROUP NUMBER 13 OS
GROUP NUMBER 13 OS
1. Race Condition:
- Define: A race condition occurs when multiple processes or threads access shared resources
concurrently, and the final outcome depends on the order of execution, leading to inconsistent or
incorrect results.
- Example: Two threads incrementing a shared counter without synchronization. If both read
the counter simultaneously, they may overwrite each other’s results, leading to incorrect
increments.
2. Critical Section:
- Identify: In a given code snippet, look for sections where shared resources (variables, files,
etc.) are accessed or modified. These are critical sections.
- Example:
int counter = 0;
void increment() {
3. Mutual Exclusion:
- Explain: Mutual exclusion ensures that only one process or thread can execute the critical
section at a time, preventing race conditions.
1. Semaphore Definition:
- `wait()` (or `P()`): Decrements the semaphore. If the value is negative, the process/thread
blocks.
- `signal()` (or `V()`): Increments the semaphore. If the value is non-negative, a blocked
process/thread is woken up.
- Example in C:
#include <semaphore.h>
sem_t sem;
sem_wait(&sem); // wait()
sem_post(&sem); // signal()
3. Producer-Consumer Problem:
- Example:
void producer() {
while (true) {
produce_item();
sem_wait(&empty);
sem_wait(&mutex);
add_to_buffer();
sem_post(&mutex);
sem_post(&full);
1. Mutex Definition:
2. Mutex Implementation:
- Example in C:
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, NULL);
pthread_mutex_lock(&mutex); // lock()
pthread_mutex_unlock(&mutex); // unlock()
- Use mutexes to ensure only one thread accesses shared resources at a time.
- Mutexes are binary (locked/unlocked), while semaphores can have multiple values.
- Mutexes are typically used for mutual exclusion, while semaphores can be used for signaling.
Task 4: Monitors
1. Monitor Definition:
2. Monitor Implementation:
- Example in Java:
java
class Monitor {
synchronized (lock) {
while (!condition) {
lock.wait();
// Critical section
lock.notifyAll();
}} }
3. Dining Philosophers Problem:
- Write a program that creates multiple threads and uses semaphores, mutexes, and monitors to
synchronize access to shared resources.
2. Analysis:
- Compare the performance of semaphores, mutexes, and monitors under different loads (e.g.,
low, medium, high).
3. Trade-offs:
- Correctness vs. Performance: Strong synchronization ensures correctness but may reduce
performance due to overhead.
Additional Considerations
1. Context Switching:
- Frequent context switching can degrade performance. Minimize it by optimizing
synchronization mechanisms.
2. Synchronization Primitives:
- Explore spinlocks (busy-waiting) and barriers (synchronization points for multiple threads).
3. Hardware Support:
4. Distributed Systems:
- Discuss challenges like network latency, partial failures, and consensus algorithms (e.g.,
Paxos, Raft).