Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/wait.h>
- /* Beachten Sie den geaenderten Prototypen von main()! Referenz: man 2 execve */
- /*dup2 statt dudup2 statt dupp, Fehlerabfrage, & abfangen was passiert, wenn man gar keine argumente angibt*/
- int main(int argc, char *argv[], char *envp[])
- {
- int pid1, pid2;
- int status1, status2;
- int pipefds[2];
- if (argc != 3)
- {
- printf("Falsche Anzahl der Argumente (nicht 3).\n");
- exit(EXIT_FAILURE);
- }
- if (pipe(pipefds) < 0)
- {
- perror("Fehler bei pipe().\n");
- exit(EXIT_FAILURE);
- }
- pid1 = fork();
- if(pid1 < 0)
- {
- perror("fork()");
- exit(EXIT_FAILURE);
- }
- else if(!pid1)
- {
- /*child1*/
- if(dup2(pipefds[1], 1) < 0) //pipefds[0] ist lese-ende, 1 ist schreib-ende?
- {
- perror("Fehler bei dup2 bei Child1.\n");
- exit(EXIT_FAILURE);
- }
- if(close(pipefds[0]) < 0)
- {
- perror("Fehler bei close() in Child1.\n");
- exit(EXIT_FAILURE);
- }
- char *argv1[] = { "/bin/ls", "-la", argv[1], NULL};
- execve(argv1[0], argv1, envp);
- perror("execve()");
- exit(EXIT_FAILURE);
- }
- pid2 = fork();
- if(pid2 <0)
- {
- perror("fork()");
- exit(EXIT_FAILURE);
- }
- else if(!pid2)
- {
- if(dup2(pipefds[0], 0) < 0)
- {
- perror("Fehler bei dup2 bei Child2.\n");
- exit(EXIT_FAILURE);
- }
- if(close(pipefds[1]) < 0)
- {
- perror("Fehler bei close() bei Child2.\n");
- exit(EXIT_FAILURE);
- }
- char *argv2[] = {"/bin/grep", argv[2], NULL};
- execve(argv2[0], argv2, envp);
- perror("execve()");
- exit(EXIT_FAILURE);
- }
- if (close(pipefds[0]) < 0)
- {
- perror("Fehler bei close() bei Parent.\n");
- exit(EXIT_FAILURE);
- }
- if (close(pipefds[1]) < 0)
- {
- perror("Fehler bei close() bei Parent.\n");
- exit(EXIT_FAILURE);
- }
- waitpid(pid1, &status1, 0);
- waitpid(pid2, &status2, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement