Mutually exclusive access to critical resources by the process

Source: Internet
Author: User

Critical resources and critical areas

Critical resource (Critical Resource): A resource that can be used only by one process at a time. Such as: Hardware has a printer, software has variables, disk files (when writing).

Critical Zone (critical section): The code that accesses the critical resource in the process becomes the critical section.

In order to achieve mutually exclusive access to the critical resource, the mutually exclusive access of the process to the critical resource can be realized as long as the process is mutually exclusive to its own critical section.

Synchronization mechanism

To enable each process to access its own critical section, the operating system needs a synchronization mechanism to coordinate the operation of each process.

1. Rules of the synchronization mechanism

(1) Idle let in: when no process is in the critical section, indicating that the critical resource is idle, allowing the request to enter the critical section of the process into the critical section

(2) Busy wait: when there is a process in the critical section, indicating that critical resources are being accessed, other requests to enter the critical section of the process must wait.

(3) Limited wait: The waiting process should be allowed to enter its own critical section within a limited number of events, avoiding the "death" state

(4) The right to wait: When the process cannot enter its own critical section, should immediately release the processor to avoid entering the "busy" state

2. Hardware synchronization mechanism

A lock is set for a critical resource, and when no process is used for the critical resource, the lock is open and the lock is closed when the critical resource is being used by the process.

The lock may be a Boolean variable lock, initially, lock = FALSE, and the process lock test is required each time the critical resource is accessed.

BOOL Lock false ;     if (! Lock )    {        locktrue;        Access critical section; // Access critical section resource    }

However, errors can occur in multitasking machines. Assuming that 2 processes have access to the same critical resource, process a passes the lock test, but when lock is not set to true, process scheduling occurs, process B starts the lock test, and the lock test is passed, allowing two processes to access the same critical resource.

(1) off interrupt: Closes the interrupt before entering the lock test until the lock test is complete and the interrupt is opened after locking.

(2) using the Test-and-set directive: This is a hardware instruction, and the directive is an atomic operation (atomic operataion), atomic manipulation refers to the execution of the process is either not done, then all done, that is, like an atom indivisible. The following TS is an atomic operation, so the above-mentioned problem does not occur.

 bool  TS (bool  *lock ) // { bool   old;    Old  = lock  ;  lock  = true ; //  return   old;}  
 while (TS (&lock)); // Loop test until TS (&lock) is false access critical section; Lock false;

(3) using the SWAP directive: The directive is also a hardware directive and is atomic. You can see that the following code actually thought is the same as the above code thought. It's just a different way of achieving it.

void swap (boolBOOL *b) {    // if a==true, then the swap statement does not affect the lock if a==false , then just lock    bool  tmp    = *A;     *a = *b;     *b = tmp;}
BOOL true ;  Do {    swap (&lock,&key);}  while (key!=false)

3. Semaphore mechanism

Mutually exclusive access to critical resources by the process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.