Horje
dining philosophers problem solution using semaphores in c Code Example
dining philosophers problem solution using semaphores in c
#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<semaphore.h>#include<unistd.h>sem_t room;sem_t chopstick[5];void * philosopher(void *);void eat(int);int main(){	int i,a[5];	pthread_t tid[5];		sem_init(&room,0,4);		for(i=0;i<5;i++)		sem_init(&chopstick[i],0,1);			for(i=0;i<5;i++){		a[i]=i;		pthread_create(&tid[i],NULL,philosopher,(void *)&a[i]);	}	for(i=0;i<5;i++)		pthread_join(tid[i],NULL);}void * philosopher(void * num){	int phil=*(int *)num;	sem_wait(&room);	printf("\nPhilosopher %d has entered room",phil);	sem_wait(&chopstick[phil]);	sem_wait(&chopstick[(phil+1)%5]);	eat(phil);	sleep(2);	printf("\nPhilosopher %d has finished eating",phil);	sem_post(&chopstick[(phil+1)%5]);	sem_post(&chopstick[phil]);	sem_post(&room);}void eat(int phil){	printf("\nPhilosopher %d is eating",phil);}/* BY - ANUSHKA DESHPANDE */
Source: medium.com




Whatever

Related
trapezoid midsegmnent Code Example trapezoid midsegmnent Code Example
longest word in string in c Code Example longest word in string in c Code Example
Free robux 100 Code Example Free robux 100 Code Example
ip banned message Code Example ip banned message Code Example
dpy run code Code Example dpy run code Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7