#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
sem_t sem1,sem2,sem3;
void* task1(void* arg){
int i=0;
while(1){
sem_wait(&sem1);
printf("A");
fflush(NULL);
i++;
sem_post(&sem2);
if(i>9){
pthread_exit(NULL);
}
}
}
void* task2(void* arg){
int j=0;
while(1){
sem_wait(&sem2);
printf("B");
fflush(NULL);
j++;
sem_post(&sem3);
if(j>9){
pthread_exit(NULL);
}
}
}
void* task3(void* arg){
int k=0;
while(1){
sem_wait(&sem3);
printf("C");
fflush(NULL);
k++;
sem_post(&sem1);
if(k>9){
printf("\n");
pthread_exit(NULL);
}
}
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2,tid3;
sem_init(&sem3,0,0);
sem_init(&sem2,0,0);
sem_init(&sem1,0,1);
pthread_create(&tid1,NULL,task1,NULL);
pthread_create(&tid2,NULL,task2,NULL);
pthread_create(&tid3,NULL,task3,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
return 0;
}