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!
== Need for critical sections == Different code or processes may consist of the same variable or other resources that must be read or written but whose results depend on the order in which the actions occur. For example, if a variable {{var|x}} is to be read by process A, and process B must write to the variable {{var|x}} at the same time, process A might get either the old or new value of {{var|x}}. [[File:Critical section fg.jpg|thumb|Flow graph depicting need for critical section]] '''Process A:''' <syntaxhighlight lang="c"> // Process A . . b = x + 5; // instruction executes at time = Tx . </syntaxhighlight> '''Process B:''' <syntaxhighlight lang="c"> // Process B . . x = 3 + z; // instruction executes at time = Tx . </syntaxhighlight> In cases where a [[Lock (computer science)|locking]] mechanism with finer granularity is not needed, a critical section is important. In the above case, if A needs to read the updated value of {{var|x}}, executing process A, and process B at the same time may not give required results. To prevent this, variable {{var|x}} is protected by a critical section. First, B gets the access to the section. Once B finishes writing the value, A gets the access to the critical section, and variable {{var|x}} can be read. By carefully controlling which variables are modified inside and outside the critical section, concurrent access to the shared variable are prevented. A critical section is typically used when a multi-threaded 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 that a shared resource, for example, a printer, can only be accessed by one process at a time.
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)