Last active
April 13, 2017 23:19
-
-
Save filipefigcorreia/61a82057bdeaf74f02ae17182665c643 to your computer and use it in GitHub Desktop.
Problema 4 da ficha 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define MAXSIZE 1024 | |
#define READ 0 | |
#define WRITE 1 | |
int main(int argc, char *argv[]){ | |
char lsCommand[256]; | |
char grepCommand[256]; | |
int p[2]; | |
int j[2]; | |
pid_t pid = 0; | |
pipe(p); | |
pipe(j); | |
if(argc != 3){ | |
printf("Usage: %s [ls argument] [grep argument]\n",argv[0]); | |
exit(1); | |
} | |
pid = fork(); | |
if(pid == 0){ | |
close(p[READ]); | |
dup2(p[WRITE],STDOUT_FILENO); | |
execlp("ls","ls","-laR",argv[1],NULL); | |
perror("erro"); | |
}else if(pid > 0){ | |
pid = fork(); | |
if(pid == 0){ | |
close(j[READ]); | |
dup2(j[WRITE],STDOUT_FILENO); | |
close(p[WRITE]); | |
dup2(p[READ],STDIN_FILENO); | |
execlp("grep","grep",argv[2],NULL); | |
perror("erro"); | |
}else{ | |
close(p[WRITE]); | |
close(p[WRITE]); | |
close(j[WRITE]); | |
dup2(j[READ],STDIN_FILENO); | |
execlp("sort","sort",NULL); | |
perror("erro"); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment