#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
int fd1[2], fd2[2], fd3[2], fd4[2];
pid_t pid1, pid2, pid3, pid4;
if(pipe(fd1) == -1 || pipe(fd2) == -1 || pipe(fd3) == -1 || pipe(fd4) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid1 = fork();
if(pid1 == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if(pid1 == 0) {
close(fd1[0]);
dup2(fd1[1], STDOUT_FILENO);
execlp("cat", "cat", "file.txt", NULL);
perror("execlp");
exit(EXIT_FAILURE);
} else { // Parent process
close(fd1[1]); // Close unused write end
wait(NULL); // Wait for child to finish
char buffer[1024];
ssize_t count = read(fd1[0], buffer, sizeof(buffer)); // Read from pipe
printf("%.*s", (int)count, buffer); // Print the output
}
pid2 = fork();
if(pid2 == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if(pid2 == 0) {
close(fd1[1]);
dup2(fd1[0], STDIN_FILENO);
close(fd2[0]);
dup2(fd2[1], STDOUT_FILENO);
execlp("tr", "tr", "[:upper:]", "[:lower:]", NULL);
perror("execlp");
exit(EXIT_FAILURE);
}else { // Parent process
close(fd2[1]); // Close unused write end
wait(NULL); // Wait for child to finish
char buffer[1024];
ssize_t count = read(fd2[0], buffer, sizeof(buffer)); // Read from pipe
printf("%.*s", (int)count, buffer); // Print the output
}
pid3 = fork();
if(pid3 == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if(pid3 == 0) {
close(fd2[1]);
dup2(fd2[0], STDIN_FILENO);
close(fd3[0]);
dup2(fd3[1], STDOUT_FILENO);
execlp("grep", "grep", "distance", NULL);
perror("execlp");
exit(EXIT_FAILURE);
} else { // Parent process
close(fd3[1]); // Close unused write end
wait(NULL); // Wait for child to finish
char buffer[1024];
ssize_t count = read(fd3[0], buffer, sizeof(buffer)); // Read from pipe
printf("%.*s", (int)count, buffer); // Print the output
}
pid4 = fork();
if(pid4 == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if(pid4 == 0) {
close(fd3[1]);
dup2(fd3[0], STDIN_FILENO);
execlp("wc", "wc", "-l", NULL);
perror("execlp");
exit(EXIT_FAILURE);
}else { // Parent process
close(fd4[1]); // Close unused write end
wait(NULL); // Wait for child to finish
char buffer[1024];
ssize_t count = read(fd4[0], buffer, sizeof(buffer)); // Read from pipe
printf("%.*s", (int)count, buffer); // Print the output
}
close(fd4[0]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
int fd1[2], fd2[2], fd3[2], fd4[2];
pid_t pid1, pid2, pid3, pid4;
if(pipe(fd1) == -1 || pipe(fd2) == -1 || pipe(fd3) == -1 || pipe(fd4) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid1 = fork();
if(pid1 == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if(pid1 == 0) {
char *argv[] = {"cat", "file.txt", NULL};
close(fd1[0]);
dup2(fd1[1], STDOUT_FILENO);
execvp("cat", argv);
perror("execlp");
exit(EXIT_FAILURE);
} else { // Parent process
close(fd1[1]); // Close unused write end
wait(NULL); // Wait for child to finish
char buffer[1024];
ssize_t count = read(fd1[0], buffer, sizeof(buffer)); // Read from pipe
printf("%.*s", (int)count, buffer); // Print the output
}
pid2 = fork();
if(pid2 == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if(pid2 == 0) {
close(fd1[1]);
dup2(fd1[0], STDIN_FILENO);
close(fd2[0]);
dup2(fd2[1], STDOUT_FILENO);
execlp("tr", "tr", "[:upper:]", "[:lower:]", NULL);
perror("execlp");
exit(EXIT_FAILURE);
}else { // Parent process
close(fd2[1]); // Close unused write end
wait(NULL); // Wait for child to finish
char buffer[1024];
ssize_t count = read(fd2[0], buffer, sizeof(buffer)); // Read from pipe
printf("%.*s", (int)count, buffer); // Print the output
}
pid3 = fork();
if(pid3 == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if(pid3 == 0) {
close(fd2[1]);
dup2(fd2[0], STDIN_FILENO);
close(fd3[0]);
dup2(fd3[1], STDOUT_FILENO);
execlp("grep", "grep", "distance", NULL);
perror("execlp");
exit(EXIT_FAILURE);
} else { // Parent process
close(fd3[1]); // Close unused write end
wait(NULL); // Wait for child to finish
char buffer[1024];
ssize_t count = read(fd3[0], buffer, sizeof(buffer)); // Read from pipe
printf("%.*s", (int)count, buffer); // Print the output
}
pid4 = fork();
if(pid4 == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if(pid4 == 0) {
close(fd3[1]);
dup2(fd3[0], STDIN_FILENO);
execlp("wc", "wc", "-l", NULL);
perror("execlp");
exit(EXIT_FAILURE);
}else { // Parent process
close(fd4[1]); // Close unused write end
wait(NULL); // Wait for child to finish
char buffer[1024];
ssize_t count = read(fd4[0], buffer, sizeof(buffer)); // Read from pipe
printf("%.*s", (int)count, buffer); // Print the output
}
close(fd4[0]);
return 0;
}