nashealing.blogg.se

Dining philosophers pthread c using struct
Dining philosophers pthread c using struct














Int eat_time = ( rand() % WORK_TIME) + 1 Int think_time = ( rand() % WORK_TIME) + 1 Srand( time( NULL) + philosopher_number) Int philosopher_number = *( int *) param Philosophers think and eat in this function Runs infinitely for all philosophers in different threads release the lock, so that other threads can use shared data need to already have the lock before calling this Wait_temp = (- 1) * get_posix_clock_time()

dining philosophers pthread c using struct

Printf( "Philosopher %d is hungry \n ", philosopher_number) Void pickup_chopsticks( int philosopher_number)ĭiningPhilosophers.

Dining philosophers pthread c using struct for free#

lock) Ītomic call, the philosopher becomes hungryĬhecks for free chopsticks, if not free then wait till signal Pthread_mutex_unlock(&diningPhilosophers. Printf( "#Eating count = %d \n\n ", meal_no) Ĭheck_chopsticks((philosopher_number + NUMBER_OF_PHILOSOPHERS - 1) % NUMBER_OF_PHILOSOPHERS) Ĭheck_chopsticks((philosopher_number + 1) % NUMBER_OF_PHILOSOPHERS)

dining philosophers pthread c using struct

Printf( "Philosopher %d finish eating \n ", philosopher_number) Void return_chopsticks( int philosopher_number)

dining philosophers pthread c using struct

condition) Ītomic call, change state of philosopher to thinkingĬall the neighbours to check for free chopsticks this does not unlock the mutex, need to unlock from parent function wait time is equal to start time - time at this momentĭiningPhilosophers. Printf( "Philosopher %d looking for chopsticks \n ", i) Printf( "Philosopher %d giving way to %d to eat to AVOID starvation \n ", i, (i + NUMBER_OF_PHILOSOPHERS - 1) % NUMBER_OF_PHILOSOPHERS) Ĭheck if neighbours aren't eating and philosopher isn't starvingĬhange state to eating and signal the condition Printf( "Philosopher %d giving way to %d to eat to AVOID starvation \n ", i, (i + 1) % NUMBER_OF_PHILOSOPHERS) * we will exit from a child thread without caring about memory leaks */įor ( int k = 0 k cur & n1 > MAX_WAIT_TIME) Struct TheDiningPhilosophers diningPhilosophers














Dining philosophers pthread c using struct