Show how counting semaphores (i.e., semaphores that can hold an arbitrary value) can be implemented using only binary semaphores and ordinary machine instructions.
译:
如何用二值信号量和一些机器指令实现计数信号量。
Answer:
Associated with each counting semaphore are two binary semaphores, M ,used for mutual exclusion, and B , used for blocking. Also associated with each counting semaphore is a counter that holds the number of ups minus the number of downs, and a list of processes blocked on that semaphore. To implement down, a process first gains exclusive access to the semaphores, counter, and list by doing a down on M . It then decrements the counter. If it is zero or more, it just does an uponM and exits. If M is negative, the process is put on the list of blocked processes. Then an upis done on M and a down is done on B to block the process. To implement up, first M is downed to get mutual exclusion, and then the counter is incremented. Ifit is more than zero, no one was blocked, so all that needs to be done is to up M. If, however, the counter is now negative or zero, some process must be removed from the list. Finally, an upis done on B and M in that order.
答案译文:
用两个二值信号量和一个计数器counter实现一个计数信号量:M用于互斥,B用于阻塞,counter用于记录up减去down的次数,再用一个链表来记录阻塞在这个计数信号量上的进程。
down的实现:进程先对M进行down来获得counter、链表的独占访问权,并把counter减1。如果counter大于等于0,直接对M进行up即可;否则,记录在链表再up,然后对B进行down从而阻塞这个进程。
up的实现:进程同样先对M进行down,counter加1,若其大于0,直接对M进行up即可;否则counter小于等于0,把链表中一个进程移出,然后对B、M依次up。
Data Structure:
binary-semaphore M,B;
int counter;
Initalization:
M = 1;
B = 0;
Wait Operation:
wait(M);
counter--;
if(counter < 0)
{
signal(M);
wait(B);
}
signal(M);
Signal Operation:
wait(M);
counter++;
if(counter <= 0)
signal(B);
else
signal(M);
译:
如何用二值信号量和一些机器指令实现计数信号量。
Answer:
Associated with each counting semaphore are two binary semaphores, M ,used for mutual exclusion, and B , used for blocking. Also associated with each counting semaphore is a counter that holds the number of ups minus the number of downs, and a list of processes blocked on that semaphore. To implement down, a process first gains exclusive access to the semaphores, counter, and list by doing a down on M . It then decrements the counter. If it is zero or more, it just does an uponM and exits. If M is negative, the process is put on the list of blocked processes. Then an upis done on M and a down is done on B to block the process. To implement up, first M is downed to get mutual exclusion, and then the counter is incremented. Ifit is more than zero, no one was blocked, so all that needs to be done is to up M. If, however, the counter is now negative or zero, some process must be removed from the list. Finally, an upis done on B and M in that order.
答案译文:
用两个二值信号量和一个计数器counter实现一个计数信号量:M用于互斥,B用于阻塞,counter用于记录up减去down的次数,再用一个链表来记录阻塞在这个计数信号量上的进程。
down的实现:进程先对M进行down来获得counter、链表的独占访问权,并把counter减1。如果counter大于等于0,直接对M进行up即可;否则,记录在链表再up,然后对B进行down从而阻塞这个进程。
up的实现:进程同样先对M进行down,counter加1,若其大于0,直接对M进行up即可;否则counter小于等于0,把链表中一个进程移出,然后对B、M依次up。
Data Structure:
binary-semaphore M,B;
int counter;
Initalization:
M = 1;
B = 0;
Wait Operation:
wait(M);
counter--;
if(counter < 0)
{
signal(M);
wait(B);
}
signal(M);
Signal Operation:
wait(M);
counter++;
if(counter <= 0)
signal(B);
else
signal(M);