- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
void main() {
ph_fork f1, f2, f3, f4, f5;
f_arr[0] = &f1;
f_arr[1] = &f2;
f_arr[2] = &f3;
f_arr[3] = &f4;
f_arr[4] = &f5;
philosopher ph1, ph2, ph3, ph4, ph5;
ph_arr[0] = &ph1;
ph_arr[1] = &ph2;
ph_arr[2] = &ph3;
ph_arr[3] = &ph4;
ph_arr[4] = &ph5;
f1.number = 1;
sem_init(&f1.is_free, 0, 1);
f2.number = 2;
sem_init(&f2.is_free, 0, 1);
f3.number = 3;
sem_init(&f3.is_free, 0, 1);
f4.number = 4;
sem_init(&f4.is_free, 0, 1);
f5.number = 5;
sem_init(&f5.is_free, 0, 1);
ph1.number = 1;
ph1.ph_fork_amount = 0;
ph1.ph_forks[0] = 1;
ph1.ph_forks[1] = 2;
ph1.times_eaten = 0;
ph1.times_thought = 0;
ph1.st = THINKING;
pthread_create(&ph1.thread, NULL, routine, (void*)&ph1);
ph2.number = 2;
ph2.ph_fork_amount = 0;
ph2.ph_forks[0] = 2;
ph2.ph_forks[1] = 3;
ph2.times_eaten = 0;
ph2.times_thought = 0;
ph2.st = THINKING;
pthread_create(&ph2.thread, NULL, routine, (void*)&ph2);
ph3.number = 3;
ph3.ph_fork_amount = 0;
ph3.ph_forks[0] = 3;
ph3.ph_forks[1] = 4;
ph3.times_eaten = 0;
ph3.times_thought = 0;
ph3.st = THINKING;
pthread_create(&ph3.thread, NULL, routine, (void*)&ph3);
ph4.number = 4;
ph4.ph_fork_amount = 0;
ph4.ph_forks[0] = 4;
ph4.ph_forks[1] = 5;
ph4.times_eaten = 0;
ph4.times_thought = 0;
ph4.st = THINKING;
pthread_create(&ph4.thread, NULL, routine, (void*)&ph4);
ph5.number = 5;
ph5.ph_fork_amount = 0;
ph5.ph_forks[0] = 5;
ph5.ph_forks[1] = 1;
ph5.times_eaten = 0;
ph5.times_thought = 0;
ph5.st = THINKING;
pthread_create(&ph5.thread, NULL, routine, (void*)&ph5);
pthread_join(ph1.thread, NULL);
pthread_join(ph2.thread, NULL);
pthread_join(ph3.thread, NULL);
pthread_join(ph4.thread, NULL);
pthread_join(ph5.thread, NULL);
sem_destroy(&f1.is_free);
sem_destroy(&f2.is_free);
sem_destroy(&f3.is_free);
sem_destroy(&f4.is_free);
sem_destroy(&f5.is_free);
printf("\nThe lunch has ended!\n--------\nRESULTS:\nPhilosopher 1 has eaten %d times and thought %d times\nPhilosopher 2 has eaten %d times and thought %d times\nPhilosopher 3 has eaten %d times and thought %d times\nPhilosopher 4 has eaten %d times and thought %d times\nPhilosopher 5 has eaten %d times and thought %d times\n", ph1.times_eaten, ph1.times_thought, ph2.times_eaten, ph2.times_thought, ph3.times_eaten, ph3.times_thought, ph4.times_eaten, ph4.times_thought, ph5.times_eaten, ph5.times_thought);
}
Сейчас просто знаете бывает много ядер у процессоров