Semaphores

Semaphores are global objects that allow you to synchronize the operation of several processes or threads. From the programmer's point of view, a semaphore is simply a global counter that can be manipulated only using special functions. If this counter is equal to N , this means that N processes have access to the resource. Consider functions for working with semaphores in more detail.

  • CreateSemaphore Creates global object, the semaphore, and returns its descriptor. The function accepts the following parameters:

    • First parameterThe pointer to the structure that defines access attributes. It has meaning only for operating systems of the Windows NT family. Usually, it is assumed to be NULL .

    • Second parameterThe initial value of the semaphore counter. It determines how many tasks can initially access the resource.

    • Third parameterThe number of tasks that can simultaneously access the resource.

    • Fourth parameterPointer to the string that contains the semaphore name .

  • OpenSemaphore Opens the existing semaphore and returns the semaphore descriptor. This function is not used frequently. As a rule, a semaphore is created, and its descriptor is assigned to the global variables and then used in the threads being generated. This function accepts the following parameters:

    • First parameterDefines the desired access level to the semaphore. It can take the following values: SEMAPHORE_MODIFY_STATE = 2H , which allows the use of the ReleaseSemaphore function; SYNCHRONIZE = 100000H , which allows the use of any waiting function (Windows NT family only); and SEMAPHORE_ALL_ACCESS = 0F0000h + SYNCHRONIZE + 3H , which specifies all possible access flags to the semaphore.

    • Second parameterSpecifies if the process created by the CreateProcess function can inherit the handle. If this parameter is set to TRUE , then the handle can be inherited; otherwise , the process cannot inherit the handle.

    • Third parameterA pointer to a null- terminated string that names the semaphore to be opened.

  • WaitForSingleObject Waits for the semaphore to open . If this function competes successfully, which means that access to the requested object is provided, it returns zero. The return value 102h means that the waiting period has expired . Parameters of this function are as follows :

    • First parameterSemaphore handle.

    • Second parameterWaiting time in milliseconds . If this parameter is equal to INFINITE = OFFFFFFFFh , the waiting time is unlimited.

  • ReleaseSemaphore Releases the semaphore, thus allowing other processes to gain access to the resource. Parameters of this function are as follows:

    • First parameterSemaphore handle.

    • Second parameterDefines which value must be added to the semaphore counter. Most frequently, this parameter is equal to 1.

    • Third parameterPointer to the variable, into which the previous counter value must be placed.

Consider the algorithm of working with the semaphore. First, create a semaphore using the CreateSemaphore function, and assign its descriptor to the global variable. Before attempting to access the resources, the access to which must be limited, the thread must call the WaitForSingleObject function. When access is provided, the function returns 0. Having completed work with the resource, it is necessary to call the ReleaseSemaphore function. Thus, the access counter is released by 1, and the WaitForSingleObject function, in turn , decreases the counter value. Using semaphores, it is possible to control the number of threads that can simultaneously access the resource. The maximum counter value specifies how many threads can simultaneously access the object. Usually, as I have already mentioned, this value is assumed to be 1.



The Assembly Programming Master Book
The Assembly Programming Master Book
ISBN: 8170088178
EAN: 2147483647
Year: 2004
Pages: 140
Authors: Vlad Pirogov

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net