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

int main(int argc, char* argv[]) {
	int pf[2];
	pipe(pf);
	int f = fork();
	if (f == 0) { // child
		if (close(pf[0]) < 0) {
			perror("close");
			return 1;
		}
		dup2(pf[1], 1);
		if (close(pf[1]) < 0) {
			perror("close");
			return 2;
		}

		execlp(argv[1], argv[1], (char*)NULL);
	}
	else {
		if (close(pf[1]) < 0) {
			perror("close");
			return 3;
		}
		dup2(pf[0], 0);
		if (close(pf[0]) < 0) {
			perror("close");
			return 4;
		}

		int status;
		wait(&status);

		execlp(argv[2], argv[2], (char*)NULL);
	}
	return 0;
}