Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a program that includes a parent process and a child process. The parent p

ID: 3822103 • Letter: W

Question

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]).

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. o 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 “quit”. o 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.

Explanation / Answer


package producerconsumer.chegg;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
public class ProducerConsumerTest {
   public static void main(String[] args) {
       Pipe c = new Pipe();
       Producer p1 = new Producer(c, 1);
       Consumer c1 = new Consumer(c, 1);
       p1.start();
       c1.start();
   }
}
class Pipe {
   private Queue<String> contents = new LinkedList<String>();
   private boolean available = false;
   private boolean isExit = false;
   public synchronized String get() {
       while (available == false) {
           try {
               wait();
           } catch (InterruptedException e) {
           }
       }
       available = false;
       notifyAll();
       return contents.poll();
   }
   public synchronized void put(String value) {
       while (available == true) {
           try {
               wait();
           } catch (InterruptedException e) {
           }
       }
       contents.add(value);
       available = true;
       notifyAll();
   }
   public boolean isExit() {
       return isExit;
   }
   public void setExit(boolean exitStatus) {
       isExit = exitStatus;
   }
}
class Consumer extends Thread {
   private Pipe communicationPipe;
   private int number;
   public Consumer(Pipe c, int number) {
       communicationPipe = c;
       this.number = number;
   }
   public void run() {
       List<String> poem = this.readFile("abc");
       while (!communicationPipe.isExit()) {
           String value = communicationPipe.get();
           boolean isWordMatched = false;
           if ("QUIT".equalsIgnoreCase(value)) {
               System.out.println(" Quit !! Child thread goind to be died");
               break;
           }
           for (String string : poem) {
               if (string != null && value != null) {
                   if (string.contains(value)) {
                       System.out.println("Word found, Line is : " + string);
                       isWordMatched = true;
                       break;
                   }
               }
           }
           if (!isWordMatched) {
               System.out.println("Word not found !!");
           }
       }
   }
   public List<String> readFile(String fileNameWithPath) {
       List<String> list = new ArrayList<String>();
       try (BufferedReader br = new BufferedReader(new FileReader(new File(
               "d:\" + fileNameWithPath + ".txt")))) {
           String sCurrentLine;
           while ((sCurrentLine = br.readLine()) != null) {
               list.add(sCurrentLine);
           }
       } catch (IOException e) {
           e.printStackTrace();
       }
       return list;
   }
}
class Producer extends Thread {
   private Pipe cubbyhole;
   private int number;
   public Producer(Pipe c, int number) {
       cubbyhole = c;
       this.number = number;
   }
   public void run() {
       String word = "";
       Scanner scanner = null;
       while (!"quit".equals(word)) {
           scanner = new Scanner(System.in);
           System.out.println(" Enter word ");
           word = scanner.next();
           if (!"quit".equals(word)) {
               cubbyhole.put(word);
           } else {
               cubbyhole.setExit(true);
               // just to notify the child thread that parent got stopped and
               // please stop your process as well
               cubbyhole.put("Quit");
               if (cubbyhole.isExit()) {
                   System.out.println("Parent thread going to be died");
                   break;
               }
           }
       }
       if (scanner != null) {
           scanner.close();
       }
   }
}
Description:
------------------
Please run the main method of ProducerConsumerTest.java class. Please let me know if you face any problem here.


Output
------------
Enter word
john
Enter word
Word not found !!
mummny]
Enter word
Word not found !!
mummy
Enter word
Word found, Line is : mummny finger mummy finger where are you !!
how are you
Enter word
Word found, Line is : how are you !!
quit
Parent thread going to be died
Quit !! Child thread goind to be died