Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice06 / ex4.c
blob: c499cb3a3855ab8868bf9edde05bc0df0882497a (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
#include <unistd.h> // pipe
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char* argv[]) {
	int filedes[2];
	if (pipe(filedes) < 0) {
		perror("pipe");
		return 1;
	}

	pid_t fpid = fork();
	if (fpid == 0) {
		close(filedes[1]);
		char buf[1024];
		int count = 2;
		while (count < argc) {
			read(filedes[0], buf, 1024);
			if (buf[0] != '\0') {
				printf("%s\n", buf);
				count++;
				// TODO: изчистване на buf
			}
		}
	}
	else {
		close(filedes[0]);
		for (int i = 1; i < argc; i++) {
			write(filedes[1], argv[i], strlen(argv[i]));
			sleep(1);
		}
		wait(NULL);
	}
}