Question


Process Synchronization Assignment: Readers-Writers Problem . Consider readers and writers share a bounded array. ii. Writers

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

import java.util.concurrent.Semaphore;

class ReaderWritersProblem {

static Semaphore readLock = new Semaphore(1);
static Semaphore writeLock = new Semaphore(1);
static int rCount = 0;

static class Read implements Runnable {
@Override
public void run() {
try {
//Acquire Section
readLock.acquire();
rCount++;
if (rCount == 1) {
writeLock.acquire();
}
readLock.release();

//Reading section
System.out.println("Thread "+Thread.currentThread().getName() + " is READING");
Thread.sleep(1500);
System.out.println("Thread "+Thread.currentThread().getName() + " has FINISHED READING");

//Releasing section
readLock.acquire();
rCount--;
if(rCount == 0) {
writeLock.release();
}
readLock.release();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}

static class Write implements Runnable {
@Override
public void run() {
try {
writeLock.acquire();
System.out.println("Thread "+Thread.currentThread().getName() + " is WRITING");
Thread.sleep(2500);
System.out.println("Thread "+Thread.currentThread().getName() + " has finished WRITING");
writeLock.release();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}

public static void main(String[] args) throws Exception {
Read read = new Read();
Write write = new Write();
Thread tr1 = new Thread(read);
tr1.setName("Reader1");
   Thread tr2 = new Thread(write);
   tr2.setName("Writer1");
Thread tr3 = new Thread(read);
tr3.setName("Reader2");
Thread tr4 = new Thread(write);
tr4.setName("Writer2");
Thread tr5 = new Thread(read);
tr5.setName("Reader3");
tr1.start();
   tr2.start();
tr3.start();
tr4.start();
tr5.start();
}
}

Add a comment
Know the answer?
Add Answer to:
Process Synchronization Assignment: Readers-Writers Problem . Consider readers and writers share a bounded array. ii. Writers...
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
  • 4. (20 points) In the class we mentioned briefly the readers-writers problem with writers priority. The problem can...

    4. (20 points) In the class we mentioned briefly the readers-writers problem with writers priority. The problem can be solved in guarded commands as follows: void writer) void reader --owriters when writers0 readers++i when (readers0)&&(active writers0) active writers++ //read //write [readers--1 [writers-- active writers-- Here writers represents the number of threads that are either writing or waiting to write. The variable active_writers represents the number of threads (0 or1) that are currently writing. Implement the solution using either POSIX threads...

  • In the class we mentioned briefly the readers-writers problem with writers priority. The problem can be solved in guard...

    In the class we mentioned briefly the readers-writers problem with writers priority. The problem can be solved in guarded commands as follows: void writer) void reader when writers-0 readers++ when (readers-0)&&(active writers0) active_writers++ /Iread //write [readers--] (writers-active writers- Here writers represents the number of threads that are either writing or waiting to write. The variable active_writers represents the number of threads (0 or 1) that are currently writing. In the class we mentioned briefly the readers-writers problem with writers priority....

  • TRUE-FALSE     Basic synchronization principles and multithreading 1. Java user threads can implement both busy-waiting and no-busy-waiting...

    TRUE-FALSE     Basic synchronization principles and multithreading 1. Java user threads can implement both busy-waiting and no-busy-waiting policy. 2. Priority inversion avoids deadlocks. 3. Spinlock mutex can be used as an adaptive mutex. 4. Java RTE can be blocked for Input/Output operation. 5. Interrupted user thread, which executes a method in a monitor, must be rolled back to undo any changes it performed. 6. The synchronization primitive by disabling interrupts can be used by an application program. 7. Bounded-waiting requirement is...

  • pleasw answer as soon as possible . this is for my exam practice . please read...

    pleasw answer as soon as possible . this is for my exam practice . please read the code and answer the question In a system, there are multiple reader processes which read from a shared file and multiple writer processes which update a shared file. The following variables are shared among all processes: int readCounter; semaphore mutex, writeLock; Reader and writer processes are given in the following C++-like pseudo programs: Reader Process // Non-Critical Section P(mutex); Writer Process // Non-Critical...

  • 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...

  • 10) Unlike a signal, which conveys only the occurrence of a particular event and contains no...

    10) Unlike a signal, which conveys only the occurrence of a particular event and contains no information content, a pipe can be thought of as a scratch file created by a system call. It can be used as a communications channel between concurrently running processes. The interface call to a pipe is similar to that for any file. In fact, the process reads and writes to a pipe just like any file. Unlike files, however, pipes do not represent actual...

  • EMULATE A PROCESS CONTROL BLOCK In this assignment you will use Java, Python or C++ to...

    EMULATE A PROCESS CONTROL BLOCK In this assignment you will use Java, Python or C++ to create a process control block. All objects described aren’t provided, you will create them. You can divide up this code into separate files or put all code in one file. Proper documentation is essential, if there are no comments 5% of the total grade will be deducted. The process control block object PCB should have the following fields: ID: a unique ID for this...

  • Assignment 1 In this assignment you will be writing a tool to help you play the...

    Assignment 1 In this assignment you will be writing a tool to help you play the word puzzle game AlphaBear. In the game, certain letters must be used at each round or else they will turn into rocks! Therefore, we want to create a tool that you can provide with a list of letters you MUST use and a list of available letters and the program returns a list of all the words that contain required letters and only contain...

  • Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to...

    Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to display several different types (classes) of objects. Let's say, we want to display three objects of type Deer, two objects of type Tree and one object of type Hill. Each of them contains a method called display. We would like to store their object references in a single array and then call their method display one by one in a loop. However, in Java,...

  • /*hello everyone. my question is mostly related to the garagetest part of this assignment that i've...

    /*hello everyone. my question is mostly related to the garagetest part of this assignment that i've been working on. i am not sure how to read the file's contents AND put them into variables/an array. should it be in an arraylist? or maybe a regular array? or no arrays at all? i am also not sure how to call the factory method from garagec. i'm thinking something along the lines of [Garage garage = new GarageC.getInstance("GarageC", numFloors, areaofEachFloor);] but i...

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