AdminDev Labs

Race Conditions

Informações:

Sinopse

## Race Conditions ### Definition - Two or more processes are reading or writing some shared data and the final result depends on who runs precisely when. ### Tanenbaum example (Printer daemon) - Process enters name of file in spooler dir - Printer daemon checks to see if file need printing - Prints and removes names from spooler dir - Spooler dir has 0 ... infinity - Two shared variables, output, point at file to be printed - in, points to free slot in spooler dir - Proc A and B queue file for printing - A reads in, stores slot 7 in variable - Clock interrupt occurs - B reads in, stores slot 7 in same variable - B writes to slot 7, updates in to slot 8 - A writes to slot 7, erasing what B put there, updates in to slot 8 - Spooler dir now in sync - B never receives output ### Golang example (Incrementing a counter) - Proc 1 read counter 0 - Yield thread 0 - Increment counter 1 - Proc 2 read counter 0 - Yield thread 0 - Increment counter 1 - Proc 1 write counter 1 - Proc 2 write counter 1 - Proc 1 read counter