Mutual Exclusion with Busy Waiting

来源:互联网 发布:淘宝助理登陆失败 编辑:程序博客网 时间:2024/05/20 21:44

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 enteredleave_region:       MOVE LOCK,#0             |store a 0 in LOCK       RET                      |return to caller
原创粉丝点击