同步与互斥

硬件指令

实现互斥:硬件指令,硬件实现的原子操作,不会被打断
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值