问题描述
共享资源的同步问题
(由于2011年11月15日(晚上),16(天))
假设有多个读取器想要从数据库中读取,并且有多个写入器想要更新数据库.尽管许多读取器可以同时从数据库读取数据,但是当写入器正在更新数据库时,不应允许其他写入器和任何读取器访问数据库.也就是说,基本上是并发的读者和互斥的作家问题,如下所示:
这也称为读取器/写入器问题.
该领域的研究人员一直将此问题作为测试案例来研究解决共享资源同步问题的不同策略.策略是:
读者偏好:
也就是说,当新的阅读者和新的作者加入系统时,阅读器进程将被赋予更高的访问数据库的优先级.
以下三种策略基于以下情况:
在读取器队列和写入器队列中分别有许多读取器和多个写入器在等待,并且一个写入器正在更新数据库.一旦编写者完成了数据库更新,那么问题便是:应该授予哪个进程访问数据库的权限?
强烈的读者偏好:
在此策略中,应允许所有等待的读取器进程通过所有等待的写入器进程访问数据库.同样,如果一个新的读取器进程和一个新的写入器进程加入了系统,则应允许该读取器进程与其他读取器一起加入,以立即读取数据库,而到达的写入器进程将进入写入器队列,以等待对数据库的访问.
读者偏好偏弱:
在这种策略中,我们没有明确选择读取器进程,而是随机选择读取器和写入器之间的下一个进程.
读者偏好较弱:
在这种策略中,我们给予等待的编写器进程比等待的读取器进程更高的优先级.
项目:
在这个项目中,您将使用弱读者首选项"解决方案来解决读写器问题.您的程序将是多线程Java程序,即每个读取器和写入器进程都将实现为单独的Java线程.您应该使用信号量/监视器进行同步.
假设:最多有5个读者和5个作家的过程.数据库只是大小为1的整数缓冲区.即
int buf = 0; //数据库,一个初始值为0的int缓冲区
编写者应生成小于10的正整数值并将其写入数据库.
要求:
您的程序应在命令行中接受读者人数和作家人数作为参数.
您的程序名称应为RW.java.
因此,用于运行系统的命令将为
> java RW<读者人数> <作者数>
阅读器进程的字符名称应为A,B,C,D,E.
编写器进程的字符名称应为F,G,H,I,J.
除了读写之外,线程还应随机睡眠一段时间(0到1000ms)以模拟实际情况.
您的程序应反映Reader的并发行为.为此,读取器应打印出读取器计数值(即当前正在读取数据库的读取器数量),如下所示.为了使这种行为更清晰和易于观察,您可能希望使Reader线程在增加读取器计数之后但在减小计数之前进入睡眠状态.
该项目的示例输出如下:
---- jGRASP执行:java ReaderWriter 2 1
开始...
->写入器F将缓冲区设置为0
->当前正在读取DB的读取器数量= 1读取到的读取器A 0
->当前正在读取DB的读取器数量= 1读取到的读取器B 0
---- jGRASP:操作完成.
________________________________________________
---- jGRASP执行:java ReaderWriter 2 1
开始...
->写入器F将缓冲区设置为1
->当前正在读取DB的读取器数量= 2已检索到读取器B 1
->当前正在读取DB的读取器数量= 1读取到的读取器A 1
---- jGRASP:操作完成.
_______________________________________________
---- jGRASP执行:java ReaderWriter 3 2
开始...
->写入器F将缓冲区设置为7
->写入器G将缓冲区设置为9
->当前正在读取DB的读取器数量= 1读取到的读取器A 9
->当前正在读取DB的读取器数量= 2已读取9个读取器B
->当前正在读取DB的读取器数量= 1已检索到9个读取器C
---- jGRASP:操作完成.
________________________________________________
---- jGRASP执行:java ReaderWriter 4 2
开始...
->写入器F将缓冲区设置为9
->写入器G将缓冲区设置为0
->当前正在读取DB的读取器数量= 2已检索到读取器B 0
->当前正在读取DB的读取器数量= 1读取到的读取器A 0
->当前正在读取DB的读取器数量= 1已检索到读取器D 0
->当前正在读取DB的读取器数量= 1读取到的读取器C 0
---- jGRASP:操作完成.
提示:查看此演示和代码段.类示例
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
推荐答案
package homework.readwrite;
public class ReadWriteApp {
public void main(String[] args) {
// code goes here
}
}
这篇关于此项目编码所需的帮助可以是C ++或Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!