From 04576dc2a3f761eb041b808b56f13a58052e7655 Mon Sep 17 00:00:00 2001 From: Stefan Zabka Date: Sun, 24 May 2020 12:19:52 +0200 Subject: Moved back to 02_exercise --- 02_exercise/beispiele/pipe_example/pipe.c | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 02_exercise/beispiele/pipe_example/pipe.c (limited to '02_exercise/beispiele/pipe_example/pipe.c') diff --git a/02_exercise/beispiele/pipe_example/pipe.c b/02_exercise/beispiele/pipe_example/pipe.c new file mode 100644 index 0000000..422e4d5 --- /dev/null +++ b/02_exercise/beispiele/pipe_example/pipe.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include + +#define BUF_SIZE 32 + +int pipe_fds[2]; + +void term(const char *msg) { + if (msg) { + fprintf(stderr, "Error: %s\n", msg); + fflush(stderr); + } + + int close_fail = 0; + int ret = close(pipe_fds[0]); + + if (ret < 0) { + fprintf(stderr, "Error: Couldn't close reading end of pipe\n"); + fflush(stderr); + close_fail = 1; + } + + ret = close(pipe_fds[1]); + + if (ret < 0) { + fprintf(stderr, "Error: Couldn't close writing end of pipe\n"); + fflush(stderr); + close_fail = 1; + } + + exit((msg || close_fail) ? EXIT_FAILURE : EXIT_SUCCESS); +} + +int main(void) { + int ret = pipe(pipe_fds); + + if (ret < 0) + term("Couldn't create pipe"); + + char out_buf[] = "hello"; + ret = write(pipe_fds[1], out_buf, strlen(out_buf) + 1); + + if (ret < 0) + term("Couldn't write to pipe"); + + printf("send msg: %s\n", out_buf); + + char in_buf[BUF_SIZE]; + memset(in_buf, 0, BUF_SIZE); + + ret = read(pipe_fds[0], in_buf, BUF_SIZE - 1); + + if (ret < 0) + term("Couldn't read from pipe"); + + printf("recv msg: %s\n", in_buf); + return 0; +} -- cgit v1.2.3-54-g00ecf