From 369a723ba50fc9520b30f4d18cdb347f8b9496ee Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 15 Nov 2023 11:58:16 +0200 Subject: [SP/P6] Solved exercises --- Year 3/System programming/Practicum6/ex5.c | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Year 3/System programming/Practicum6/ex5.c (limited to 'Year 3/System programming/Practicum6/ex5.c') diff --git a/Year 3/System programming/Practicum6/ex5.c b/Year 3/System programming/Practicum6/ex5.c new file mode 100644 index 0000000..903732d --- /dev/null +++ b/Year 3/System programming/Practicum6/ex5.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +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; +} -- cgit v1.3.1-26-gb76a