In: Advanced Math
Allow the main process to generate a text file containing the text of this assignment.
The main process will then create two other processes and pass to them the name of the file and two codes (code1 and code2) using a pipe() system call. The codes are two different alphabetic letters such as “a” and “k”.
The child processes should search the file for the character in the interval received, compute their frequencies and return them through a separate pipe to the parent process.
The parent process should compute the total number of characters in the file, and the rate of the appearance of the character frequencies received from the two child process, through separate pipes.
The parent process should then form a table of alphabetic characters and their frequencies and print the table in a proper format on the screen.
Hint: You are expected to use fork(), pipe(), read(), write(), close(), waitpid(), and other appropriate system call, if needed.
ANSWER:
#include<string.h>
#include<unistd.h>
#include<stdio.h>
int main()
{
int pipefds1[2], pipefds2[2];
int returnstatus1, returnstatus2;
int waitpid;
char pipe1writemessage[20] = "a";
char pipe2writemessage[20] = "k";
char readmessage[20];
returnstatus1 = pipe(pipefds1);
if (returnstatus1 == -1)
{
printf("Unable to create pipe 1 \n");
return 1;
}
returnstatus2 = pipe(pipefds2);
if (returnstatus2 == -1)
{
printf("Unable to create pipe 2 \n");
return 1;
}
waitpid = fork();
if (waitpid != 0) // Parent process
{
close(pipefds1[0]); // Close the unwanted pipe1 read side
close(pipefds2[1]); // Close the unwanted pipe2 write side
printf("In Parent: Writing to pipe 1 – Message is %s\n",
pipe1writemessage);
write(pipefds1[1], pipe1writemessage,
sizeof(pipe1writemessage));
read(pipefds2[0], readmessage, sizeof(readmessage));
printf("In Parent: Reading from pipe 2 – Message is %s\n",
readmessage);
}
else
{
//child process
close(pipefds1[1]); // Close the unwanted pipe1 write side
close(pipefds2[0]); // Close the unwanted pipe2 read side
read(pipefds1[0], readmessage, sizeof(readmessage));
printf("In Child: Reading from pipe 1 – Message is %s\n",
readmessage);
printf("In Child: Writing to pipe 2 – Message is %s\n",
pipe2writemessage);
write(pipefds2[1], pipe2writemessage,
sizeof(pipe2writemessage));
}
return 0;
}