从‘独木桥’到‘红绿灯’:手把手用Python模拟PV操作解决进程同步问题(附完整可运行代码)
·
从‘独木桥’到‘红绿灯’:Python实战PV操作解决进程同步难题
在计算机科学的教学中,操作系统原理往往是理论最为密集的领域之一。许多学生在学习进程同步时,面对PV操作和信号量等抽象概念,常常陷入"看得懂伪代码,写不出真程序"的困境。本文将通过Python代码,带您从经典的独木桥问题出发,逐步构建一个可视化的进程同步模拟器,最终扩展到更复杂的十字路口场景。
1. 理解独木桥问题的本质
独木桥问题本质上是一个资源争用场景的完美隐喻。想象一座只能单向通行的窄桥,东西两侧的车辆需要交替使用。这个场景恰好对应了操作系统中多个进程需要互斥访问共享资源的情况。
关键同步需求 :
- 当桥上无车时,允许任意方向的车辆开始通行
- 一旦某方向车辆开始通行,另一方向车辆必须等待
- 当前方向最后一辆车离开后,才能切换通行方向
传统教学中常用伪代码表示解决方案:
# 伪代码示例
semaphore bridge = 1 # 桥的互斥信号量
int eastCount = 0 # 东向车辆计数
semaphore eastMutex = 1 # 计数保护
# 东向车辆进程
P(eastMutex)
eastCount += 1
if eastCount == 1:
P(bridge) # 第一辆车占桥
V(eastMutex)
# ...过桥过程...
P(eastMutex)
eastCount -= 1
if eastCount == 0:
V(bridge) # 最后一辆车释放桥
V(eastMutex)
但这种抽象表示往往让初学者难以建立直观理解。接下来我们将用Python的 threading 模块将其转化为可运行的模拟程序。
2. Python实现基础独木桥模拟
我们首先构建最基本的单向通行模型。以下是完整的实现代码:
import threading
import time
import random
class OneLaneBridge:
def __init__(self):
self.bridge = threading.Semaphore(1) # 桥资源信号量
self.east_count = 0 # 东向车辆计数
self.west_count = 0 # 西向车辆计数
self.east_mutex = threading.Lock() # 东向计数锁
self.west_mutex = threading.Lock() # 西向计数锁
def eastbound_car(self, car_id):
# 东向车辆准备上桥
with self.east_mutex:
self.east_count += 1
if self.east_count == 1: # 第一辆车需要等待桥可用
self.bridge.acquire()
print(f"🚗东向车辆{car_id}开始上桥 (东向总数:{self.east_count})")
# 模拟过桥时间
time.sleep(random.uniform(0.5, 2))
print(f"🚗东向车辆{car_id}已过桥")
# 东向车辆下桥
with self.east_mutex:
self.east_count -= 1
if self.east_count == 0: # 最后一辆车释放桥
self.bridge.release()
print(f"🚗东向车辆{car_id}离开 (剩余东向:{self.east_count})")
def westbound_car(self, car_id):
# 西向车辆准备上桥
with self.west_mutex:
self.west_count += 1
if self.west_count == 1: # 第一辆车需要等待桥可用
self.bridge.acquire()
print(f"🚙西向车辆{car_id}开始上桥 (西向总数:{self.west_count})")
# 模拟过桥时间
time.sleep(random.uniform(0.5, 2))
print(f"🚙西向车辆{car_id}已过桥")
# 西向车辆下桥
with self.west_mutex:
self.west_count -= 1
if self.west_count == 0: # 最后一辆车释放桥
self.bridge.release()
print(f"🚙西向车辆{car_id}离开 (剩余西向:{self.west_count})")
# 模拟运行
def simulate_bridge():
bridge = OneLaneBridge()
cars = []
# 创建10辆随机方向的车辆
for i in range(10):
if random.choice([True, False]):
car = threading.Thread(target=bridge.eastbound_car, args=(i,))
else:
car = threading.Thread(target=bridge.westbound_car, args=(i,))
cars.append(car)
car.start()
time.sleep(random.uniform(0.1, 0.5)) # 随机间隔发车
for car in cars:
car.join()
if __name__ == "__main__":
simulate_bridge()
运行这段代码,你将在控制台看到类似如下的输出,清晰地展示了车辆同步过桥的过程:
🚗东向车辆0开始上桥 (东向总数:1)
🚗东向车辆0已过桥
🚗东向车辆2开始上桥 (东向总数:2)
🚗东向车辆0离开 (剩余东向:1)
🚗东向车辆2已过桥
🚙西向车辆1开始上桥 (西向总数:1)
🚗东向车辆2离开 (剩余东向:0)
🚙西向车辆1已过桥
🚙西向车辆1离开 (剩余西向:0)
...
代码关键点解析 :
- 使用
threading.Semaphore实现桥资源的互斥访问 - 为东西方向分别设置计数器和锁,确保计数操作的原子性
- 第一辆车上桥时获取信号量,最后一辆车离开时释放
- 随机时间间隔模拟真实交通流的不确定性
3. 进阶:限制桥上车辆数量的安全方案
基础版本允许同一方向无限数量的车辆连续过桥,这在现实中可能不安全。我们需要增加桥上最大车辆数的限制:
class SafeOneLaneBridge(OneLaneBridge):
def __init__(self, max_cars=3):
super().__init__()
self.capacity = threading.Semaphore(max_cars) # 桥上最大车辆数
def eastbound_car(self, car_id):
with self.east_mutex:
self.east_count += 1
if self.east_count == 1:
self.bridge.acquire()
self.capacity.acquire() # 等待桥上空间
print(f"🚗东向车辆{car_id}开始上桥 (东向:{self.east_count}/桥上:{self.capacity._value})")
time.sleep(random.uniform(0.5, 2))
print(f"🚗东向车辆{car_id}已过桥")
self.capacity.release() # 释放桥上空间
with self.east_mutex:
self.east_count -= 1
if self.east_count == 0:
self.bridge.release()
print(f"🚗东向车辆{car_id}离开 (剩余东向:{self.east_count})")
def westbound_car(self, car_id):
with self.west_mutex:
self.west_count += 1
if self.west_count == 1:
self.bridge.acquire()
self.capacity.acquire() # 等待桥上空间
print(f"🚙西向车辆{car_id}开始上桥 (西向:{self.west_count}/桥上:{self.capacity._value})")
time.sleep(random.uniform(0.5, 2))
print(f"🚙西向车辆{car_id}已过桥")
self.capacity.release() # 释放桥上空间
with self.west_mutex:
self.west_count -= 1
if self.west_count == 0:
self.bridge.release()
print(f"🚙西向车辆{car_id}离开 (剩余西向:{self.west_count})")
这个改进版新增了 capacity 信号量来控制桥上最大车辆数。当桥上车辆达到上限时,后续车辆需要等待前面车辆离开才能上桥。
4. 可视化增强:用Tkinter创建图形界面
纯文本输出不够直观,我们可以用Python的Tkinter库创建一个简单的可视化界面:
import tkinter as tk
from tkinter import ttk
class BridgeVisualization:
def __init__(self, bridge):
self.bridge = bridge
self.root = tk.Tk()
self.root.title("独木桥模拟器")
# 创建桥面画布
self.canvas = tk.Canvas(self.root, width=600, height=200, bg='lightblue')
self.canvas.pack(pady=10)
self.canvas.create_rectangle(100, 80, 500, 120, fill='brown', outline='black')
# 状���显示
self.status_label = ttk.Label(self.root, text="等待车辆...", font=('Arial', 12))
self.status_label.pack()
# 控制按钮
self.control_frame = ttk.Frame(self.root)
self.control_frame.pack(pady=10)
ttk.Button(self.control_frame, text="添加东向车辆",
command=lambda: self.add_car('east')).grid(row=0, column=0, padx=5)
ttk.Button(self.control_frame, text="添加西向车辆",
command=lambda: self.add_car('west')).grid(row=0, column=1, padx=5)
# 车辆计数器
self.counter_label = ttk.Label(self.root,
text=f"东向:0 西向:0 桥上:0/{self.bridge.capacity._initial_value}",
font=('Arial', 10))
self.counter_label.pack()
# 存储车辆图形对象
self.car_objects = {}
self.car_positions = {}
self.next_car_id = 0
def add_car(self, direction):
car_id = self.next_car_id
self.next_car_id += 1
color = 'red' if direction == 'east' else 'blue'
if direction == 'east':
car = threading.Thread(target=self.run_east_car, args=(car_id,))
else:
car = threading.Thread(target=self.run_west_car, args=(car_id,))
# 在界面边缘创建车辆
x_pos = 50 if direction == 'east' else 550
car_obj = self.canvas.create_rectangle(x_pos, 90, x_pos+30, 110, fill=color, tags=f"car_{car_id}")
self.car_objects[car_id] = car_obj
self.car_positions[car_id] = x_pos
car.start()
self.update_status(f"{'东' if direction == 'east' else '西'}向车辆{car_id}等待上桥")
def run_east_car(self, car_id):
self.bridge.eastbound_car(car_id)
self.update_car_position(car_id, 'east')
self.canvas.after(100, lambda: self.canvas.delete(self.car_objects[car_id]))
def run_west_car(self, car_id):
self.bridge.westbound_car(car_id)
self.update_car_position(car_id, 'west')
self.canvas.after(100, lambda: self.canvas.delete(self.car_objects[car_id]))
def update_car_position(self, car_id, direction):
step = 5 if direction == 'east' else -5
while ((direction == 'east' and self.car_positions[car_id] < 470) or
(direction == 'west' and self.car_positions[car_id] > 130)):
self.car_positions[car_id] += step
self.canvas.coords(self.car_objects[car_id],
self.car_positions[car_id], 90,
self.car_positions[car_id]+30, 110)
time.sleep(0.05)
def update_status(self, message):
self.status_label.config(text=message)
self.counter_label.config(
text=f"东向:{self.bridge.east_count} 西向:{self.bridge.west_count} "
f"桥上:{self.bridge.capacity._initial_value - self.bridge.capacity._value}/"
f"{self.bridge.capacity._initial_value}")
def run(self):
self.root.mainloop()
# 使用可视化界面运行模拟
if __name__ == "__main__":
bridge = SafeOneLaneBridge(max_cars=3)
gui = BridgeVisualization(bridge)
gui.run()
这个可视化界面可以实时显示:
- 东西方向等待的车辆数量
- 桥上当前车辆数和最大容量
- 车辆移动的动画效果
- 方向切换时的状态变化
5. 扩展到十字路口红绿灯场景
理解了独木桥问题后,我们可以将其扩展到更复杂的十字路口场景。这里我们需要管理四个方向的交通流:
class TrafficIntersection:
def __init__(self):
self.ns_light = threading.Semaphore(1) # 南北方向绿灯
self.ew_light = threading.Semaphore(0) # 东西方向绿灯
self.mutex = threading.Lock() # 保护信号灯切换
self.ns_count = 0 # 南北方向车辆数
self.ew_count = 0 # 东西方向车辆数
def northbound_car(self, car_id):
self.cross_intersection(car_id, 'north')
def southbound_car(self, car_id):
self.cross_intersection(car_id, 'south')
def eastbound_car(self, car_id):
self.cross_intersection(car_id, 'east')
def westbound_car(self, car_id):
self.cross_intersection(car_id, 'west')
def cross_intersection(self, car_id, direction):
# 确定方向组
if direction in ('north', 'south'):
light = self.ns_light
opposite = self.ew_light
with self.mutex:
self.ns_count += 1
else:
light = self.ew_light
opposite = self.ns_light
with self.mutex:
self.ew_count += 1
# 等待绿灯
light.acquire()
print(f"🚗{direction}向车辆{car_id}在绿灯下通过路口")
time.sleep(random.uniform(0.3, 1))
print(f"🚗{direction}向车辆{car_id}已通过")
light.release()
# 检查是否需要切换信号灯
with self.mutex:
if direction in ('north', 'south'):
self.ns_count -= 1
if self.ns_count == 0 and self.ew_count > 0:
self.ns_light.acquire() # 切换为红灯
self.ew_light.release() # 另一方向绿灯
else:
self.ew_count -= 1
if self.ew_count == 0 and self.ns_count > 0:
self.ew_light.acquire() # 切换为红灯
self.ns_light.release() # 另一方向绿灯
这个实现展示了如何将PV操作应用于更复杂的同步场景。信号灯的控制逻辑与独木桥问题一脉相承,但增加了方向组的划分和动态切换机制。
更多推荐
所有评论(0)