parent
75b37f6eb8
commit
1b17e2f3c4
@ -0,0 +1,49 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <semaphore.h>
|
||||
sem_t A, B, C;
|
||||
void* p1(void *arg){
|
||||
|
||||
for(int i=0;i<10;i++){
|
||||
sem_wait(&C);
|
||||
printf("A\n");
|
||||
sem_post(&A);
|
||||
}
|
||||
usleep(1000*1000);
|
||||
}
|
||||
void* p2(void *arg){
|
||||
|
||||
for(int i=0;i<10;i++){
|
||||
sem_wait(&A);
|
||||
printf("B\n");
|
||||
sem_post(&B);
|
||||
}
|
||||
usleep(1000*1000);
|
||||
}
|
||||
void* p3(void *arg){
|
||||
|
||||
for(int i=0;i<10;i++){
|
||||
sem_wait(&B);
|
||||
printf("C\n");
|
||||
sem_post(&C);
|
||||
}
|
||||
|
||||
usleep(1000*1000);
|
||||
}
|
||||
|
||||
int main()
|
||||
{ sem_init( &A, 0,0);
|
||||
sem_init( &B, 0,0);
|
||||
sem_init( &C, 0,1);
|
||||
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