Skip to content

Philosophers dinner problem in C. Introduction to threads, mutexes, processes, and semaphores

Notifications You must be signed in to change notification settings

Kuninoto/42_Philosophers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Philosophers (42Porto - 42Cursus) Linux Logo  C Logo 

Grade: 100/100

Installing and running the project:

1- Clone this repository

git clone https://github.com/Kuninoto/42_Philosophers

3- Navigate to lvl_3_philosophers and run make

cd 42_Philosophers/lvl_3_philosophers
make

4- make clean so that you don't keep those object files that you won't need anymore

make clean

5- Run your new dining philosopher simulation program!

./philo <nbr_of_philos> <time_to_die> <time_to_eat> <time_to_sleep> [nbr_of_times_each_philo_must_eat]

Mandatory: Solving the dining philosophers problem using threads and mutexes
Bonus: Solving the same problem but this time with processes and semaphores

Side note/warning for 42 students visiting:

I did the bonus part but I did NOT delivered it, it isn't in its final form and neither I tested it sufficiently to guarantee that it's fully working.

Makefile Available Targets:

make or make all - compiles philo or philo_bonus (depending on the folder you're in)
make clean - wipes all object files
make fclean - deletes executable and all object files
make re - fclean + all

Useful links:

General thread understanding:
Introduction to threads (pthreads) | C programming
How to create and join threads in C (pthreads)
Short introduction to threads (pthreads)
Multithreading in C
What are race conditions
How to create threads in a loop (pthread_create)

Threads vs. Processes:
Difference between process and thread
Difference between processes and threads

Mutex:
What is a mutex in C? (pthread_mutex)
Mutex lock for Linux Thread Synchronization

Project Guide:
https://github.com/TommyJD93/Philosophers

General semaphore understanding:
Semaphores in Process Synchronization
How to use POSIX semaphores in C language
Introduction to semaphores in C
Semaphores
What is a semaphore? How do they work? (Example in C)
Inter Process Communication - Semaphores
Can Semaphore Be Initialized To Negative?
When to call sem_unlink()?

Mutex vs. Semaphore:
Mutex vs Semaphore
Semaphore Vs. Mutex - A Clear Understanding

Mutex vs. Semaphore Rule of Thumb:

Mutex Semaphore
Behaves like a lock Behaves like a limited inventory
pthread_mutex_lock() sem_wait(), decrements the number of available things, if sem == 0, waits.
pthread_mutex_unlock() sem_post(), increments back the number of available things

fork() IBM documentation:
https://www.ibm.com/docs/en/zos/2.1.0?topic=functions-fork-create-new-process#rtfor

waitpid() IBM documentation:
https://www.ibm.com/docs/en/zos/2.1.0?topic=functions-waitpid-wait-specific-child-process-end

sem_open() IBM documentation: https://www.ibm.com/docs/en/i/7.1?topic=ssw_ibm_i_71/apis/ipcsemo.html

Inter Process Communication
Shared memory

Disclaimer

At 42School, almost every project must be written in accordance to the Norm, the schools' coding standard. As a result, the implementation of certain parts may appear strange and for sure had room for improvement.


Made by Nuno Carvalho (Kuninoto) | kuninoto@proton.me