问题描述:The Cigarette-Smokers Problem. Consider a system with three smoker processes and one agent process. Each smoker continuously rolls a cigarette and then smokes it. But to roll and smoke a cigarette, the smoker needs three ingredients: tobacco, paper, and matches. One of the smoker processes has paper, another has tobacco, and the third has matches. The agent has an infinite supply of all three materials. The agent places two of the ingredients on the table. The smoker who has the remaining ingredient then makes and smokes a cigarette, signaling the agent on completion. The agent then puts out another two of the three ingredients, and the cycle repeats.

问题分析:通过问题描述,可以构建出一个 4 进程的系统,其中 3 个进程为 smoker 程序的实例,另一个是 agent 程序的实例。首先,Agent 执行提供材料的操作(原则上来说,抽烟者先就座等待也是可行的,代码上也易于实现,但是我编写了一段代码发现很累赘,这作为一个需要改进的部分,暂时在代码中做 TODO 标记),然后执行对 Smoker_i的 V 操作唤醒 Smoker_i,其信号量增 1,i 的值由 Agent 随机确定。接着,Agent 开始等待,进程切换到Smoker_i,Smoker_i 执行 P 操作,信号量减为 0,开始获取材料,卷烟,抽烟操作。抽完烟后 Smoker_i执行 V 操作,通知 Agent,然后 Smoker_i 循环至开头,开始等待,进程切换回 Agent。Agent 也开始循环,如此周期往复。采用同步机制的 PV 操作伪码如下:

Read the rest of this entry »

如果您喜欢本站的内容,欢迎订阅我的RSS以获取本站最新资讯。

大中华局域网用户订阅地址:http://feed.casparant.com/

国际互联网用户订阅地址:http://feeds.casparant.com/casparant

06月 10th, 2008 | 1,055 views纯发泄,Java考点

自动调用toString方法——

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//A.java
public class A extends B
{
	public String toString()
	{
		return "A";
	}
}
 
//B.java
public class B
{
	public String toString()
	{
		return "B";
	}
}
 
//C.java
public class C
{
	public static void main(String[] args)
	{
		Object o[] = {new A(), new B()};
 
		System.out.print(o[0]);
		System.out.print(o[1]);
	}
}

Read the rest of this entry »


Powered by Wordpress © 2008 - Caspar Ant | iKon Theme by TextNData | Admin |