parent
a4497b8d0c
commit
1bb2b5d269
@ -0,0 +1,52 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <semaphore.h>
|
||||
sem_t count1,count2,count3;
|
||||
void* p1(void *arg){
|
||||
for(int i=0;i<30;i++){
|
||||
sem_wait(&count1);
|
||||
printf("A\n");
|
||||
if(i % 3 == 2){
|
||||
for(int i = 0;i < 3;i ++)
|
||||
sem_post(&count2);
|
||||
}
|
||||
}
|
||||
}
|
||||
void* p2(void *arg){
|
||||
for(int i=0;i<30;i++){
|
||||
sem_wait(&count2);
|
||||
printf("B\n");
|
||||
if(i % 3 == 2){
|
||||
for(int i = 0;i < 3;i ++)
|
||||
sem_post(&count3);
|
||||
}
|
||||
}
|
||||
}
|
||||
void* p3(void *arg){
|
||||
for(int i=0;i<30;i++){
|
||||
sem_wait(&count3);
|
||||
printf("C\n");
|
||||
if(i % 3 == 2){
|
||||
for(int i = 0;i < 3;i ++)
|
||||
sem_post(&count1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
sem_init(&count1, 0 , 3);
|
||||
sem_init(&count2, 0 , 0);
|
||||
sem_init(&count3, 0 , 0);
|
||||
pthread_t tid[3];
|
||||
pthread_create(&tid[0], NULL, p1, NULL);
|
||||
pthread_create(&tid[1], NULL, p2, NULL);
|
||||
pthread_create(&tid[2], NULL, p3, NULL);
|
||||
for(int i = 0; i < 3; i++)
|
||||
pthread_join(tid[i], NULL);
|
||||
printf("main is over\n");
|
||||
}
|
Loading…
Reference in new issue