信号量打印ABC
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <25061head.h>
sem_t sem[1];
void *callback(void *arg)
{while(1){sem_wait(&sem[0]);printf("A\n");sleep(1);sem_post(&sem[1]);}pthread_exit(NULL);
}
void *callback2(void *arg)
{while(1){sem_wait(&sem[1]);printf("B\n");sleep(1);sem_post(&sem[2]);}pthread_exit(NULL);
}
void *callback3(void *arg)
{while(1){sem_wait(&sem[2]);printf("C\n");sleep(1);sem_post(&sem[0]);}pthread_exit(NULL);
}int main(int argc, const char *argv[])
{sem_init(&sem[0],0,1);sem_init(&sem[1],0,0);sem_init(&sem[2],0,0);pthread_t tid,tid2,tid3;if(pthread_create(&tid,NULL,callback,NULL)!=0){printf("pthread_create error");return -1;}if(pthread_create(&tid2,NULL,callback2,NULL)!=0){printf("pthread_create error");return -1;}if(pthread_create(&tid3,NULL,callback3,NULL)!=0){printf("pthread_create error");return -1;}pthread_join(tid,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);sem_destroy(&sem[0]);sem_destroy(&sem[1]);sem_destroy(&sem[2]);return 0;
}