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!
==Uses of critical sections == === Kernel-level critical sections === Typically, critical sections prevent thread and [[process migration]] between processors and the [[Preemption (computing)|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 the [[Scheduling (computing)|scheduler]] interrupts the current process or thread in a critical section, the scheduler will either allow the currently executing process or thread to run to completion of the critical section, or it will schedule the process or thread for another complete quantum. The scheduler will not migrate the process or thread to another processor, and it will not schedule another process or thread to run while the current process or thread is in a critical section. Similarly, if an [[interrupt]] occurs in a critical section, the interrupt information is recorded for future processing, and execution is returned to the process or thread in the critical section.{{cn|date=April 2025}} Once the critical section is exited, and in some cases the scheduled quantum completed, the pending interrupt will be executed. The concept of scheduling quantum applies to "[[Round-robin scheduling|round-robin]]" and similar [[Scheduling (computing)|scheduling policies]]. Since critical sections may [[Execution (computers)|execute]] only on the processor on which they are entered, synchronization is only required within the executing processor. This allows critical sections to be entered and exited at almost no cost. No inter-processor synchronization is required. Only instruction stream synchronization is needed.<ref>{{Cite journal |last=Dubois, Scheurich |first=Michel, Christoph |year=1988 |title=Synchronization, Coherence, and Event Ordering in Multiprocessors |journal=Survey and Tutorial Series |volume=21 |issue=2 |pages=9–21 |doi=10.1109/2.15 |s2cid=1749330}}</ref> Most processors provide the required amount of synchronization by interrupting the current execution state. This allows critical sections in most cases to be nothing more than a per processor count of critical sections entered. Performance enhancements include executing pending interrupts at the exit of all critical sections and allowing the scheduler to run at the exit of all critical sections. Furthermore, pending interrupts may be transferred to other processors for execution. Critical sections should not be used as a long-lasting locking primitive. Critical sections should be kept short enough so they can be entered, executed, and exited without any interrupts occurring from the [[Computer hardware|hardware]] and the scheduler. Kernel-level critical sections are the base of the [[software lockout]] issue. === Critical sections in data structures === In parallel programming, the code is divided into threads. The [[Read–write conflict|read-write conflicting]] variables are split between threads and each thread has a copy of them. Data structures such as [[linked list]]s, [[Tree (data structure)|trees]], and [[hash table]]s have data variables that are linked and cannot be split between threads; hence, implementing parallelism is very difficult.<ref name=":1">{{Cite book|title=Fundamentals of Parallel Multicore Architecture|last=Solihin|first=Yan|date=17 November 2015|publisher=Taylor & Francis |isbn=9781482211184}}</ref> To improve the efficiency of implementing data structures, multiple operations such as insertion, deletion, and search can be executed in parallel. While performing these operations, there may be scenarios where the same element is being searched by one thread and is being deleted by another. In such cases, the output may be [[Erroneous program|erroneous]]. The thread searching the element may have a hit, whereas the other thread may subsequently delete it. These scenarios will cause issues in the program running by providing false data. To prevent this, one method is to keep the entire data structure under critical section so that only one operation is handled at a time. Another method is locking the node in use under critical section, so that other operations do not use the same node. Using critical section, thus, ensures that the code provides expected outputs.<ref name=":1" /> === Critical sections in relation to peripherals === Critical sections also occur in code which manipulates external peripherals, such as I/O devices. The registers of a peripheral must be programmed with certain values in a certain sequence. If two or more processes control a device simultaneously, neither process will have the device in the state it requires and incorrect behavior will ensue. When a complex unit of information must be produced on an output device by issuing multiple output operations, exclusive access is required so that another process does not corrupt the datum by interleaving its own bits of output. In the input direction, exclusive access is required when reading a complex datum via multiple separate input operations. This prevents another process from consuming some of the pieces, causing corruption. Storage devices provide a form of memory. The concept of critical sections is equally relevant to storage devices as to shared data structures in main memory. A process which performs multiple access or update operations on a file is executing a critical section that must be guarded with an appropriate file locking mechanism.
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)