philosophers
所属分类:数值算法/人工智能
开发工具:C
文件大小:11KB
下载次数:0
上传日期:2023-01-24 16:08:00
上 传 者:
sh-1993
说明: 用餐哲学家问题的实现。一个经典的计算机科学项目。以...的概念为中心...
(An implementation of the Dining Philosophers Problem. A classic computer science project. Centered on the concept of concurrency. Each thread is to perform its tasks while avoiding data races.)
文件列表:
philo (0, 2023-01-25)
philo\Makefile (1648, 2023-01-25)
philo\inc (0, 2023-01-25)
philo\inc\philo.h (3218, 2023-01-25)
philo\src (0, 2023-01-25)
philo\src\actions.c (3370, 2023-01-25)
philo\src\conditionals.c (2060, 2023-01-25)
philo\src\fork_actions.c (2435, 2023-01-25)
philo\src\get_time.c (1055, 2023-01-25)
philo\src\main.c (1383, 2023-01-25)
philo\src\parse_args.c (2554, 2023-01-25)
philo\src\philosophers.c (2982, 2023-01-25)
philo\src\philosophers_cont.c (1459, 2023-01-25)
philo\src\routines.c (2813, 2023-01-25)
philo\src\sleep.c (1335, 2023-01-25)
philo\src\sole_philo.c (1343, 2023-01-25)
# philosophers
Philosophers is a 42 Wolfsburg parallel computing project. An implementation of the Dining Philosophers Problem. A classic computer science project.
There are two key concepts to understand for this project: threads and mutexes. The prior is essential to deploy concurrent routines, while the latter is crucial to avoid data races.
## Rules
- A number of philosophers sit at a round table where a large bowl of spaghetti lies in the center.
- Each philosopher has a routine of eating, sleeping and thinking that repeats.
- There are as many forks as there are philosophers on the table.
- Each philosopher needs the two closest forks to eat.
- Philosophers can’t speak with each other.
- Every philosopher needs to eat and should never starve.
- Unless the conditions do not allow it. No philosophers should die.
### Available parameters:
- number_of_philosophers: The number of philosophers and also the number of forks.
- time_to_die (in milliseconds): If a philosopher didn’t start eating time_to_die milliseconds since the beginning of their last meal or the beginning of the simulation, they die.
- time_to_eat (in milliseconds): The time it takes for a philosopher to eat. During that time, they will need to hold two forks.
- time_to_sleep (in milliseconds): The time a philosopher will spend sleeping.
- number_of_times_each_philosopher_must_eat (optional argument): If all philosophers have eaten at least number_of_times_each_philosopher_must_eat times, the simulation stops. If not specified, the simulation stops when a philosopher dies.
In case of invalid arguments, the program must display an appropriate error message.
Errors include for example: some arguments are not integers, some arguments are
bigger than an integer and/or zero or negative values are present in places it does not make sense.
# Usage
Use `make` to compile the program.
Add the desired parameters:
```
./philo
```
# Example
Input:
```
./philo 5 800 200 200 7
```
Output:
```
0 ms: 1 has taken a fork
0 ms: 1 has taken a fork
0 ms: 1 is eating
0 ms: 3 has taken a fork
0 ms: 3 has taken a fork
0 ms: 3 is eating
0 ms: 5 has taken a fork
200 ms: 3 is sleeping
200 ms: 1 is sleeping
200 ms: 4 has taken a fork
200 ms: 2 has taken a fork
200 ms: 2 has taken a fork
200 ms: 2 is eating
200 ms: 5 has taken a fork
200 ms: 5 is eating
400 ms: 3 is thinking
400 ms: 4 has taken a fork
400 ms: 4 is eating
400 ms: 2 is sleeping
400 ms: 1 is thinking
...
3700 ms: 5 is eating
3700 ms: 2 is thinking
3700 ms: 1 is sleeping
3800 ms: 3 is sleeping
3800 ms: 4 is thinking
3800 ms: 2 has taken a fork
3800 ms: 2 has taken a fork
3800 ms: 2 is eating
3900 ms: 5 is sleeping
3900 ms: 1 is thinking
4000 ms: 3 is thinking
4000 ms: 2 is sleeping
4100 ms: 5 is thinking
4200 ms: 2 is thinking
```
Input:
```
./philo 4 310 200 100
```
Output:
```
0 ms: 1 has taken a fork
0 ms: 1 has taken a fork
0 ms: 1 is eating
0 ms: 3 has taken a fork
0 ms: 3 has taken a fork
0 ms: 3 is eating
200 ms: 3 is sleeping
200 ms: 1 is sleeping
200 ms: 2 has taken a fork
200 ms: 2 has taken a fork
200 ms: 2 is eating
200 ms: 4 has taken a fork
200 ms: 4 has taken a fork
200 ms: 4 is eating
300 ms: 3 is thinking
300 ms: 1 is thinking
310 ms: 1 died
```
近期下载者:
相关文件:
收藏者: