Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Process Synchronization/dining-philosophers.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ void *thinkEatRepeat(int i)

printf("%d eating...\n",i+1 );
sleep(1);

pthread_mutex_unlock(&chopsticks[i]);
pthread_mutex_unlock(&chopsticks[(i+1)%n]);


if(i!=n){
pthread_mutex_unlock(&chopsticks[i]);
pthread_mutex_unlock(&chopsticks[(i+1)%n]);
}
else{ //It handles the case when all philosphers pick the chopstick to their right which will result in a deadlock
pthread_mutex_unlock(&chopsticks[i]);
pthread_mutex_unlock(&chopsticks[(i-1)%n]);
}
printf("\t%d Finished eating\n", i+1);

return NULL;
Expand All @@ -48,4 +53,4 @@ int main()
{
pthread_mutex_destroy(&chopsticks[i]);
}*/
}
}