void *read_data();
char buf[64]={0};
sem_t sem1;
sem_t sem2;
int main()
{
pthread_t thread1;
pthread_t thread2;
sem_init(&sem1,0,0);
sem_init(&sem2,0,1);
if(0!=pthread_create(&thread1,NULL,write_data,NULL))
{
perror("pthread_create thread1");
exit(-1);
}
if(0!=pthread_create(&thread2,NULL,read_data,NULL))
{
perror ("pthread_create thread2");
exit(-1);
}
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
return 0;
}
void *write_data()
{
while(1)
{
sem_wait(&sem2);
fgets(buf,64,stdin);
sem_post(&sem1);
}
}
void *read_data()
{
while(1)
{
sem_wait(&sem1);
printf("%s",buf);
sem_post(&sem2);
}
}