Please can some one briefly tell me with example what does the means of critical section? in simple language
The critical section is a code segment where the shared variables can be accessed. An atomic action is required in a critical section i.e. only one process can execute in its critical section at a time. All the other processes have to wait to execute in their critical sections.
To ensure exclusive use of critical sections some synchronization mechanism is required at the entry and exit of the program. Critical section is a piece of a program that requires mutual exclusion of access.
The critical section refers to the segment of code where processes access shared resources, such as common variables and files, and perform write operations on them. Since processes execute concurrently, any process can be interrupted mid-execution.
Critical sections are sequences of instructions that cannot be interleaved among multiple threads. A simple example of a critical section arises when two threads share a global variable globalvar and both try to change its value with globalvar++ .
A critical section is a section of code that needs to be executed without outside interference - i.e. without another thread potentially affecting/being affected by "intermediate" states within the section.
For instance, a reservation system might have a critical section when reserving something in that it needs to both check to see if the item is available and then mark it as no longer available, without some other attempt at reserving the room changing that status in the middle.
Thus, the critical section of a piece of code is a place where only one thread of execution is allowed to be at a time, to prevent things like race conditions.
A critical section is a section of code where if more than one thread was to be doing it an the same time, they could interfere with each other is such a way as to cause an incorrect result or other malfunction. Imagine a simple bank routine for processing a check:
doCheck (writer, recipient, amount) {
if (amount < 0) return DIAF;
balance = getBalance( writer );
if (balance < amount) return NSF;
setBalance( recipient, getBalance(recipient) + amount );
setBalance( writer, balance - amount );
}
Now imagine my balance is $11,000 and I wrote two checks:
John, ColumbiaHouse, $0.01
John, MrsJohn, $10,000
Now our bank processes so many checks that "ace" programmer DonaldJavaSlump adds threading, because performance -- but he doesn't know about critical sections, so thread 1 starts processing the first check:
if (amount < 0) return DIAF; // (0.01 < 0)? OK
balance = getBalance(John) // $11,000
if (balance < amount) return NSF; // (11,000 < 0.01?) OK
setBalance( ColumbiaHouse, ... // KaChing! ColumbiaHouse gets paid
and now thread 2 starts through the same section of code processing the second check:
if (amount < 0) return DIAF; // (10,000 < 0)? OK
balance = getBalance( John ); // $11,000 (still!)
if (balance < amount) return NSF; // (11,000 < 10,000?) OK
setBalance( MrsJohn, ... // KaChing! MrsJohn gets paid
setBalance( John, balance - amount ); // my balance is now $1,000 :(
and then thread 1 gets around to finishing its job:
setBalance( John, balance - amount ); // now balance is $10,999.99!!! :)
Now John and MrsJohn are extra happy and go on a cruise with free money!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With