commit 59f3c21dab258363031fcd995d6801eb0ea75666
parent af093765d73c4ad9f0c82f593316c6de8f5b5525
Author: Vetle Haflan <vetle@haflan.dev>
Date: Sun, 8 Mar 2020 19:41:25 +0100
Add poll() example (with fifo)
Diffstat:
2 files changed, 118 insertions(+), 0 deletions(-)
diff --git a/c/linux_system/02-io/fifo_poll.c b/c/linux_system/02-io/fifo_poll.c
@@ -0,0 +1,63 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <stdbool.h>
+#include <unistd.h>
+
+#define MAX_BUF 1024
+
+// Example use of poll()
+// Reads from and writes to same fifo, using poll() to watch POLLIN and POLLOUT
+// events and act when the fd is ready
+
+char * myfifo = "./this.fifo";
+
+int main()
+{
+ int fd;
+ /* create the FIFO (named pipe) */
+ mkfifo(myfifo, 0666);
+ /* open in nonblock to avoid waiting */
+ fd = open(myfifo, O_RDWR);
+ if (fd == -1) {
+ printf("main: %d\n", errno);
+ return 1;
+ }
+
+ // Variables to write return values and read data to
+ int len, ret;
+ char input[MAX_BUF], output[MAX_BUF];
+
+ // Define array of pfd(s). Only one file descriptor in this case
+ struct pollfd pfds[1];
+ pfds[0].fd = fd;
+ pfds[0].events = POLLIN | POLLOUT; // Watch poll in and out events
+ while (strcmp(input, "exit\n")) {
+ ret = poll(pfds, 1, 1); // 1 fd, 1 second timeout
+ if (ret == -1) {
+ perror("poll");
+ return 1;
+ }
+ if (!ret) {
+ continue;
+ }
+ if (pfds[0].revents & POLLIN) {
+ len = read(fd, output, MAX_BUF);
+ printf("Received: %d bytes\n", len);
+ output[len] = '\0'; // ignore garbage from previous read
+ printf("Received: %s", output);
+ continue;
+ }
+ if (pfds[0].revents & POLLOUT) {
+ printf("> ");
+ fgets(input, MAX_BUF, stdin);
+ write(fd, input, strlen(input));
+ }
+ }
+ close(fd);
+
+ return 0;
+}
diff --git a/c/linux_system/fifo_thread.c b/c/linux_system/fifo_thread.c
@@ -0,0 +1,55 @@
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <pthread.h>
+
+#define MAX_BUF 1024
+
+char * myfifo = "./this.fifo";
+
+void * read_fifo() {
+ // open, read, and display the message from the FIFO
+ int fd = open(myfifo, O_RDONLY);
+ if (fd == -1) {
+ printf("read_fifo: %d", errno);
+ }
+ char buf[MAX_BUF];
+ while (strcmp(buf, "exit")) {
+ int len = read(fd, buf, MAX_BUF);
+ printf("Received: %d bytes\n", len);
+ printf("Received: %s\n", buf);
+ if (!strcmp(buf, "exit")) {
+ printf("Should exit");
+ }
+ }
+ close(fd);
+}
+
+int main()
+{
+ int fd;
+ /* create the FIFO (named pipe) */
+ mkfifo(myfifo, 0666);
+ /* open in nonblock to avoid waiting */
+ pthread_t read_thread;
+ pthread_create(&read_thread, NULL, read_fifo, (void *) NULL);
+ fd = open(myfifo, O_WRONLY | O_ASYNC);
+ if (fd == -1) {
+ printf("main: %d\n", errno);
+ return 1;
+ }
+ /* write to the FIFO */
+ char input[MAX_BUF];
+ while (strcmp(input, "exit")) {
+ fgets(input, MAX_BUF, stdin);
+ write(fd, input, strlen(input));
+ }
+ pthread_join(read_thread, NULL);
+ close(fd);
+
+ return 0;
+}