Question

Question II This question carries 50% of the marks for this assignment. This question is about...

Question II

This question carries 50% of the marks for this assignment.

This question is about avoiding race condition by using synchronization mechanism provided by the JAVA programming language. We provided you with a Netbeans JAVA project. The project idea is as follows: we want to merge the content of four files into a single file, as follows. Below is the content of all four files:

F1 content: 1 1 1 1

F2 content: 2 2 2 2

F3 content: 3 3 3 3

F4 content: 4 4 4 4

After merging them into the Sink file, we expect the following content in the sink file:1234123412341234

We want to read from each file only one integer value at a time (starting form file number 1, number 2 and so on) and write theses integers values ( in the same order we read them) into a specific sink file, we want to repeat it until all file files are empty.

Please note that all files have the same number of integers.

We have provided you with a template project that needs to be modified to accomplish the above task correctly.

The project is composed of the following classes including four text files:

Four text files called “f1.txt”, “f2.txt”, “f3.txt” and “f4.txt”, each file has a sequence of integer corresponding its name, for instance “f2.txt” contains a sequence of integers all are 2.

The following JAVA source code files are included in the NetBeans projects:

WriteToFile: A helper class to write into a file.

SyncWriteToFile: which control the access to the file, it does so by aggregating an object of the class “WriteToFile”

MergeFiles that extends Thread: it is intended to read all integers form a specific file and use an object of “SyncWriteToFile” to write them into the sink file at the appropriate place.

TMA_Tm298_SummerEgJo: contains the main method to test the program

Read the source code of the above classes carefully and run the project. You will discover that the content of the sink file is not as expected and each time you run it you got a different content. Provide screen shots form running the project without any modification. Provide convincing justification, why you got these results. [15 marks]

In order to get the expected sink file content, you need to modify the project as follows:

You can do so by first identifying the shared resources and then the critical section code in the project. [10 marks]. Then you need to change the critical section code so that the sink file contains the following result no matter how many times you run the project. The sink file correct content is :1234123412341234. [25 marks]

You are sked to submit the whole project (as one zip file) after all modification.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// PLEASE LIKE THE SOLUTION
// FEEL FREE TO DISCUSSION IN COMMENT SECTION
// JAVA PROGRAM

import java.io.*;
import java.util.*;

class WriteToFiles{
   // attribute to store file writer
   public static void writeFile(FileWriter myWriter,char a){
       try{
           myWriter.write(a);
       }
       catch(Exception e){
           System.out.println(e.getMessage());
       }
   }
}

class MergeFiles extends Thread{
   // run method
   // attribute
   // writer file
   static FileWriter myWriter;
  
   // reader file
   static FileReader reader;

   public void run(){
       try{
           // read and write to file
           int i;
           if((i = reader.read()) != -1){
               WriteToFiles wr = new WriteToFiles();
               wr.writeFile(myWriter,(char)i);
               Test.done = 0;
           }
           else{
               Test.done++;
           }
       }
       catch (Exception e){
           // message
           System.out.println ("Exception is caught");
       }
   }
}

// Test
class Test{
   // attribute
   static int done = 0;

   public static void main(String rg[]){

       try{

           MergeFiles.myWriter = new FileWriter(new File("output.txt"));
           FileReader file1 = new FileReader(new File("f1.txt"));
           FileReader file2 = new FileReader(new File("f2.txt"));
           FileReader file3 = new FileReader(new File("f3.txt"));
           FileReader file4 = new FileReader(new File("f4.txt"));

           // now call reader writer in sync way
           while(Test.done != 4){

               MergeFiles.reader = file1;
               MergeFiles r1 = new MergeFiles();
               r1.run();
               r1.join();

               MergeFiles.reader = file2;
               MergeFiles r2 = new MergeFiles();
               r2.run();
               r2.join();

               MergeFiles.reader = file3;
               MergeFiles r3 = new MergeFiles();
               r3.run();
               r3.join();

               MergeFiles.reader = file4;
               MergeFiles r4 = new MergeFiles();
               r4.run();
               r4.join();
           }

           // now close files
           MergeFiles.myWriter.close();
       }
       catch(Exception e){
           System.out.println(e.getMessage());
       }
   }

}

// SAMPLE OUTPUT

f1.txt x 1111

F2.txt x 2222

f3.txt 3333

f4.txt 4444

output.txt x Syn 1234123412341234

Add a comment
Know the answer?
Add Answer to:
Question II This question carries 50% of the marks for this assignment. This question is about...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question I This question carries 20% of the marks for this assignment. You are asked to...

    Question I This question carries 20% of the marks for this assignment. You are asked to develop a set of bash shell script: Write a script that asks for the user's salary per month. If it is less or equals to 800, print a message saying that you need to get another job to increase your income, what you earn is the Minim living cost. If the user's salary is higher than 800 and below 2000, print a message telling...

  • Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...

    Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeFiles as follows: java MergeFiles filel file2 mergedFile II. MergeFiles reads names stored in files file and file2 III. Merges the two list of names into a single list IV. Sorts that single list V. Ignores repetitions VI. Writes the sorted, free-of-repetitions list to a new file named mergedFile.txt VII. The names in all three files are stored as one...

  • Question III This question carries 20% of the marks for this assignment. Given the following mix...

    Question III This question carries 20% of the marks for this assignment. Given the following mix of tasks, task lengths and arrival times, compute the completion [5 marks and response time time from the arrival to the finish time) (5 marks for each task, along with the average response time for the FIFO. RR and SJF algorithms. Assume a time slice of 10 milliseconds and that all times are in milliseconds. You are kindly asked to provide the Gantt Chart...

  • use python IDEL Please highlight the answer Problem 3 Files. Read the following code and answer...

    use python IDEL Please highlight the answer Problem 3 Files. Read the following code and answer the next five questions about it. The contents of the files list 1.txt and list2.txt are the following (given side by side): list 1. txt content: list2.txt content 2 apples 2 apples 3 oranges 1 loaf of bread 2 eggs 2 eggs 1 cereal box # the program code starts here filename1 = "list 1.txt" filename2 - "list2.txt" filename3 - "listdiff.txt" with open (filename1,"r")...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • Assignment Modify the code Binary File IO Example Code. The code reads and prints information abo...

    In java Assignment Modify the code Binary File IO Example Code. The code reads and prints information about a BMP file. Modify the code so that it also prints the resolution of the BMP file (both width and height) and the number of bits per pixel. Do not use any Java library or third-party library code that reads BMP files. You must read the file using simple Java code and extract the information, similar to the code in the example....

  • Could I get some help with this assignment In this assignment, you'll write a program which...

    Could I get some help with this assignment In this assignment, you'll write a program which reads a text file, and prints a histogram of its word sizes. So, for example, the historgram should show the number of words of length 1, of length 2, etc., up to the number of words of length n, where n is some constant defined in your program. To obtain text files that are suitably long, you could try Project Gutenberg, a site containing...

  • Python Help Please! This is a problem that I have been stuck on.I am only suppose...

    Python Help Please! This is a problem that I have been stuck on.I am only suppose to use the basic python coding principles, including for loops, if statements, elif statements, lists, counters, functions, nested statements, .read, .write, while, local variables or global variables, etc. Thank you! I am using python 3.4.1. ***( The bottom photo is a continuation of the first one)**** Problem statement For this program, you are to design and implement text search engine, similar to the one...

  • create a new Java application called "WeightedAvgWithExceptions" (without the quotation marks), according to the following guidelines...

    create a new Java application called "WeightedAvgWithExceptions" (without the quotation marks), according to the following guidelines and using try-catch-finally blocks in your methods that read from a file and write to a file, as in the examples in the lesson notes for reading and writing text files. Input File The input file - which you need to create and prompt the user for the name of - should be called 'data.txt', and it should be created according to the instructions...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT