31 lines
631 B
C
31 lines
631 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/wait.h>
|
|
|
|
int main() {
|
|
char *command1[] = {"ls","ls", ".", NULL};
|
|
char *command2[] = {"wc","wc", "-l", NULL};
|
|
int p[2];
|
|
|
|
pipe(p);
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0) {
|
|
close(p[0]);
|
|
dup2(p[1], STDOUT_FILENO);
|
|
execvp(command1[0], &command1[1]);
|
|
close(p[1]);
|
|
exit(0);
|
|
}
|
|
else{
|
|
close(p[1]);
|
|
dup2(p[0], STDIN_FILENO);
|
|
//dup2(STDIN_FILENO,p[0]);
|
|
execvp(command2[0], &command2[1]);
|
|
close(p[0]);
|
|
exit(0);
|
|
}
|
|
return 0;
|
|
} |