This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 263
/
mkfifo.c
114 lines (99 loc) · 3.02 KB
/
mkfifo.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#define FIFO_PATH "fifo123"
int main(int argc, char** argv) {
int fd;
char buffer[1024];
if (mkfifo(FIFO_PATH, S_IRWXU) < 0) {
perror("mkfifo error");
return 1;
}
pid_t pid = fork();
if (pid < 0) {
perror("fork error");
return 1;
} else if (pid == 0) {
/* client */
fd = open(FIFO_PATH, O_NONBLOCK | O_RDONLY);
if (fd < 0) {
perror("[child] open error");
return 1;
}
/* note that Linux guarantees either no read message or the complete message on FIFO since
* message size is less than PIPE_BUF; see man pipe(7) */
ssize_t bytes = 0;
while (bytes <= 0) {
errno = 0;
bytes = read(fd, &buffer, sizeof(buffer));
if (bytes < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
perror("[child] read error");
return 1;
}
sched_yield();
}
buffer[sizeof(buffer) - 1] = '\0';
if (bytes < sizeof(buffer))
buffer[bytes] = '\0';
if (close(fd) < 0) {
perror("[child] close error");
return 1;
}
printf("read on FIFO: %s\n", buffer);
} else {
/* server */
fd = -1;
while (fd < 0) {
/* wait until client is ready for read */
errno = 0;
fd = open(FIFO_PATH, O_NONBLOCK | O_WRONLY);
if (fd < 0 && errno != ENXIO) {
perror("[parent] open error");
return 1;
}
sched_yield();
}
/* note that Linux guarantees sending the complete message on FIFO since message size is
* less than PIPE_BUF and there are no signals possible in this test; see man pipe(7) */
snprintf(buffer, sizeof(buffer), "Hello from write end of FIFO!");
if (write(fd, &buffer, strlen(buffer) + 1) < 0) {
perror("[parent] write error");
return 1;
}
if (close(fd) < 0) {
perror("[parent] close error");
return 1;
}
pid = wait(NULL); /* wait for child termination, just for sanity */
if (pid < 0) {
perror("[parent] wait error");
return 1;
}
if (unlink(FIFO_PATH) < 0) {
perror("[parent] unlink error");
return 1;
}
/* Check if we can create a normal file with the same name. */
fd = open(FIFO_PATH, O_CREAT | O_TRUNC, 0600);
if (fd < 0) {
perror("[parent] open error");
return 1;
}
if (close(fd) < 0) {
perror("[parent] close error");
return 1;
}
if (unlink(FIFO_PATH) < 0) {
perror("[parent] unlink error");
return 1;
}
}
return 0;
}