/*----------------------------------------------------- Script for AdSense -----------------------------------------------------*/ /* */ /* Footer ----------------------------------------------- */ #footer { clear: both; text-align: center; color: #333333; } #footer .widget { margin:.5em; padding-top: 20px; font-size: 85%; line-height: 1.5em; text-align: left; } /** Page structure tweaks for layout editor wireframe */ body#layout #header { width: 750px; } -->

Friday, April 23, 2010

Thread Synchronization With Kernel Objects : 3

After Events, Now we will discuss Semaphore and Mutex Kernel Objects.

Semaphore Kernel Objects
Semaphore kernel objects are used for resource counting.

Let's say that I'm developing a server process in which I have allocated a buffer that can hold client requests. I've hard-coded the size of the buffer so that it can hold a maximum of five client requests at a time. If a new client attempts to contact the server while five requests are outstanding, the new client is turned away with an error indicating that the server is busy and the client should try again later. When my server process initializes, it creates a thread pool consisting of five threads, each thread ready to process individual client requests as they come in.

Initially, no clients have made any requests, so my server doesn't allow any of the threads in the pool to be schedulable. However, if three client requests come in simultaneously, three threads in the pool should be schedulable. You can handle this monitoring of resources and scheduling of threads very nicely using a semaphore: the maximum resource count is set to 5 since that is the size of my hard-coded buffer. The current resource count is initially set to 0 since no clients have made any requests. As client requests are accepted, the current resource count is incremented

The rules for a semaphore are as follows:

1) If the current resource count is greater than 0, the semaphore is signaled.

2) If the current resource count is 0, the semaphore is nonsignaled.

3) The system never allows the current resource count to be negative.

4) The current resource count can never be greater than the maximum resource count.

Win32 APIs for semaphore object.

CreateSemaphore()//.....//API to create semaphore object.
OpenSemaphore()//.....// open semaphore object.
ReleaseSemaphore()//......//Release semaphore object.

Mutex kernel Object
Mutex kernel objects ensure that a thread has mutual exclusive access to a single resource.
A mutex object contains a usage count, a thread ID, and a recursion counter. Mutexes behave identically to critical sections, but mutexes are kernel objects, while critical sections are user-mode objects. This means that mutexes are slower than critical sections.
thread can specify a timeout value while waiting to gain access to a resource.

The thread ID identifies which thread in the system currently owns the mutex,
and the recursion counter indicates the number of times that this thread owns the mutex.Typically, they are used to guard a block of memory that is accessed by multiple threads.

The rules for a mutex are as follows:

1) If the thread ID is 0 (an invalid thread ID), the mutex is not owned by any thread and is signaled.

2) If the thread ID is nonzero, a thread owns the mutex and the mutex is nonsignaled.

Win32 APIs for Mutex Kernel Object.
CreateMutex()//.......API to create mutex object.

OpenMutex()//......API to open mutex object.

ReleaseMutex()//....API to release mutex object.

No comments: