From 44d8d10d354003c017e5cec4bb5509bd682e15af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Thu, 10 Dec 2015 12:31:20 +0100 Subject: [PATCH] Exampel code for pipe --- test/iomux_pipe_close.c | 80 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 test/iomux_pipe_close.c diff --git a/test/iomux_pipe_close.c b/test/iomux_pipe_close.c new file mode 100644 index 0000000..11590e2 --- /dev/null +++ b/test/iomux_pipe_close.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include +#include "ucore/iomux.h" + + + +void in_cb(struct IOMux *mux, struct IOMuxFD *fd, unsigned int event) +{ + + char buf[16]; + ssize_t rc; + + rc = read(fd->fd, buf, sizeof buf); + printf("Read returned %ld\n", (long)rc); + if (rc <= 0) { + if (rc == -1 && errno == EAGAIN) { + return; + } else if (rc == -1) { + perror("read"); + } + return; + } + iomux_unregister_fd(mux, fd); + close(fd->fd); + +} + +void out_cb(struct IOMux *mux, struct IOMuxFD *fd, unsigned int event) +{ + char str[] = "hello"; + ssize_t rc; + + rc = write(fd->fd, str, strlen(str)); + printf("write returned %ld\n", (long)rc); + if (rc <= 0) { + if (rc == -1 && errno == EAGAIN) { + return; + } else { + perror("write"); + } + iomux_unregister_fd(mux, fd); + } +} + + +int main(int argc, char *argv[]) +{ + int pipes[2]; + pipe(pipes); + uc_set_nonblocking(pipes[0]); + uc_set_nonblocking(pipes[1]); + + struct IOMuxFD rfd= { + .fd = pipes[0], + .what = MUX_EV_READ, + .callback = in_cb, + }; + + struct IOMuxFD wfd= { + .fd = pipes[1], + .what = MUX_EV_WRITE, + .callback = out_cb, + }; + + signal(SIGPIPE, SIG_IGN); + + struct IOMux *mux = iomux_create(IOMUX_TYPE_SELECT); + iomux_register_fd(mux, &rfd); + iomux_register_fd(mux, &wfd); + + iomux_run(mux); + + return 0; +} +