OS_2ND_INT (1)
OS_2ND_INT (1)
Process Synchronization is a technique to coordinate the execution of processes so that they can
share resources without conflicts. It ensures that multiple processes can run concurrently without
Example: Suppose two processes, P1 and P2, are trying to write to the same file. If there is no
synchronization, they may end up corrupting the file content. Process synchronization techniques like
mutex or semaphores can be used to ensure that only one process writes to the file at a time.
A Race Condition occurs when two or more processes access shared data and try to change it
Example:
int counter = 0;
void increment() {
counter++;
If two threads run the increment() function at the same time, they might both read the value of counter
as 0, increment it, and write back 1 instead of 2. This is due to the lack of synchronization.
critical section (a section of code that accesses shared resources), no other process is allowed to
Example: Consider two processes P1 and P2 accessing a shared printer. If both try to print at the
same time, the output will be garbled. We need a mechanism to ensure that only one process can
4. What are the Methods to Stop Race Conditions or Synchronization Mechanisms? Explain
withExamples
Mutex (Mutual Exclusion): Ensures that only one process can access a shared resource.
Example:
pthread_mutex_t lock;
pthread_mutex_unlock(&lock);
Monitors: High-level synchronization constructs that provide a lock and condition variables.
Peterson's Algorithm is a classic software-based solution for mutual exclusion between two processes.
Code Example:
int turn;
void process0() {
flag[0] = true;
turn = 1;
flag[0] = false;
void process1() {
flag[1] = true;
turn = 0;
flag[1] = false;
}
6. Semaphore: Explain Producer-Consumer or Bounded Buffer Problem
The Producer-Consumer Problem involves two types of processes, the producer, and the consumer,
that share a fixed-size buffer. The producer adds items to the buffer, and the consumer removes them.
semaphore full = 0;
semaphore mutex = 1;
void producer() {
while (true) {
produce_item();
wait(empty);
wait(mutex);
add_to_buffer();
signal(mutex);
signal(full);
}
void consumer() {
while (true) {
wait(full);
wait(mutex);
remove_from_buffer();
signal(mutex);
signal(empty);
consume_item();
7. Reader-Writer Problem
The Reader-Writer Problem deals with scenarios where multiple readers can access shared data
int readcount = 0;
void reader() {
wait(mutex);
readcount++;
if (readcount == 1) wait(wrt);
signal(mutex);
// Reading
wait(mutex);
readcount--;
if (readcount == 0) signal(wrt);
signal(mutex);
void writer() {
wait(wrt);
// Writing
signal(wrt);
at a round table with one fork between each pair. Philosophers must pick up two forks to eat but can
philosopher(int i) {
wait(fork[i]);
wait(fork[(i+1)%5]);
eat();
signal(fork[i]);
signal(fork[(i+1)%5]);
A Deadlock is a state where a set of processes are blocked because each process is holding a
Necessary Conditions:
Circular Wait: A circular chain of processes exists, where each process waits for a resource held by
the next.
Deadlock avoidance ensures that a system never enters a deadlocked state. It involves analyzing
resource allocation requests to ensure that granting them won't lead to a deadlock.
Check if there exists a safe sequence where all processes can finish.
Deadlock detection involves checking the system's state to identify deadlocks, typically using a wait-
for graph.
Demand Paging is a memory management scheme where pages are loaded into memory only when
Example: If a program uses only a portion of its code, only the necessary pages are loaded, saving
space.
A Page Fault occurs when a program tries to access a page not currently in memory.
Steps:
Page Replacement is needed when a new page needs to be loaded into memory, but there's no free
Popular Algorithms:
FIFO (First-In-First-Out)