hehe...actually, some part of the main program was given to me, and i wrote the Philo function, and all other functions related to it.
Now, I can get some of the philosophers to eat, but then it deadlocks. I want to solve the deadlock, but not sure how. I wanna try to force preemption of ressources: i.e, if one philosopher holds 1 chopstick, but can't get the other one, then he has to release the one he already holds...
I guess this has to be done in the get_forks function:
void get_forks(int ph_id)
{
int left=0,right=0;
while(!left)
{
sem_wait(-1,"mutex");
left=Request(ph_id-1,ph_id);
sem_signal(-1,"mutex");
if (!left)
sleep(1);
}
sem_wait(ph_id-1,"chopstick");
while(!right)
{
sem_wait(-1,"mutex");
right=Request((ph_id)%N_PHIL,ph_id);
sem_signal(-1,"mutex");
if (!right)
{
sleep(1);
}
}
sem_wait((ph_id)%N_PHIL,"chopstick");
}
Any ideas??