0% found this document useful (0 votes)
2 views4 pages

Os Assignment

The document discusses key concepts in operating systems, focusing on concurrency, process synchronization, semaphores, the Producer-Consumer problem, and the Dining Philosophers problem. It explains how concurrency allows multiple tasks to be executed simultaneously, the necessity of process synchronization to avoid conflicts, and how semaphores manage access to shared resources. Additionally, it outlines the challenges of resource sharing and deadlock in the Dining Philosophers problem, providing solutions to prevent deadlock.

Uploaded by

Matrix
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)
2 views4 pages

Os Assignment

The document discusses key concepts in operating systems, focusing on concurrency, process synchronization, semaphores, the Producer-Consumer problem, and the Dining Philosophers problem. It explains how concurrency allows multiple tasks to be executed simultaneously, the necessity of process synchronization to avoid conflicts, and how semaphores manage access to shared resources. Additionally, it outlines the challenges of resource sharing and deadlock in the Dining Philosophers problem, providing solutions to prevent deadlock.

Uploaded by

Matrix
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/ 4

Assignment (2)

Name - Shishir Mishra

Branch (CSE)

SUBJECT - OPERATING SYSTEM

1. Explain the Principle of Concurrency


Concurrency is the ability of a system to execute multiple tasks or processes
simultaneously. It allows a system to perform more than one computation at the same
time, increasing efficiency and resource utilization.

Key Points:

● Concurrency is not the same as parallelism. Parallelism involves multiple CPUs,


while concurrency is about task management (can happen even on a single CPU).
● It helps improve responsiveness and resource sharing.

Example:
In a multitasking OS, a user can listen to music while browsing the internet. Both tasks
are executed concurrently.

2. What Is Process Synchronization, and Why Is It Needed?


Process Synchronization is a mechanism that ensures that multiple processes or threads
can access shared resources (like memory or data) safely and without conflict.

Why It's Needed:

● To avoid race conditions, where two or more processes try to access and modify
the same data simultaneously.
● To maintain data consistency.
● To manage critical sections where only one process should access a resource at
a time.

3. How Semaphore Solves the Critical Section Problem (With Example)


A semaphore is a synchronization tool used to manage access to a shared resource by
multiple processes in a concurrent system.

There are two types of semaphores:

● Binary Semaphore (0 or 1): Like a lock.


● Counting Semaphore: Allows a certain number of processes to access a resource.

Working:

Semaphores use two atomic operations:

● wait() or P(): Decreases the semaphore count. If count < 0, the process is
blocked.
● signal() or V(): Increases the count. If there are blocked processes, one is
unblocked.

Example (Binary Semaphore):


semaphore S = 1; // Initialized to 1

Process 1:
wait(S); // Enter critical section
// critical section code
signal(S); // Exit critical section

Process 2:
wait(S);
// critical section
signal(S);

This ensures that only one process can enter the critical section at a time.

4. What Is the Producer-Consumer Problem? How Does It Illustrate a


Classical Synchronization Problem?
The Producer-Consumer Problem is a classic example of a synchronization problem in
which:

● The Producer generates data and puts it into a buffer.


● The Consumer removes data from the buffer and processes it.
● They share a finite-size buffer.

Problem:

● The Producer must wait if the buffer is full.


● The Consumer must wait if the buffer is empty.
Why It’s a Classic Synchronization Problem:

It demonstrates the need for mutual exclusion, synchronization, and bounded buffer
management.

Semaphores Used:

● mutex for mutual exclusion.


● full for tracking filled slots.
● empty for tracking empty slots.

Pseudocode (Simplified):
semaphore mutex = 1;
semaphore full = 0;
semaphore empty = N; // N is buffer size

Producer:
wait(empty);
wait(mutex);
// add item to buffer
signal(mutex);
signal(full);

Consumer:
wait(full);
wait(mutex);
// remove item from buffer
signal(mutex);
signal(empty);

5. Explain Dining Philosopher Problem with Suitable Solution and Deadlock


Condition
The Dining Philosophers Problem illustrates the challenges of resource sharing and
deadlock prevention.

Problem Setup:

● 5 philosophers sit at a round table.


● Each needs two forks (one on the left and one on the right) to eat.
● They must think, pick up two forks, eat, and then put down the forks.

Issue:

If all pick up one fork at the same time, none can pick up the second fork → deadlock.
Deadlock Conditions:

1. Mutual Exclusion – Only one philosopher can use a fork at a time.


2. Hold and Wait – A philosopher holds one fork while waiting for another.
3. No Preemption – A fork can't be forcibly taken.
4. Circular Wait – Each philosopher waits on the next in a circular chain.

Solution (Avoid Deadlock):

Method 1: Asymmetric Approach

● Odd-numbered philosophers pick up left fork first, then right.


● Even-numbered philosophers pick up right fork first, then left.

Pseudocode:

# Using threading and semaphores in Python (simplified concept)


from threading import Semaphore

forks = [Semaphore(1) for _ in range(5)]

def philosopher(i):
left = forks[i]
right = forks[(i+1) % 5]

if i % 2 == 0:
left.acquire()
right.acquire()
else:
right.acquire()
left.acquire()

print(f"Philosopher {i} is eating")


left.release()
right.release()

This breaks the circular wait condition, preventing deadlock.

You might also like