对于信号量实现,过程指定了什么?在生产者/消费者问题的背景下,过程是生产者方法/消费者方法吗?或者如果我们在P()中并且该值小于0,那么它是否是P()?混淆信号量定义
P() {
value = value –1;
If value < 0
add the calling process to this semaphore’s list;
block this process
}
例 如果消费者运行第一生产者产生其第一个项目之前
消费者将递减的全部价值 - >满= -1 然后因为该值小于1,这将增加该调用进程到这个信号量的列表。但我不确定过程是什么。 阻止这个过程是什么意思?这是否意味着消费者的整个方法处于停止状态,并且生产者方法运行?
代码:
#define N 100
typedef int semaphore;
Semaphore fullBuffer = 0; // Initially, no item in buffer
Semaphore empty = N; // Initially, num empty buffer
Semaphore mutex = 1; // No thread updating the buffer
void producer(void) {
int item;
while(TRUE){
item = produce_item();
down(&empty);
down(&mutex);
insert_item(item);
up(&mutex);
up(&full);
}
}
void consumer(void) {
int item;
while(TRUE){
down(&full);
down(&mutex);
item = remove_item();
up(&mutex);
up(&empty);
consume_item(item);
}
}
因此,消费者一旦达到障碍声明就会“停止”,然后生产者将运行? – 2014-10-12 07:35:42
只有'完整'信号量已经为零,或'生产者'已经将'互斥量'减少到零时才有效。生产者可能已经在运行,但它不能被阻止,因为如果'消费者'被阻塞,那么它被阻塞,因为它等待'生产者'增加信号量,这将允许它再次运行。 '生产者'和'消费者'都可以同时运行。 – SimLeek 2014-10-12 18:36:35