Mutual Exclusion with Busy Waiting

Disabling Interrupts

  a useful technique within the operating system itself but is not appropriate as a general mutual exclusion mechanism for user processes 建议在操作系统内部使用

Lock Variables

   建议不使用

Strict Alternation

 

while (TRUE){                              while (TRUE) {
    while(turn != 0)       /* loop* /;         while(turn != 1)       /* loop* /;
    critical_region();                         critical_region();
    turn  = 1;                                 turn  = 0;
    noncritical_region();                      noncritical_region();
}                                          }
                 (a)                                        (b)

  如果两个进程的运行速度相差较多,则不合适。

A lock that uses busy waiting is called a spin lock.

Peterson's Solution

结合了 lock variable 和 strict alternation的软件实现mutual exclusion的方法。

#define FALSE 0
#define TRUE  1
#define N     2                      /* number of processes */

int turn;                            /* whose turn is it? */
int interested[N];                   /* all values initially 0 (FALSE)*/
void enter_region(int process)       /* process is 0 or 1 */
{
     int other;                      /* number of the other process */
     other = 1 - process;            /* the opposite of process */
     interested[process] = TRUE;     /* show that you are interested */
     turn = process;                 /* set flag */
     while (turn == process && interested[other] == TRUE) /* null statement */;
}
void leave_region(int process)       /* process: who is leaving */
{
     interested[process] = FALSE;    /* indicate departure from critical region */
}

The TSL Instruction
a proposal that requires a little help from the hardware
enter_region:
        TSL REGISTER,LOCK       |copy LOCK to register and set LOCK to 1
        CMP REGISTER,#0         |was LOCK zero?
        JNE ENTER_REGION        |if it was non zero, LOCK was set, so loop
        RET                     |return to caller; critical region entered


leave_region:
       MOVE LOCK,#0             |store a 0 in LOCK
       RET                      |return to caller
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值