ex1 td5
This commit is contained in:
47
td/td5/ex1.c
Normal file
47
td/td5/ex1.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <sys/wait.h>
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
pid_t p;
|
||||
int t[2];
|
||||
assert(pipe(t) == 0);
|
||||
|
||||
p = fork();
|
||||
|
||||
if (p == 0){
|
||||
close(t[0]);
|
||||
dup2(t[1],STDOUT_FILENO);
|
||||
close(t[1]);
|
||||
execlp("ls","ls",NULL);
|
||||
assert(0);
|
||||
}
|
||||
|
||||
p = fork();
|
||||
|
||||
if (p == 0){
|
||||
int fd = open("output.txt",O_WRONLY|O_CREAT|O_APPEND,0644);
|
||||
assert(fd != -1);
|
||||
|
||||
close(t[1]);
|
||||
dup2(t[0],STDIN_FILENO);
|
||||
close(t[0]);
|
||||
dup2(fd,STDOUT_FILENO);
|
||||
close(fd);
|
||||
execlp("wc","wc",NULL);
|
||||
assert(0);
|
||||
|
||||
}
|
||||
|
||||
close(t[0]);
|
||||
close(t[1]);
|
||||
|
||||
wait(NULL);
|
||||
wait(NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user