Набросок примера работы с FIFO
This commit is contained in:
parent
b2c49c7507
commit
f51467cd80
4
code/C/FIFO/.gitignore
vendored
Normal file
4
code/C/FIFO/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
example_fifo
|
||||||
|
|
||||||
|
reader
|
||||||
|
writer
|
9
code/C/FIFO/README.md
Normal file
9
code/C/FIFO/README.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
```sh
|
||||||
|
clear; clang -std=c99 -o reader reader.c && ./reader
|
||||||
|
# или
|
||||||
|
tcc -run reader.c
|
||||||
|
```
|
||||||
|
|
||||||
|
```sh
|
||||||
|
tcc -run writer.c
|
||||||
|
```
|
67
code/C/FIFO/reader.c
Normal file
67
code/C/FIFO/reader.c
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/select.h>
|
||||||
|
#include <poll.h>
|
||||||
|
|
||||||
|
#define FIFO_NAME "example_fifo"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
unsigned int timeout_seconds = 2;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
int fd = open(FIFO_NAME, O_RDONLY | O_NONBLOCK);
|
||||||
|
if (fd < 0) {
|
||||||
|
perror("open");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct pollfd waiter = {.fd = fd, .events = POLLIN};
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
bool stop = false;
|
||||||
|
|
||||||
|
int poll_result = poll(&waiter, 1, timeout_seconds * 1000);
|
||||||
|
switch (poll_result) {
|
||||||
|
case 0:
|
||||||
|
puts("No data from fifo");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
if (waiter.revents & POLLIN) {
|
||||||
|
char buffer[BUFSIZ];
|
||||||
|
ssize_t len = read(fd, buffer, sizeof(buffer));
|
||||||
|
if (len < 0) {
|
||||||
|
perror("read");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Data: %s\n", buffer);
|
||||||
|
} else if (waiter.revents & POLLERR) {
|
||||||
|
puts("Got a POLLERR");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
} else if (waiter.revents & POLLHUP) {
|
||||||
|
stop=true; // Writer closed its end
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
perror("poll error");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stop) {break;}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (close(fd) < 0) {
|
||||||
|
perror("close");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
18
code/C/FIFO/writer.c
Normal file
18
code/C/FIFO/writer.c
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define FIFO_NAME "example_fifo"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char message[1024] = "OLOLO MESSAGE";
|
||||||
|
|
||||||
|
mkfifo(FIFO_NAME, 0666);
|
||||||
|
int fd = open(FIFO_NAME, O_WRONLY);
|
||||||
|
write(fd, message, sizeof(message));
|
||||||
|
close(fd);
|
||||||
|
// unlink(FIFO_NAME);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user