操作系统模拟公交车司机与售票员进程之间的协同关系
2017-12-19 13:33
323 查看
使用java多线程编写代码:
一.实验目的:
用两个进程来模拟汽车司机与售票员之间的协同关系:一方面只有售票员把车门关好了司机才能关门,因此,售票员关好车门应通知司机开车;另一方面,只有当汽车已经停下,售票员才能开门上下客,故司机停车后应通知售票员。汽车当前正在始发站停车上客,试设必要的信号量并赋初值,写出它们的同步过程。
二.实验思路:
建立两个线程driver表示汽车司机,conductor表示售票员,两个线程同时进行。信号变量bus和seller分别代表汽车和售票员。初始值分别为0和1。线程driver中将bus=0,seller=1;线程seller中将bus=1,seller=0。在t从0到10000时间内两个进程交替执行,从而模拟司机与售票员的协同关系。
三.实验结果:
The bus is starting from the start station
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 2 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 3 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 4 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 5 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 6 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 7 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 8 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 9 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 10 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 11 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 12 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 13 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
Bus has reached the terminal station
End
四.实验源代码:
public class firsttask {
private static int bus=0;
private static int seller=1;
private static int t=0;//设置一个线程的执行时间
private static int is=1;
public static void main(String[]
args) {
Thread driver=new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
t++;
if(t==10000) {
System.out.println("The bus is running");
System.out.println("Bus has reached the terminal station");
bus=0;
seller=1;
System.out.println("End");
break;
}
if(bus==1) {
is++;
System.out.println("The bus is running");
System.out.println("having reached the "+
is+" bus stop");
System.out.println("passengers go on the bus");
bus=0;
seller=1;
}
}
}
});
Thread conductor=new Thread(new Runnable() {
@Override
public void run() {
while(true) {
if(t==10000) {
break;
}
if(seller==1) {
if(is==1) {
System.out.println("The bus is starting from the start station");
System.out.println("The seller is selling the tickets");
System.out.println("The seller tells the driver to close the door");
bus=1;
seller=0;
}
if(is!=1) {
System.out.println("The seller is selling the tickets");
System.out.println("The seller tells the driver to close the door");
bus=1;
seller=0;
}
}
}
}
});
driver.start();
conductor.start();
}
}
一.实验目的:
用两个进程来模拟汽车司机与售票员之间的协同关系:一方面只有售票员把车门关好了司机才能关门,因此,售票员关好车门应通知司机开车;另一方面,只有当汽车已经停下,售票员才能开门上下客,故司机停车后应通知售票员。汽车当前正在始发站停车上客,试设必要的信号量并赋初值,写出它们的同步过程。
二.实验思路:
建立两个线程driver表示汽车司机,conductor表示售票员,两个线程同时进行。信号变量bus和seller分别代表汽车和售票员。初始值分别为0和1。线程driver中将bus=0,seller=1;线程seller中将bus=1,seller=0。在t从0到10000时间内两个进程交替执行,从而模拟司机与售票员的协同关系。
三.实验结果:
The bus is starting from the start station
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 2 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 3 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 4 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 5 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 6 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 7 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 8 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 9 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 10 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 11 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 12 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
having reached the 13 bus stop
passengers go on the bus
The seller is selling the tickets
The seller tells the driver to close the door
The bus is running
Bus has reached the terminal station
End
四.实验源代码:
public class firsttask {
private static int bus=0;
private static int seller=1;
private static int t=0;//设置一个线程的执行时间
private static int is=1;
public static void main(String[]
args) {
Thread driver=new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
t++;
if(t==10000) {
System.out.println("The bus is running");
System.out.println("Bus has reached the terminal station");
bus=0;
seller=1;
System.out.println("End");
break;
}
if(bus==1) {
is++;
System.out.println("The bus is running");
System.out.println("having reached the "+
is+" bus stop");
System.out.println("passengers go on the bus");
bus=0;
seller=1;
}
}
}
});
Thread conductor=new Thread(new Runnable() {
@Override
public void run() {
while(true) {
if(t==10000) {
break;
}
if(seller==1) {
if(is==1) {
System.out.println("The bus is starting from the start station");
System.out.println("The seller is selling the tickets");
System.out.println("The seller tells the driver to close the door");
bus=1;
seller=0;
}
if(is!=1) {
System.out.println("The seller is selling the tickets");
System.out.println("The seller tells the driver to close the door");
bus=1;
seller=0;
}
}
}
}
});
driver.start();
conductor.start();
}
}
相关文章推荐
- 关于操作系统中进程、线程、和任务之间的关系
- 进程与线程之间的关系
- 细说进程与应用程序与上下文之间的关系
- 进程与线程之间的资源的关系
- 模拟操作系统优先级调度进程
- hadoop进程之间的对应关系和相关概念
- [C#参考]细说进程、应用程序域与上下文之间的关系
- Java进程之间以及跟JVM关系
- 实验三 进程调度模拟程序--操作系统
- 创建进程 fork()函数的基本使用 父子进程之间的关系
- Windows应用程序,操作系统,计算机硬件之间的相互关系
- 进程、线程和程序之间的关系
- 操作系统实验之一--进程调度算法的模拟实现
- linux下进程-进程组-会话之间的关系-代码说明一切
- 转:C#综合揭秘——细说进程、应用程序域与上下文之间的关系
- 进程 和 线程 之间的关系
- 快速理解:编程语言 il clr 机器码 操作系统之间的关系,用饮料作比喻
- 进程 、进程组、会话、控制终端之间的关系
- 进程 、进程组、会话、控制终端之间的关系 (转载 http://blog.csdn.net/yh1548503342/article/details/41891047)
- 细说进程、应用程序域与上下文之间的关系(一)——进程的概念与作用