Files
BUT2FI_2026_R3.05/td/td6/correction2/ex1.c
T
2026-10-07 17:19:14 +02:00

42 lines
636 B
C

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/wait.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
pid_t p1,p2;
int fd[2];
assert(pipe(fd) == 0);
p1 = fork();
if (p1 == 0){
close(fd[0]);
dup2(fd[1],STDOUT_FILENO);
close(fd[1]);
execlp("ls","ls",NULL);
assert(0);
}
p2 = fork();
if (p2 == 0){
int out = open("output.txt",O_CREAT|O_WRONLY|O_APPEND,0644);
dup2(out,STDOUT_FILENO);
close(out);
close(fd[1]);
dup2(fd[0],STDIN_FILENO);
close(fd[0]);
execlp("wc","wc",NULL);
assert(0);
}
close(fd[0]);
close(fd[1]);
wait(NULL);
wait(NULL);
return 0;
}