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

OS Lab 5 23102C0019

The document outlines an experiment on the Dining Philosophers Problem as part of an Operating Systems course. It aims to teach students about synchronization challenges in concurrent programming, focusing on avoiding deadlock and starvation while ensuring mutual exclusion. The provided Java program demonstrates a solution using semaphores to manage resource sharing among philosophers.

Uploaded by

sathishshreya8
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)
12 views7 pages

OS Lab 5 23102C0019

The document outlines an experiment on the Dining Philosophers Problem as part of an Operating Systems course. It aims to teach students about synchronization challenges in concurrent programming, focusing on avoiding deadlock and starvation while ensuring mutual exclusion. The provided Java program demonstrates a solution using semaphores to manage resource sharing among philosophers.

Uploaded by

sathishshreya8
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/ 7

Department of Computer Engineering

A.Y-2024-25

Semester S.E. Semester IV – CMPN


Subject Operating System
Subject Professor In- Prof. Snehal Andhare
charge
Assisting Teachers Prof. Snehal Andhare
Laboratory

Student Name Shreya sathish


Roll Number 23102C0019
Grade and Subject Teacher’s
Signature

Experiment Number PBLE


Experiment Title Dining Philosophers Problem
Objectives The objective of the Dining Philosophers Problem is to design a
(Skill Set / Knowledge solution where multiple philosophers share resources (forks) and can
Tested / Imparted) eat without causing deadlock, starvation, or resource contention. The
goal is to ensure mutual exclusion (only one philosopher can use a fork
at a time), avoid situations where no philosopher can eat (deadlock),
and ensure that all philosophers get a chance to eat (starvation-free).
Description
The Dining Philosophers Problem is a synchronization challenge where
multiple philosophers share resources (forks) to eat. The goal is to avoid
deadlock (where philosophers wait forever) and starvation (where some
philosophers never eat), while ensuring mutual exclusion (no two philosophers
use the same fork at the same time) and efficient concurrency (maximizing the
number of philosophers eating at once).
import java.util.concurrent.Semaphore;
Program
public class DiningPhilosophers {

static final int N = 5;


static Semaphore[] forks = new
Semaphore[N];
static Semaphore waiter = new Semaphore(N -
1);

Lab Work – Course _Operating System___ – Semester _4__- Program _CMPN SE____ P age |1
Department of Computer Engineering
A.Y-2024-25

static class Philosopher extends Thread {


int philosopherId;

Philosopher(int philosopherId) {
this.philosopherId = philosopherId;
}

@Override
public void run() {
for (int i = 0; i < 3; i++) {
try {

System.out.println("Philosopher " +
philosopherId + " is thinking.");
Thread.sleep((int)
(Math.random() * 2000) + 1000);

System.out.println("Philosopher " +
philosopherId + " is hungry.");
waiter.acquire();

int leftFork =
philosopherId;
int rightFork =
(philosopherId + 1) % N;

forks[leftFork].acquire();

System.out.println("Philosopher " +
philosopherId + " picked up left fork " +
leftFork);

forks[rightFork].acquire();

System.out.println("Philosopher " +

Lab Work – Course _Operating System___ – Semester _4__- Program _CMPN SE____ P age |2
Department of Computer Engineering
A.Y-2024-25

philosopherId + " picked up right fork " +


rightFork);

System.out.println("Philosopher " +
philosopherId + " is eating.");
Thread.sleep((int)
(Math.random() * 2000) + 1000);

forks[leftFork].release();

System.out.println("Philosopher " +
philosopherId + " put down left fork " +
leftFork);

forks[rightFork].release();

System.out.println("Philosopher " +
philosopherId + " put down right fork " +
rightFork);

waiter.release();

} catch (InterruptedException
e) {

Thread.currentThread().interrupt();
}
}
}
}

public static void main(String[] args) {


for (int i = 0; i < N; i++) {
forks[i] = new Semaphore(1);
}

Lab Work – Course _Operating System___ – Semester _4__- Program _CMPN SE____ P age |3
Department of Computer Engineering
A.Y-2024-25

Philosopher[] philosophers = new


Philosopher[N];
for (int i = 0; i < N; i++) {
philosophers[i] = new
Philosopher(i);
philosophers[i].start();
}

for (int i = 0; i < N; i++) {


try {
philosophers[i].join();
} catch (InterruptedException e) {

Thread.currentThread().interrupt();
}
}

System.out.println("\nAll philosophers
have finished eating.");
}
}
Output

Lab Work – Course _Operating System___ – Semester _4__- Program _CMPN SE____ P age |4
Department of Computer Engineering
A.Y-2024-25

Lab Work – Course _Operating System___ – Semester _4__- Program _CMPN SE____ P age |5
Department of Computer Engineering
A.Y-2024-25

Conclusion The Dining Philosophers Problem illustrates the challenges of resource


sharing and thread synchronization in concurrent systems. It
emphasizes avoiding deadlock, starvation, and ensuring mutual
exclusion while managing shared resources. The problem provides
valuable insights into designing efficient and fair solutions for
concurrent programming.

Lab Work – Course _Operating System___ – Semester _4__- Program _CMPN SE____ P age |6
Department of Computer Engineering
A.Y-2024-25

Lab Work – Course _Operating System___ – Semester _4__- Program _CMPN SE____ P age |7

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