Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <string.h>
- int main(void)
- {
- int pid,
- c_fd[2],
- p_fd[2];
- char *str;
- if (pipe(c_fd) == -1 || pipe(p_fd) == -1)
- {
- fprintf(stderr, "Pipe failed.\n");
- exit(1);
- }
- pid = fork();
- if (pid == -1)
- {
- fprintf(stderr, "Fork failed.\n");
- exit(1);
- }
- // child process
- else if (pid == 0)
- {
- // redirect stdin to c_fd output and close c_fd input
- dup2(0, c_fd[0]);
- close(c_fd[1]);
- // redirect stdout to p_fd input and close p_fd output
- dup2(1, p_fd[1]);
- close(p_fd[0]);
- while (read(c_fd[0], &str, 11) != 0)
- if (strcmp(str, "Guten Tag!\n") == 0)
- printf("Oh, hallo!\n");
- exit(0);
- }
- // parent process
- else
- {
- // redirect stdout to c_fd input and close c_fd output
- dup2(1, c_fd[1]);
- close(c_fd[0]);
- // redirect stdin to p_fd output and close p_fd input
- dup2(0, p_fd[0]);
- close(p_fd[1]);
- printf("Guten Tag!\n");
- wait(&pid);
- printf("Kindprozess hat erfolgreich geantwortet!\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement