AAE-CNAS-Labs/Topic-5-Pipes/task_4_2.c

43 lines
782 B
C

//
// Written for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.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;
}
int in = 42;
int out = 0;
int status;
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.
read(pipefd[0], &out, sizeof(out));
printf("%d\n", out);
close(pipefd[0]);
exit(EXIT_SUCCESS);
}
write(pipefd[1], &in, sizeof(in));
wait(&status);
close(pipefd[1]);
return 0;
}