Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/wait.h>
- #include <zconf.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #define CHILD_AMOUNT 2
- int create_child() {
- int i;
- for(i=0;i<CHILD_AMOUNT;i++){
- pid_t pid = fork();
- if (pid== 0) {
- return i+1;
- } else if (pid > 0) {
- continue;
- } else {
- printf("Erro ao fazer fork\n");
- exit(-1);
- }
- }
- return 0;
- }
- void close_descriptor(int descriptor) {
- if (close(descriptor) == -1) {
- printf("Nao foi possivel fechar o pipe %d\n", descriptor);
- exit(-1);
- }
- }
- int main(int argc) {
- int fd1[2],fd2[2],id;
- if (pipe(fd1) == -1) {
- printf("Erro ao criar o pipe\n");
- exit(-1);
- }
- if (pipe(fd2) == -1) {
- printf("Erro ao criar o pipe\n");
- exit(-1);
- }
- id=create_child();
- if(id==0){
- close_descriptor(fd1[0]);
- dup2(fd1[1],1);
- execlp("ls","ls","-la",(char*)NULL);
- exit(-1);
- }else if(id==1){
- close_descriptor(fd1[1]);
- close_descriptor(fd2[0]);
- dup2(fd1[0],0);
- dup2(fd2[1],1);
- sleep(1);
- execlp("sort","sort",(char*)NULL);
- exit(-1);
- }else{
- close_descriptor(fd2[1]);
- dup2(fd2[0],0);
- sleep(2);
- execlp("wc","wc","-l",(char*)NULL);
- exit(-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement