Detailed semaphore

2.14 Semaphore (emphasis)

Processes collaborate by passing signals

  • The process is blocked due to a certain condition and cannot continue to advance
  • The process is awakened due to a certain condition and can continue to advance
Integer variable that can realize the function of semaphore: semaphore

Three operations of semaphore

  • Initialization: Initialization is a non-negative number, representing a certain initial state
  • Self-increasing semSignal ( operation code: V ): This operation makes the semaphore +1, if the semaphore is still <=0 at this time, wake up the process blocked by the P operation
  • Self-decrement semWait ( operation code: P ): This operation makes the semaphore -1, if the semaphore is less than 0 at this time, it will block the process of performing the P operation

2.14.1 Semaphore classification

  • Binary semaphore: the semaphore value is 0/1
struct binary_semaphore{
    
    
	enum {
    
     zero, one } value;
	queueType queue;//内置队列
};  
void semwaitB(binary_semaphore s){
    
    //P操作
	if(s.value == one)
		s .value = zero;
	else {
    
    									//此时阻塞
			place this process in s.queue;//当前进程加入阻塞队列
			block this process;//不是忙等,不占cpu时间,等待唤醒
	}
}
void semsignalB(semaphore s){
    
    //V操作
	if( s.queue is empty())
		s .value=one;
	else {
    
    
			remove a process P from s.queue;//从阻塞队列取出进程
			place process P on ready list;//唤醒阻塞进程
	}
}
  • Counting semaphore: the general semaphore, the semaphore has a larger value range
struct semaphore {
    
    
	int count;
	queueType queue;
};

void semwait ( semaphore s ){
    
    //P操作
	s.count--;
	if ( s.count <0 ){
    
    
		place this process in s.queue;
		block this process;
	}
}

void semsignal ( semaphore s){
    
    //V操作
	s.count++;
	if ( s.count <= 0 ){
    
    
		remove a process P from s.queue;
		place process P on ready list;
	}
}

2.14.2 Queue inside the semaphore

  • Strong semaphore: the process is moved out according to FIFO
  • Weak semaphore: does not specify the order in which processes are removed

2.14.3 Use semaphores to solve mutual exclusion problems

When a process cannot perform a P operation, it needs to wait for another process's V operation to achieve mutual exclusion

/* program mutualexclusion */
const int n = / *number of processes */;
semaphore s = 1 ;//初始化

void P(int i){
    
    
	while (true){
    
    
		semwait(s);//P操作:semaphore=1,可以--
		/*访问临界区*/;
		semsignal(s);//V操作:semaphore++
		/*remainder*/;
	}
}

void main (){
    
    
	parbegin (P( 1),P( 2), . . ., P(n) ) ;
}

Insert picture description here

2.14.4 Implementation of Semaphore

  • semWait and semSignal are implemented as primitives
  • Only one process uses PV operation to repair semaphore at any time
  • Can be implemented in hardware
  • Can be implemented using the previous machine instructions
semWait (s){
    
    P操作
	while (compare_and_swap(s.flag,0,1) == 1)//指令作用:s.flag=0?s.flag=1:;循环作用:s.flag=1跳出循环
		/ *do nothing */;
	s.count--;
	if (s.count < 0) {
    
    
		place this process in s.queue;
		block this process (must also set s.flag to 0);
	}
	s.flag = 0;
}

semSignal(s){
    
    //V操作
	while (compare and swap(s.flag, 0,1) == 1)//指令作用:s.flag=0?s.flag=1:;循环作用:s.flag=1跳出循环
		/* do nothing */;
	s.count++;
	if (s.count <= 0){
    
    
		remove a process P from s.queue;
		place process P on ready list;
	}
	s.flag = 0;
}

  • s.count ≥ 0, s.count represents the number of processes that perform the semWait(s) operation without being blocked (can be regarded as the number of available resources). In this case, the semaphore can support synchronization and mutual exclusion.
  • s.count <0, s.count represents the number of processes blocked on the s.queue queue.

Now the semaphore contains a new integer element, s.flag, which, of course, may lead to a new kind of busy waiting. However, semWait and semSignal operations are relatively short, so the amount of busy waiting involved should not be large. For single-processor systems, interrupts can be shielded during the duration of semWait or semSignal operations. The relatively short duration of these operations means that this method is reasonable.

Guess you like

Origin blog.csdn.net/qq_44722674/article/details/111507920