0% found this document useful (0 votes)
19 views6 pages

Dining Philosopher Sleeping Barber Id32

Uploaded by

aboltabol092
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)
19 views6 pages

Dining Philosopher Sleeping Barber Id32

Uploaded by

aboltabol092
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/ 6

AHSANULLAH UNIVERSITY OF SCIENCE AND TECHNOLOGY (AUST)

141 & 142, Love Road, Tejgaon Industrial Area, Dhaka-1208.

Department of Computer Science and Engineering


Program: Bachelor of Science in Computer Science & Engineering

Assignment

Course No: CSE3213


Course Title: Operating System

Date of Submission: 04/01/24

Topic: Dining Philosophers and Sleeping Barber Problem

Submitted to,

Mr. Mohammad Moinul Hoque


Associate Professor, Department of CSE, AUST.

Submitted by,

Tahsin Shadman - 20200204032


Dining Philosophers Problem
Description:
The Dining Philosophers Problem tells us about N philosophers sitting at a
round table, everyone needs two forks to eat, but we have only N forks
available, one between every pair of philosophers. Our target is to enable all
of the philosophers to eat without deadlock and ensure maximum
concurrent eating.

Pseudo-Code: }
}
#define N 5
typedef int semaphore;

#define LEFT (i + N - 1) % N
#define RIGHT (i + 1) % N void take_forks(int i) {
down(&mutex);
#define THINKING 0 state[i] = HUNGRY;
#define HUNGRY 1 test(i);
#define EATING 2 up(&mutex);
down(&s[i]);
int state[N]; }
semaphore mutex = 1;
semaphore s[N]; void put_forks(int i) {
down(&mutex);
void philosopher(int i) { state[i] = THINKING;
while (TRUE) { test(LEFT);
think(); test(RIGHT);
take_forks(i); up(&mutex);
eat(); }
put_forks(i);
void test(int i) { state[i] = EATING;
if (state[i] == HUNGRY && up(&s[i]);
state[LEFT] != EATING && }
state[RIGHT] != EATING) { }

Explanation and Scenarios:

Scenario 1: Initial State


- All philosophers start in the `THINKING` state.
- All forks are available first.

Scenario 2: Philosophers Attempting to Eat


- A philosopher decides to eat, making it to the `HUNGRY` state and
attempts to get both forks.
- If both forks are available, the philosopher starts eating (`EATING` state).
- If one or both forks are not there, the philosopher remains `HUNGRY`,
waiting for freeing both forks

Scenario 3: Deadlock Avoidance


- If all philosophers simultaneously pick up their left forks, they succeed.
- However, none can pick up their right forks, potentially causing a
deadlock.

Scenario 4: Resolving Deadlock


- If a philosopher can't acquire both forks, it returns the left fork and starts
thinking again.
- Another philosopher may then acquire the left fork and try to obtain the
right fork.
- This process continues until a philosopher successfully gets both forks and
start eating.

Explanation:
- Each philosopher follows a routine of thinking, attempting to acquire
forks, eating, and putting down forks.
- The state array keeps track of the philosopher's status (`THINKING`,
`HUNGRY`, or `EATING`).
- The `mutex` semaphore ensures exclusive access to modify the
philosopher's state.
- The `test` function checks adjacent philosophers' states to permit a
philosopher to start eating without causing deadlock.

This solution shows that all philosophers eventually get a chance to eat,
avoiding deadlock by releasing forks if they can't be acquired.

Sleeping Barber Problem Report

Description:
The Sleeping Barber Problem shows a scenario with one barber, a barber
chair, and a number of chairs for waiting customer. The challenge is to
synchronize the barber's activity with customer arrivals, ensuring the
barber serves customer when available and rest when there is no one.
Customers must be served by the barber or leave if there are no chairs.

Pseudo-Code:
#define CHAIRS 5
typedef int semaphore;

semaphore customers = 0;
semaphore barbers = 0;
semaphore mutex = 1;
int waiting = 0;

void barber(void) {
while (TRUE) {
down(&customers); // Sleep if no customers are waiting
down(&mutex); // Acquire access to 'waiting'
waiting = waiting - 1; // Decrement count of waiting customers
up(&barbers); // Barber is now ready to cut hair
up(&mutex); // Release 'waiting'
cut_hair(); // Barber cuts hair (outside critical region)
}
}

void customer(void) {
down(&mutex); // Enter critical region
if (waiting < CHAIRS) { // If no free chairs, leave
waiting = waiting + 1; // Increment count of waiting customers
up(&customers); // Wake up barber if necessary
up(&mutex); // Release access to 'waiting'
down(&barbers); // Sleep if no free barbers
get_haircut(); // Be seated and served
} else {
up(&mutex); // Shop is full; do not wait
}
}

Explanation and Scenarios:

Scenario 1: Barber Waiting for Customers


- The barber initially waits for customers (`down(&customers)`) before
starting any haircut.
- Upon a customer's arrival (`up(&customers)`), the barber is notified to
cut hair (cut_hair()).

Scenario 2: Customer Arrival


- When a customer arrives, they check for available chairs in the waiting
area (waiting < CHAIRS).
- If there are empty chairs, the customer takes a seat, signals their presence
to the barber (up(&customers)), and waits for a haircut (down(&barbers)).
- If there are no empty chairs, the customer leaves the shop.
Scenario 3: Barber Serving Customers
- The barber is awakened by customers arriving (up(&customers)).
- One time a customer is present, the barber reduce the count of waiting
customer (waiting = waiting - 1), shows that a customer is getting service
- After serving the customer, the barber signals that he is available
(up(&barbers)) for another customer.

Explanation:
- The solution uses semaphores (customer, barbers, mutex) to synchronize
the actions of the barber and customers.
- Customers arrive, check for available seating, and signal the barber if they
can be served.
- The barber waits for customers to arrive and serves them one by one,
ensuring mutual exclusion and proper synchronization that avoids race
condition.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy