week 3 of teaching - inter-process communication (busy waiting)

IPC (Inter process communication)

race conditions

over the years we have developed a few techniques to avoid race conditions

mutual exclusion

race condition solution requirements:

  1. no no two process must be inside their critical region at the same time
  2. must make no assumptions about clock speed or core count
  3. no process outside of critical region can block other processes from doing so
  4. no process should have to wait forever to access it’s critical region

potential known solutions

busy waiting

lock variables

this has a critical issue as if the scheduler switches, just after one process has read the lock state, to the other process. both processes will have read lock as 0 and will be free to both entire their critical regions at the same time.

strict alternation

assuming alternation of processes

process 0 process 1
while (true){
    while(turn != 0) /* loop */;
    critical_region();
    turn = 1;
    non_critical_region();
}
while (true){
    while(turn != 1) /* loop */;
    critical_region();
    turn = 0;
    non_critical_region();
}

peterson’s algorithm

this example does not assume alternation and although is implemented here for two processes, a solution for N processes does exist

#define FALSE 0;
#define TRUE 1;

#define N 2;

int turn;
int interested[N];

void enter_region(int process){
    int other;

    other = 1 - process;
    interested[process] = TRUE;
    turn = process;
    while(turn == process && interested[other] == TRUE);
}

void leave_region(int process){
    interested[process] = FALSE
}

hardware solution

enter_region:
    TSL REGISTER,LOCK
    CPM REGISTER, #0
    JNE enter_region
    RET

leave_region:
    MOVE LOCK, #0
    RET

Back to notes...

click here to go back to more