38 lines
715 B
Plaintext
38 lines
715 B
Plaintext
const int N = 5;
|
|
binarysem forks[N];
|
|
binarysem print_sem;
|
|
semaphore s;
|
|
|
|
void phil(int id)
|
|
{
|
|
int i;
|
|
for(i = 0; i < 1000; i++){
|
|
//wait(s); dzieki temu sem nie bedzie deadlocka inne rozwiazanie w zad dom
|
|
wait(print_sem);
|
|
cout << "Jestem filozofem o numerze "<<id<<" i mysle" <<endl;
|
|
signal(print_sem);
|
|
|
|
wait(forks[id]);
|
|
wait(forks[(id+1)%N]);
|
|
|
|
wait(print_sem);
|
|
cout << "Jestem filozofem o numerze " <<id<<" i jem po raz " <<i+1<<endl;
|
|
signal(print_sem);
|
|
|
|
signal(forks[id]);
|
|
signal(forks[(id+1)%N]);
|
|
//signal(s);
|
|
}
|
|
}
|
|
|
|
main(){
|
|
int i;
|
|
for(i=0; i<N; i++)
|
|
initialsem(forks[i], 1);
|
|
initialsem(print_sem, 1);
|
|
//initialsem(s, 4);
|
|
cobegin{
|
|
phil(0); phil(1); phil(2); phil(3); phil(4);
|
|
}
|
|
}
|