实现信号同步功能
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<pthread.h>
#include<semaphore.h>
sem_t s1,s2;
void* run(void* arg)
{
while(1)
{
sem_wait(&s2);
printf("子线程\n");
sleep(1);
sem_post(&s1);
}
}
int main(int argc, const char *argv[])
{
pthread_t id;
pthread_create(&id,NULL,run,NULL);
pthread_detach(id);
sem_init(&s1,0,1);
sem_init(&s2,0,0);
while(1)
{
sem_wait(&s1);
printf("主线程\n");
sleep(1);
sem_post(&s2);
}
return 0;
}