Windows Kernel Mechanism

Source: Internet
Author: User

Windows kernel objects:

The Windows kernel can process any event in an orderly manner, and how to arrange and run the internal mechanism (including the event mechanism, mutex lock mechanism, semaphore mechanism, and wait mechanism ).

An example of "parking space in a car farm, parking space in a car" is used to explain the meaning of Windows kernel objects,

 

1. Event
N (infinite) Car N (infinite) parking space. For manual event resetting, it is like a parking lot with infinite parking spaces. When the parking lot is open (setevent), all the waiting cars (waiting threads) can go in and stop. When the parking lot is closed (resetevent), all cars (threads) cannot enter.

N (infinite) Car 1 parking space. For automatic event resetting, there is only one parking space. When the parking lot is open (setevent), only one car (thread) goes in and stops. When a car goes in and stops, because only one parking space is full, the parking lot is automatically closed, so other cars must wait. When the car leaves, it must re-open the parking lot (setevent) so that other cars can come in; otherwise, all cars must wait.

 

2. mutex
N (infinite) Car 1 parking space.

It can be regarded as an automatic reset event, although the implementation principle may be different. Since this is one of the cases, it is easier to use.

A car needs to seize the parking space while other cars have to wait. The vehicle must call releasemutex to release the parking space for other vehicles to enter.

Compared with event, mutex can be regarded as a permanently open parking lot (that is, no switch is set ). All cars compete in a fair manner and stop first, but at the same time, only one car can occupy space.

 

3. semaphore
N (infinite) m (limited) parking space. We must clarify whether we are concerned about the number of parking spaces that have been parked or the number of available parking spaces.

When a car arrives, you want to apply for a parking space (releasesemaphore). If you have any remaining parking space, you can apply for a parking space and return the currently used parking space. At this time, 1 will be added to the used parking space.

However, if the remaining parking space is 0, that is to say, because the parking space has been fully stopped, the application will fail, the parking lot will be closed, and the rest will wait.

If you want to free up a parking space, you must remove a car. At this time, you must have a car stopped before the car can be removed (waitforsingleobject); after you remove a car, the remaining parking space will be one more, there will be one fewer parking space.

This kernel object is very poorly designed, its concept is very vague and opposite, and its name is also very vague. For example, waitforsingleobject is reduced, but its judgment condition is whether the parking space already exists, while releasesemaphore is increased, but the judgment condition is whether the remaining empty parking space exists.

In addition, literally, releasesemaphore is definitely a reduction, but it is an increase.

In short, the kernel object is very easy to understand. If we look into it, it will cause people to crash.4. waitforsingleobject
Wait. The running thread can continue only when the conditions are met.

 

4. waitforsingleobject
Wait. The running thread can continue only when the conditions are met.

• The event was established on the condition that it was good to have arrived, the parking lot was open, and there were free parking spaces;
• Mutex is founded on the condition that there are free parking spaces (parking lots are always available );
• Semaphore was founded on the condition that a car was suspended;


5. Applicable scenarios
• Event is used for conditional notifications;
• Mutex is used for mutex competition;
• Semaphore is used to limit the number of reads and writes.

Related Article

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.