Questions tagged [dining-philosopher]

The dining philosophers problem is an example problem often used in concurrent algorithm design to illustrate synchronization issues and techniques for resolving them.

5 philosophers sit around a circular table. Each philosopher spends his life alternatively thinking and eating. In the centre of the table is a large plate of noodles. A philosopher needs two chopsticks to eat a helping of noodles. Unfortunately, as philosophy is not as well paid as computing, the philosophers can only afford five chopsticks. One chopstick is placed between each pair of philosophers and they agree that each will only use the chopstick to his immediate right and left.

The problem to solved is how should the philosophers behave to avoid starvation or deadlocks.

For a full description and discussion, see the wikipedia article.

96 questions
1
vote
5 answers

Deadlock when calling two synchronized method

class Downloader extends Thread { private InputStream in; private OutputStream out; private ArrayList listeners; public Downloader(URL url, String outputFilename) throws IOException { in =…
1
vote
1 answer

Dining philosophers : Chandy-Misra approach : how does it avoid a deadlock?

I am trying it out , However a question : In the wiki , the 3rd point of that algorithm says : When a philosopher with a fork receives a request message, he keeps the fork if it is clean, but gives it up when it is dirty. If he sends the fork…
Somjit
  • 2,503
  • 5
  • 33
  • 60
1
vote
1 answer

Memory leak in Java? Dining philosophers implemented with Semaphores

I seem to have created a memory leak in Java, which I didn't even realize was possible. I implemented one solution to the dining philosophers concurrency problem, based on a figure in Andrew Tanenbaum's book Modern Operating Systems. It works fine…
1
vote
1 answer

pthread_cond_wait strange behaviour

I'm trying to solve Dining philosophers problem using C++. Code is compiled with g++ -lpthread. Entire solution is on philosophers github. Repository contains two cpp files: main.cpp and philosopher.cpp. "Main.cpp" creates mutex variable, semaphore,…
akegalj
  • 379
  • 2
  • 10
0
votes
2 answers

How to use multiple fork() running at the same time? and how to use semaphore in c?

I'm working on assignment where the professor is asking for a solution to solve the dinning philosopher problem using semaphores. Here is my code so far: #include #include #include #include #include…
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0
votes
1 answer

how can i force deadlock state in this code regarding "DINING PHILOSOPHERS PROBLEM"?

Having written the code regarding the dinner philosophers problem which avoids the deadlock, I now want to implement the code such that the deadlock occurs I know that deadlock can occur if each of the philosophers is holding only one wand and waits…
0
votes
1 answer

is there a way to create a thread that will check other thread in C, dinning philosophers implementation

I have a question regarding threads in C, I know that to create a thread the function pthread_create is needed and I'm currently working on the dining philosopher problem and in this implementation of that problem I have to look if a philosopher has…
dieri
  • 23
  • 6
0
votes
0 answers

Why does this code have deadlock? I've broken the circular wait, so it should't happen (dining philosophers)

Main.java: package esempio2; public class Main { public static void main(String args[]){ int numFilosofi = 5; Forchetta forchette[] = new Forchetta[numFilosofi]; Filosofo filosofi[] = new Filosofo[numFilosofi]; …
Allexj
  • 1,375
  • 6
  • 14
  • 29
0
votes
0 answers

Multiple threads in Java for resolving deadlock

I'm having trouble with the deadlock problem with threads. It seems the problem is about how threads work. I have 2 questions here. Although I created 5 threads and start them, not all of them call run(). It's random but I don't know why. There's…
Haru
  • 71
  • 5
0
votes
0 answers

Is using two lock statements one after another thread safe?

I've been learning about multi-thread programming and working on the dining philosophers problem. I'm trying to cause a deadlock without sleeping any threads. Here is the code snippet that I'm using: public class Program { const int…
Ali Ihsan Elmas
  • 174
  • 4
  • 15
0
votes
2 answers

(C) Dining Philosophers - How to make concurrent threads wait till a condition is satisfied?

I am a beginner and I was implementing Dining Philosopher's problem. However, I ran across an issue. In my philosopher() function, I want my other threads to wait till the right and left chopsticks are available for them to use. How should I…
0
votes
1 answer

how to initialize a unique lock that has already been declared in c++?

I created a class and I declared an array of unique locks and an array of mutexes as private variables. My question is how do I connect the two of them in the constructor of the class? header file: #include #include #include…
0
votes
0 answers

Semaphores and Thread Synchronization

I've been trying to learn about semaphores and thread synchronization by going through some classical problems like producer-consumer problem and dining philosopher's problem. While reading the solutions of the above problems implemented in C, I've…
Nimrod
  • 375
  • 1
  • 12
0
votes
1 answer

Solve Dining Philosophers Problem Using pthreads, mutex locks, and condition variables

I'm trying to implement the dining philosophers problem in C using pthreads, mutex locks, and condition variables. It needs to take a command line argument to specify how long the program should run. I have to use the sleep function to accomplish…
0
votes
2 answers

What happens when two or more philosophers check the mutex to be 1 and at the same time down the mutex and enter test function

#define N 5 /* number of philosophers */ #define LEFT (i + N−1) % N /* number of i’s left neighbor */ #define RIGHT (i + 1) % N /* number of i’s right neighbor */ #define THINKING 0 /* philosopher is thinking */ #define HUNGRY 1 /* philosopher is…