AAE-CNAS-Labs/Topic-6/task_6_3.c
2021-04-03 20:08:15 +02:00

44 lines
750 B
C

//
// Written for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
int pipefd[2];
if (pipe(pipefd) < 0) {
fprintf(stderr, "Something went wrong!\n");
return -1;
}
pid_t pid;
pid = fork();
if (pid == -1) { // If something went wrong.
perror("Fork: ");
exit(EXIT_FAILURE);
}
if (pid == 0) { // If this is a child process.
dup2(pipefd[1], STDOUT_FILENO);
printf("printf() from a child\n");
close(pipefd[1]);
exit(EXIT_SUCCESS);
}
char buff[100] = {};
while (read(pipefd[0], &buff, sizeof(buff)) > 0) {
printf("PARENT: %s", buff);
}
int status;
wait(&status);
return 0;
}