-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe-reader.c
More file actions
37 lines (33 loc) · 892 Bytes
/
pipe-reader.c
File metadata and controls
37 lines (33 loc) · 892 Bytes
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
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#define MAX_BUF 1024
int
main()
{
int i = 2;
int fd;
int timeout = 10 * 1000 * 1000;
char *myfifo = "/tmp/myfifo";
struct stat sb;
/* open, read, and display the message from the FIFO */
if (stat(myfifo, &sb) == -1) {
perror("stat");
return -1;
}
while (timeout) {
fd = open(myfifo, O_RDONLY);
char buf[MAX_BUF];
if (read(fd, buf, MAX_BUF) > -1) {
if (strlen(buf) > 0) {
printf("Received: %s\n", buf);
close(fd);
}
}
usleep(500);
timeout -= 500;
}
return 0;
}