Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
Synchronization problem of shared resources

(due on Nov. 15(evening), 16(day) , 2011)
Assume there are multiple readers that want to read from a database and also, there are multiple writers that want to update the database. While many readers can read from the database at the same time, but when a writer is updating the database, no other writer and any reader should be allowed to access the database. That is , basically it is concurrent readers and mutually exclusive writer problem as shown below:



This is also known as Reader/Writer problem.

Researchers in this field have been using this problem as a test case to study different strategies for solving the synchronization problem of the shared resources. The strategies are:

Reader preference:

That is when a new reader and a new writer join the system, the reader process is given the higher priority to access the database.

The following three strategies are based on the following situation:

There are a number of readers and a number of writers waiting in reader queue and writer queue respectively, and a writer is updating the database. Once the writer finishes updating the database, then the question is: which process should be given access to the database?

Strong reader preference:

In this strategy, all the waiting reader processes should be allowed to access the database over all the waiting writer processes. Also, if a new reader process and a new writer process join the system, the reader process should be allowed to join the other readers in reading the database immediately, while the arriving writer process enters the writer queue waiting for the access to the database.

Weak reader preference:

In this strategy, we do not explicitly select the reader process but we select the next process between reader and writer randomly.

Weaker reader preference:

In this strategy, we give a waiting writer process a higher priority than a waiting reader process.

Project:

In this project you will solve the reader/writer problem by using "Weak reader preference" solution. Your program will be a multithreaded Java program, i.e., every reader and writer process will be implemented as a separate Java thread. You should use semaphore/monitor for synchronization.

Assumptions: There are at most 5 readers and 5 writers processes. The database is just an integer buffer of size 1.That is

int buf = 0; //database, an int buffer with initial value 0

The writers should generate a positive integer value less than 10 and write it to the database.

Requirements :

Your program should accept the number of readers and number of writers as a parameter in the command line.
Your program name should be RW.java.
Hence the command to run the system will be
>java RW <# of readers> <# of writers>

The reader processes should be given char names A, B, C, D, E.
The writer processes should be given char names F, G, H, I, J.
In addition to reading and writing, threads should sleep a random amount of time (0 to 1000ms) to simulate a real situation.
Your program should reflect the concurrent behavior of Readers. For this purpose, Readers should print out the reader count value (i.e. number of readers currently reading the database) as indicated below. To make this behavior more clear and easy to observe, you might want to make your Reader threads sleep after incrementing the reader count but before decrementing it.
A sample output of the project is given below:


----jGRASP exec: java ReaderWriter 2 1

Starting...

-> Writer F set buffer to 0
-># of readers currently reading DB = 1 Reader A retrieved 0
-># of readers currently reading DB = 1 Reader B retrieved 0

----jGRASP: operation complete.
________________________________________________
----jGRASP exec: java ReaderWriter 2 1

Starting...

-> Writer F set buffer to 1
-># of readers currently reading DB = 2 Reader B retrieved 1
-># of readers currently reading DB = 1 Reader A retrieved 1

----jGRASP: operation complete.
_______________________________________________
----jGRASP exec: java ReaderWriter 3 2

Starting...

-> Writer F set buffer to 7
-> Writer G set buffer to 9
-># of readers currently reading DB = 1 Reader A retrieved 9
-># of readers currently reading DB = 2 Reader B retrieved 9
-># of readers currently reading DB = 1 Reader C retrieved 9

----jGRASP: operation complete.
________________________________________________
----jGRASP exec: java ReaderWriter 4 2

Starting...

-> Writer F set buffer to 9
-> Writer G set buffer to 0
-># of readers currently reading DB = 2 Reader B retrieved 0
-># of readers currently reading DB = 1 Reader A retrieved 0
-># of readers currently reading DB = 1 Reader D retrieved 0
-># of readers currently reading DB = 1 Reader C retrieved 0

----jGRASP: operation complete.


Hints: Look at this demo and code snippet. class example
Posted
Updated 7-Nov-11 7:29am
v2
Comments
Smithers-Jones 7-Nov-11 15:30pm    
My, what a lazy prick you are.
Legor 10-Nov-11 5:35am    
Are you fing serious ??

Hints: we do not do your homework for you.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Nov-11 15:55pm    
Right, tell'em!
Voting a 5,
--SA
Richard MacCutchan 7-Nov-11 16:00pm    
Thank you, and to other 5-voters.
I've done the basics for you, just fill in the gaps:

Java
package homework.readwrite;

public class ReadWriteApp {

    public void main(String[] args) {
        // code goes here
    }
}
 
Share this answer
 
Comments
Albert Holguin 7-Nov-11 15:22pm    
Stop doing his homework! lol... :P
Sergey Alexandrovich Kryukov 7-Nov-11 15:56pm    
Valuable skeleton prototype, come to think about; something tells me OP did not yet approach this stage. :-)
Therefore, my 5.
--SA
TorstenH. 8-Nov-11 1:38am    
damn Nagy! You always think so clear when we struggle so hard!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900