0% found this document useful (0 votes)
12 views5 pages

OS Assignment 2

The document covers key concepts related to inter-process communication (IPC) such as race conditions, critical sections, and semaphores. It includes a code implementation of the bounded buffer producer-consumer problem using semaphores, as well as explanations of the readers-writers problem, the dining philosophers problem, and deadlock conditions. Additionally, it describes the Bankers algorithm for deadlock avoidance with an example of resource allocation.

Uploaded by

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

OS Assignment 2

The document covers key concepts related to inter-process communication (IPC) such as race conditions, critical sections, and semaphores. It includes a code implementation of the bounded buffer producer-consumer problem using semaphores, as well as explanations of the readers-writers problem, the dining philosophers problem, and deadlock conditions. Additionally, it describes the Bankers algorithm for deadlock avoidance with an example of resource allocation.

Uploaded by

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

Operating System Assignment-2

1. Explain the terms related to IPC

1) Race condition: It occurs when multiple processes access shared resources concurrently and the outcome

depends on the sequence of execution.

2) Critical section: A part of the code that accesses shared resources and must not be executed by more than

one process at a time.

3) Mutual exclusion: Ensures that only one process can access the critical section at any given time.

4) Semaphores: Synchronization tools used to control access to shared resources using counters.

5) Bounded waiting: A condition that ensures there is a limit on the number of times other processes can

enter the critical section after a process has requested to enter.

6) Mutex: A mutual exclusion object used to provide locking mechanism to protect shared resources.

7) Pipe: A unidirectional communication channel between processes.

8) Message passing: A method of inter-process communication where processes communicate by sending

and receiving messages.

2. Implementation of Bounded Buffer Producer Consumer Problem using Semaphore

#include <stdio.h>

#include <pthread.h>

#include <semaphore.h>

#define SIZE 5

int buffer[SIZE], in = 0, out = 0;

sem_t empty, full, mutex;

void *producer(void *arg) {


Operating System Assignment-2

int item = 0;

while (1) {

item++;

sem_wait(&empty);

sem_wait(&mutex);

buffer[in] = item;

in = (in + 1) % SIZE;

printf("Produced: %d\n", item);

sem_post(&mutex);

sem_post(&full);

void *consumer(void *arg) {

int item;

while (1) {

sem_wait(&full);

sem_wait(&mutex);

item = buffer[out];

out = (out + 1) % SIZE;

printf("Consumed: %d\n", item);

sem_post(&mutex);

sem_post(&empty);

}
Operating System Assignment-2

int main() {

pthread_t p, c;

sem_init(&empty, 0, SIZE);

sem_init(&full, 0, 0);

sem_init(&mutex, 0, 1);

pthread_create(&p, NULL, producer, NULL);

pthread_create(&c, NULL, consumer, NULL);

pthread_join(p, NULL);

pthread_join(c, NULL);

return 0;

3. Explain the readers and writers problem

This problem deals with synchronization between readers and writers that share a common resource (like a

file).

- Multiple readers can read the resource simultaneously.

- Writers need exclusive access.

The goal is to prevent data inconsistency by allowing:

- No reader should be kept waiting if the resource is currently being read.

- Only one writer can access the resource at a time.

- If a writer is writing, all readers and other writers must wait.

4. Explain the Dining-philosophers problem


Operating System Assignment-2

This is a classic synchronization problem involving 5 philosophers sitting at a table doing one of two things:

thinking or eating.

- Each needs two forks to eat.

- Shared forks between each philosopher can lead to deadlock if all pick up their left fork simultaneously.

The challenge is to design a protocol such that no philosopher starves and deadlock is avoided.

5. What is Deadlock? List the conditions that lead to deadlock.

Deadlock is a situation in which a set of processes are blocked because each process is holding a resource

and waiting for another resource held by another process.

Conditions for deadlock:

1) Mutual Exclusion

2) Hold and Wait

3) No Preemption

4) Circular Wait

6. Explain the Bankers algorithm for deadlock avoidance with an example

Bankers algorithm is used to avoid deadlocks by ensuring a safe state before allocating resources.

It checks if granting a request will lead to a safe state.

Example:

Processes: P0, P1, P2

Resources: A = 10

Allocation: [P0=3, P1=2, P2=2]

Max: [P0=7, P1=5, P2=3]

Available: A = 3
Operating System Assignment-2

Need = Max - Allocation

[P0=4, P1=3, P2=1]

Safe Sequence: P2 P1 P0 (All can finish in order without causing deadlock)

You might also like