进程同步和PV操作理解
首先用python模拟了生产者消费者问题,代码如下:
import threading
import random
BUFFER_SIZE = 10
buffer = [[]] * 10
counter = 0
in_ = 0
out_ = 0
item = 0
def productor():
global in_, buffer, counter, item
while 1:
# 发信号停
while counter == BUFFER_SIZE:
print("productor wait...\n")
item = random.randint(1, 1000)
buffer[in_].append(item)
print("product {}\n".format(item))
print("product counter {}\n".format(counter))
in_ = (in_+1) % BUFFER_SIZE
# 发信号走
counter += 1
def customer():
global out_, buffer, counter, item
while 1:
# 发信号停
while counter == 0:
print("customer wait...\n")
item = buffer[out_].pop()
print("custom {}\n".format(item))
print("custom counter {}\n".format(counter))