[英]Semaphore in Java/Operating Systems
I am new in the forum and in programming in general.At the moment i study "Operation Systems" and Java. 我是论坛和编程的新手。目前我正在研究“操作系统”和Java。 This week we started the topic "Semaphores" and it seems to me that it's a bit confusing. 本周我们开始讨论“信号量”这个主题,在我看来这有点令人困惑。 I have this exercise: 我有这个练习:
shared variable numberofworks = 0
共享变量numberofworks = 0
EmployeeA { while (True) { A_works_outside(); A_works_inside(); numberofworks =numberofworks + 1; } } EmployeeB { while (True) { B_works_outside(); B_works_inside(); numberofworks =numberofworks + 1; } }
well,the exercise wants to use semaphores (P and V) so 好吧,练习想要使用信号量(P和V)
A_works_inside()
runs only when B_works_outside() is finished 在重复EmployeeA时, A_works_inside()
仅在B_works_outside()完成时运行 In i repetition of EmployeeB the Β_works_inside()
runs only when Α_works_outside() is finished 在重复EmployeeB时, Β_works_inside()
仅在Α_works_outside()完成时运行
Variable numberofworks
must be the sum of numberofworksA + numberofworksB
变量numberofworks
必须是numberofworksA + numberofworksB
的总和
I must write the code in Java and in "operation system" before helping me in this exercise (if you can of course) General questions in semaphores: What's the basic difference between mutex and a sempahore? 在帮助我进行本练习之前,我必须用Java和“操作系统”编写代码(如果可以的话)信号量中的一般问题:互斥体和sempahore之间的基本区别是什么? and how should i understand when to use mutex or not? 我应该如何理解何时使用互斥?
Trying to use a resource protected by a Mutex: 尝试使用受互斥锁保护的资源:
- Hi! Can I ?
*If free*
- Yup go on.
*Else*
- No, go away!
Trying to use a resource protected by a Semaphore: 尝试使用信号量保护的资源:
- Hi! Can I ?
*If free*
- Yup go on.
*Else*
- No, wait in the queue!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.