<< Chapter < Page | Chapter >> Page > |
A race condition occurs when two (or more) processes are about to perform some action. Depending on the exacttiming, one or other goes first. If one of the processes goes first, everything works, but if another one goes first, an error, possibly fatal, occurs.
Imagine two processes both accessing x, which is initially 10.
We must prevent interleaving sections of code that need to be atomic with respect to each other. That is, the conflicting sectionsneed mutual exclusion . If process A is executing its critical section, it excludes process B from executing its critical section.Conversely if process B is executing is critical section, it excludes process A from executing its critical section.
Requirements for a critical section implementation.
The operating system can choose not to preempt itself. That is, we do not preempt system processes (if the OS is client server)or processes running in system mode (if the OS is self service). Forbidding preemption for system processes would prevent the problem above where x x+1 notbeing atomic crashed the printer spooler if the spooler is part of the OS.
But simply forbidding preemption while in system mode is not sufficient.
Does not work if the system has several processors.
Software solutions for two processes
Initially P1wants=P2wants=false
Code for P1 Code for P2
Loop forever { Loop forever {
P1wants = true ENTRY P2wants = true
while (P2wants) {} ENTRY while (P1wants) {}
critical-section critical-section
P1wants = false EXIT P2wants = false
non-critical-section } non-critical-section }
Notification Switch
Would you like to follow the 'Operating systems' conversation and receive update notifications?