In concurrent programming, a critical section is a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution. A critical section will usually terminate in fixed time, and a thread, task, or process will have to wait a fixed time to enter it (aka bounded waiting). Some synchronization mechanism is required at the entry and exit of the critical section to ensure exclusive use, for example a semaphore.
By carefully controlling which variables are modified inside and outside the critical section, concurrent access to that state is prevented. A critical section is typically used when a multithreaded program must update multiple related variables without a separate thread making conflicting changes to that data. In a related situation, a critical section may be used to ensure a shared resource, for example a printer, can only be accessed by one process at a time.
How critical sections are implemented varies among operating systems.
The simplest method is to prevent any change of processor control inside the critical section. On uni-processor systems, this can be done by disabling interrupts on entry into the critical section, avoiding system calls that can cause a context switch while inside the section, and restoring interrupts to their previous state on exit. Any thread of execution entering any critical section anywhere in the system will, with this implementation, prevent any other thread, including an interrupt, from being granted processing time on the CPU - and therefore from entering any other critical section or, indeed, any code whatsoever - until the original thread leaves its critical section.
This brute-force approach can be improved upon by using semaphores. To enter a critical section, a thread must obtain a semaphore, which it releases on leaving the section. Other threads are prevented from entering the critical section at the same time as the original thread, but are free to gain control of the CPU and execute other code, including other critical sections that are protected by different semaphores.
Some confusion exists in literature about the relationship between different critical sections in the same program. Clearly, a resource that must be protected from concurrent access must be accessible by several entry points in the code. If the resource were only accessible through a single entry point, then multithreaded processing would not be a necessary consideration. Each resource must be guarded by a common, "global" semaphore. Is each piece now a critical section, or are all the pieces guarded by the same semaphore in aggregate a single critical section? This confusion is evident in definitions of a critical section such as "... a piece of code that can only be executed by one process or thread at a time".
Other articles related to "critical section, section, critical sections":
... A job then cannot preempt a lower priority critical section if its priority is lower than the ceiling priority for that section ... that is, an almost arbitrarily long delay from the time a high priority task requests a critical section, until it is served ... guarantee that at most one lower priority critical section must finish before the high priority task gets its critical section ...
... Typically, critical sections prevent process and thread migration between processors and the preemption of processes and threads by interrupts and other processes and threads ... Critical sections often allow nesting ... Nesting allows multiple critical sections to be entered and exited at little cost ...
... If they are the same, the thread is permitted to enter the critical section ... If they are not the same, then another thread must already be in the critical section and this thread must busy-wait or yield ... When a thread leaves the critical section controlled by the lock, it atomically increments the dequeue ticket ...
... request, then site sends an message to the process which has currently been granted access to the critical section by site ... Critical section Site enters the critical section on receiving a message from all sites in ... Upon exiting the critical section, sends a message to all sites in ...
Famous quotes containing the words section and/or critical:
“Socialite women meet socialite men and mate and breed socialite children so that we can fund small opera companies and ballet troupes because there is no government subsidy.”
—Sugar Rautbord, U.S. socialite fund-raiser and self-described trash novelist. As quoted in The Great Divide, book 2, section 7, by Studs Terkel (1988)
“You took my heart in your hand
With a friendly smile,
With a critical eye you scanned,
Then set it down,
And said: It is still unripe,
Better wait awhile;”
—Christina Georgina Rossetti (18301894)