Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Critical section
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Implementation of critical sections == The implementation of critical sections vary among different operating systems. A critical section will usually terminate in finite time,<ref name=":0">{{Cite book |title=GNU/Linux Application Programming (2nd ed.). [Hingham, Mass.] |last=Jones |first=M. Tim |publisher=Charles River Media |year=2008 |isbn=978-1-58450-568-6 |pages=264}}</ref> and a thread, task, or process must wait for a fixed time to enter it ([[Peterson's algorithm#Bounded waiting|bounded waiting]]). To ensure exclusive use of critical sections, some synchronization mechanism is required at the entry and exit of the program. A critical section is a piece of a program that requires [[mutual exclusion]] of access. [[File:Locks and critical sections.jpg|thumb|381x381px|Locks and critical sections in multiple threads]] As shown in the figure,<ref>{{Cite book |last=Chen, Stenstrom |first=Guancheng, Per |title=2012 International Conference for High Performance Computing, Networking, Storage and Analysis |chapter=Critical lock analysis: Diagnosing critical section bottlenecks in multithreaded applications |date=Nov 10β16, 2012 |pages=1β11 |doi=10.1109/sc.2012.40 |isbn=978-1-4673-0805-2|s2cid=12519578 }}</ref> in the case of mutual exclusion ([[mutex]]), one thread blocks a critical section by using locking techniques when it needs to access the shared resource, and other threads must wait their turn to enter the section. This prevents conflicts when two or more threads share the same memory space and want to access a common resource.<ref name=":0" /> [[File:Critical section pseudo code.png|thumb|289x289px|Pseudocode for implementing critical section]] The simplest method to prevent any change of processor control inside the critical section is implementing a semaphore. In uniprocessor 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. With this implementation, any execution thread entering any critical section in the system will prevent any other thread, including an interrupt, from being granted processing time on the CPU until the original thread leaves its critical section. This brute-force approach can be improved by using [[Semaphore (programming)|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. Semaphore locking also has a time limit to prevent a deadlock condition in which a lock is acquired by a single process for an infinite time, stalling the other processes that need to use the shared resource protected by the critical section.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)