In this assignment, you will write a program that includes a parent process and
ID: 3908881 • Letter: I
Question
In this assignment, you will write a program that includes a parent process and a child process. The parent process will read a word from standard input and send it to the child process. The child process will print the first line of a poem that contains the word. If the word is not in any line of the poem, the child process will print a message. The poem is read from a file whose name is provided as a program parameter (argv[1]) I. General Requirements Your programs' implementation must include the following features: .The program starts by reading the poem from a file. The name of the file is given as a parameter. If the filename is wrong or the file cannot be opened, the program prints an error message and exits. .The program then creates a pipe (named or unnamed) that the parent and child processes use to communicate. If the pipe cannot be created properly, the program prints an error message and exits. The program will create a child process. After that, the parent and the child processes run concurrently The parent process keeps reading a word from the standard input and writes the word to the pipe. The parent process exits when the word is "i The child process will read a word from the pipe and search for the word in the poem. If the word is not in the poem, it prints a message that the word is not found. Otherwise, it prints the first line where the word appears in the poem. o o Your submission must include a Makefile which includes rules to compile your program. . A sample run may look like this: turing2 : ~/Documents/Assign6% more poem . txt Time flies, and memories fade People change and new friendships are made Only the true remain forever at our side Eventually the disappointment and pain will subside. It is all about the journey and what has yet to come. Life is what you make of it; you are what you 've become Life is too short to hold onto regrets. Forgiveness is key even though you may never forget. turing2 :~/Documents/Assign6% ./a.out poem .txt what what is found: It is al1 about the journey and what has yet to come key key is found: Forgiveness is key even though you may never forget my is not found rose rose is not found t is found: Only the true remain forever at our side quit turing2 : ~/Documents/Assign6% You may use any systems or tools to create and run your program. However, your program must run correctly on a departmental Linux computer Please follow the guidelines in the programming guideline documentExplanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc , char *argv[])
{
int fd[2], num_bytes;
pid_t cpid;
char data[] = "Hello, world! ";
char readbuf[2];
char readbuf1[40];
int j,i;
if (pipe(fd) == -1){
printf("Pipe error ");
return 0;
}
srand(time(NULL));
if((cpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(cpid == 0)
{ //Child Process
close(fd[1]);
char line[100];
while(1){
num_bytes = read(fd[0], readbuf1, sizeof(readbuf1));
printf("Received: %s ",readbuf1);
if (strcmp(readbuf1,"quit") == 0)
break;
FILE *fp = fopen(argv[1], "r");
if (fp == NULL)
break;
int found = 0;
while (fgets(line,100,fp)!= NULL){
if (strtok(line,readbuf1) != NULL){
printf("%s found: %s ",readbuf1,line);
found = 1;
}
}
fclose(fp);
if (found == 0)
printf("%s not found ",readbuf1);
}
close(fd[0]);
exit(0);
}
else
{ //Parent Process
close(fd[0]);
char word[20];
while(1){
scanf("%s", &word);
write(fd[1], word, (strlen(word)));
if (strcmp(word,"quit") == 0)
break;
}
close(fd[1]);
}
return(0);
}
Makefile
gcc <filename>