Skip to content

Latest commit

 

History

18 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Philosophers

This project is about learning the basics of process threading using mutexes and semaphores. The dining philosophers problem, formulated in 1965 by Edsger Dijkstra, can be summed up as following:

  • One or more philosophers sit at a round table, and each has a plate of spaghetti
  • On the table, there are as many forks as there are philosophers
  • The philosophers can either eat, think or sleep
  • In order to eat, each philosopher needs to use 2 forks
  • When a philosopher has finished eating, they put their forks back on the table and start sleeping
  • The forks are then available to be used by other philosophers
  • The simulation stops when a philosopher dies of starvation.

The number of philosophers, time to die, time to eat, time to sleep (in ms) and optional number of times each philospher must eat are the arguments to the program.

philo1

Usage

Warning: Makefile is configured for Linux use only.

  • Compile with make
  • Then run with ./philo and for example 4 800 300 200. In this case the program runs indefinitely
  • Running with for example 2 401 200 200 will work as well
  • With 2 400 200 200, a philosopher will die and the simulation stops

Here is how running ./philo 4 800 300 200 looks like in the terminal:

philo2

And thanks to nafuka11, here is a visualization of what's actually happening:

philo3

Restrictions

This project is written in C and 42 norm compliant:

  • All variables have to be declared and aligned at the top of each function
  • One function cannot have more than 25 lines
  • It is considering cheating to use functions that are forbidden in the subject

About

Introduction to process threading in C

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages