硬件指令
实现互斥:硬件指令,硬件实现的原子操作,不会被打断
tsl指令和xchg指令
当前指令执行完,才会检测中断
If the signal comes while an instruction is being executed, it is held until the execution of the instruction is complete.
At the end of each instruction, before the next instruction is fetched, the CPU checks to see if any interrupt requests
are pending.
Hardware interrupts are typically actioned at the end of the current CPU instruction being executed.
The specification of the Intel i486 for example is very clear.The processor only responds to interrupts between instructions.
After the execution of the current instruction,the processor verifies the interrupt signal to check whether
any interrupt is pending.
If no interrupt is pending then the processor proceeds to fetch the next instruction in the sequence.
If the processor finds the pending interrupts,it suspends the execution of the current program by saving the address
of the next instruction that has to be executed and it updates the program counter with the starting address of
the interrupt service routine to service the occurred interrupt.
信号量
信号量机制:信号量S(整型变量),P/V操作
P(S):
含义:若S<=0,则死循环一直判断S和0;S>0,则S=S-1
V(S):
含义:S=S+1
P/V操作是两条原语
P中的死循环while可以被中断(S-1的那部分逻辑是原子的)
有的地方这里的实现不是死循环,而是挂起加入了等待队列(变为阻塞态)(这时整个P操作都是原子的)
原语:完成某种功能且不能被分割,打断的执行序列,是原子的
信号量用于互斥:
令信号量mutex=1
P(mutex)
临界段
V(mutex)
信号量用于同步:
令信号量sync=0
P1进程执行: S1;V(sync);
P2进程执行: P(sync);S2;
这样就保证了P2进程的S2操作一定在P1进程的S1操作之后执行
interrupt
interrupt
interrupt
interrupt
https://zhidao.baidu.com/question/544877944.html
semaphore