-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsem_xfer.c
More file actions
60 lines (52 loc) · 1.26 KB
/
psem_xfer.c
File metadata and controls
60 lines (52 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Data is transfered from stdin to stdout via two threads and a global buffer.
* stdin -> Thread 1 -> Global Buffer -> Thread 2 -> stdout
* Access to the global buffer is protected using a posix semaphore.
**/
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <semaphore.h>
#include <signal.h>
#define BUFSZ 512
static char buf[BUFSZ];
static ssize_t n;
static sem_t sem;
static void exit_err(char *reason) {
perror(reason);
exit(EXIT_FAILURE);
}
static void *reader(void *arg) {
puts("Reader running...");
while (1) {
if ((n = read(STDIN_FILENO, buf, BUFSZ)) == -1)
exit_err("read");
sem_post(&sem);
}
return (void*)0;
}
static void *writer(void *arg) {
puts("Writer running...");
while (1) {
sem_wait(&sem);
if (write(STDOUT_FILENO, buf, n) == -1)
exit_err("write");
}
return (void*)0;
}
int main(int argc, char **argv) {
pthread_t treader, twriter;
void *ret;
if (sem_init(&sem, 0, 0) == -1)
exit_err("sem_init");
if (pthread_create(&treader, NULL, reader, NULL) != 0)
exit_err("pthread create 1");
if (pthread_create(&twriter, NULL, writer, NULL) != 0)
exit_err("pthread create 2");
pthread_join(treader, &ret);
pthread_join(twriter, &ret);
exit(EXIT_SUCCESS);
}