Question: Write a C program that creates 3 processes ( one parent and two children ) . The processes will relay information to each other in

Write a C program that creates 3 processes (one parent and two children). The processes will relay information to each other in following fashion:
The first process (parent) reads a text file input.txt line by line.
The second process reads from the first pipe and reverse letters (Capital to small and small to
capital).
The third process reads from the second pipe and outputs to a file e.g.,output.txt.
Please complete the missing parts in the provided skeleton code and dont change any other parts. Also, create
a text file, which contains several text lines. The file will be used as an input for your program. Lastly, please highlight what code you added in another color so I can see what additions you made.
#include
#include
#include
#include
#include
#define READ_END 0
#define WRITE_END 1
int main(int argc, char* argv[]){
// declare pipes
int fd1[2], fd2[2];
pid_t pid;
if (pipe(fd1)==-1|| pipe(fd2)==-1){
fprintf(stderr, "Pipe failed");
return 1;
}
pid = fork();
if (pid <0){
fprintf(stderr, "Fork Failed");
return 1;
}
if (pid >0){// Parent process
close(fd1[READ_END]);
close(fd2[WRITE_END]);
FILE* fp = fopen("input.txt","r");
if (fp == NULL){
fprintf(stderr, "Failed to open file
");
exit(1);
}
char line[BUFSIZ];
while (fgets(line, BUFSIZ, fp)){
write(fd1[WRITE_END], line, strlen(line));
memset(line,0, BUFSIZ);
read(fd2[READ_END], line, BUFSIZ);
printf("%s", line);
}
fclose(fp);
close(fd1[WRITE_END]);
close(fd2[READ_END]);
}
else {// Child process
pid = fork();
if (pid >0){// Second process
close(fd1[WRITE_END]);
close(fd2[READ_END]);
char line[BUFSIZ];
while (read(fd1[READ_END], line, BUFSIZ)){
for (int i =0; i < strlen(line); i++){
// Change lower to upper and upper to lower cases
// write code here
}
write(fd2[WRITE_END], line, strlen(line));
memset(line,0, BUFSIZ);
}
close(fd1[READ_END]);
close(fd2[WRITE_END]);
}
else {// Third process
// Close unused pipes
// write code here
FILE* fp = fopen("output.txt","w");
char line[BUFSIZ];
while (read(fd2[READ_END], line, BUFSIZ)){
// prints to the output.txt file
// write code here
}
fclose(fp);
close(fd2[READ_END]);
}
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!