#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#define N 64
char buf[N];
sem_t get, put;
void * thread_routine(void *arg)
{
while (1)
{
sem_wait(&get);
sleep(5);
printf("%s", buf);
sem_post(&put);
}
}
int main()
{
pthread_t thread;
void *thread_result;
if (sem_init(&get, 0, 0) == -1)
{
perror("sem_init get");
exit(-1);
}
if (sem_init(&put, 0, 1) == -1)
{
perror("sem_init r");
exit(-1);
}
if (pthread_create(&thread, NULL, thread_routine, NULL) != 0)
{
fprintf(stderr, "pthread_create %s\n", strerror(errno));
exit(-1);
}
do
{
sem_wait(&put);
fgets(buf, N, stdin);
sem_post(&get);
} while (strncmp(buf, "quit", 4));
exit(0);
}
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#define N 64
char buf[N];
sem_t get, put;
void * thread_routine(void *arg)
{
while (1)
{
sem_wait(&get);
sleep(5);
printf("%s", buf);
sem_post(&put);
}
}
int main()
{
pthread_t thread;
void *thread_result;
if (sem_init(&get, 0, 0) == -1)
{
perror("sem_init get");
exit(-1);
}
if (sem_init(&put, 0, 1) == -1)
{
perror("sem_init r");
exit(-1);
}
if (pthread_create(&thread, NULL, thread_routine, NULL) != 0)
{
fprintf(stderr, "pthread_create %s\n", strerror(errno));
exit(-1);
}
do
{
sem_wait(&put);
fgets(buf, N, stdin);
sem_post(&get);
} while (strncmp(buf, "quit", 4));
exit(0);
}