44 lines
750 B
C
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;
|
|
} |