You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
952 B
50 lines
952 B
|
|
#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");
|
|
}
|