Files
BUT2FI_2025_R3.05/td/td5/ex1.c

48 lines
650 B
C
Raw Normal View History

2025-10-01 15:07:46 +02:00
#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;
}