summaryrefslogtreecommitdiffstats
path: root/02_exercise/beispiele/pipe_example/pipe.c
blob: 422e4d57d0d6595a5d5879b41d16431b9f7dc49c (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#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;
}