Re: "Semaphore" - Was ist das?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Marcel Valentin (marcel.valentin_at_de.bosch.com)
Date: 06/18/04


Date: Fri, 18 Jun 2004 11:39:39 +0200


> Hallo,
> wollte grad eine Datei oeffnen, da bekomme ich die
> Fehlermeldung:
> "Die Semaphore kann nicht (erneut)gesetzt werden. I/O
> error 103"
>
> Was ist das?

Auszug aus dem MSDN, allerdings in Englisch:

Error 103: The semaphore cannot be set again.

Semaphore Objects
A semaphore object is a synchronization object that maintains a count
between zero and a specified maximum value. The count is decremented each
time a thread completes a wait for the semaphore object and incremented each
time a thread releases the semaphore. When the count reaches zero, no more
threads can successfully wait for the semaphore object state to become
signaled. The state of a semaphore is set to signaled when its count is
greater than zero, and nonsignaled when its count is zero.

The semaphore object is useful in controlling a shared resource that can
support a limited number of users. It acts as a gate that limits the number
of threads sharing the resource to a specified maximum number. For example,
an application might place a limit on the number of windows that it creates.
It uses a semaphore with a maximum count equal to the window limit,
decrementing the count whenever a window is created and incrementing it
whenever a window is closed. The application specifies the semaphore object
in call to one of the wait functions before each window is created. When the
count is zero - indicating that the window limit has been reached - the wait
function blocks execution of the window-creation code.

A thread uses the CreateSemaphore function to create a semaphore object. The
creating thread specifies the initial count and the maximum value of the
count for the object. The initial count must be neither less than zero nor
greater than the maximum value. The creating thread can also specify a name
for the semaphore object. Threads in other processes can open a handle to an
existing semaphore object by specifying its name in a call to the
OpenSemaphore function. For additional information about names for mutex,
event, semaphore, and timer objects, see Interprocess Synchronization.

Threads that are waiting for a semaphore are placed in a first in, first out
(FIFO) queue. Therefore, the first thread to wait on the semaphore will be
the first to successfully complete the wait, regardless of thread priority.
However, kernel-mode APCs and events that suspend a thread from waiting will
cause the system to remove the thread from the queue. When the thread
resumes its wait for the semaphore, it is placed at the end of the queue.
Each time one of the wait functions returns because the state of a semaphore
was set to signaled, the count of the semaphore is decreased by one. The
ReleaseSemaphore function increases a semaphore's count by a specified
amount. The count can never be less than zero or greater than the maximum
value.
The initial count of a semaphore is typically set to the maximum value. The
count is then decremented from that level as the protected resource is
consumed. Alternatively, you can create a semaphore with an initial count of
zero to block access to the protected resource while the application is
being initialized. After initialization, you can use ReleaseSemaphore to
increment the count to the maximum value.

A thread that owns a mutex object can wait repeatedly for the same mutex
object to become signaled without its execution becoming blocked. A thread
that waits repeatedly for the same semaphore object, however, decrements the
semaphore's count each time a wait operation is completed; the thread is
blocked when the count gets to zero. Similarly, only the thread that owns a
mutex can successfully call the ReleaseMutex function, though any thread can
use ReleaseSemaphore to increase the count of a semaphore object.

A thread can decrement a semaphore's count more than once by repeatedly
specifying the same semaphore object in calls to any of the wait functions.
However, calling one of the multiple-object wait functions with an array
that contains multiple handles of the same semaphore does not result in
multiple decrements.



Relevant Pages

  • Re: The worst conforming pthread implementation
    ... > location concerned with particular semaphore object (presumably atomic ... > semaphore object must be in total order. ... I think that it must execute at least acquire fence. ...
    (comp.programming.threads)
  • Re: Use Semaphore or mutex between kernel and user-mode ?
    ... to a semaphore object through IOCTL. ... Your kernel mode code then does ObReferenceObjectByHandle(make sure to ... object at which point you can use Ke semaphore functions. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: The worst conforming pthread implementation
    ... location concerned with particular semaphore object (presumably atomic ... RMW operation with embedded acquire_release fence). ... semaphore object must be in total order. ... I think that it must execute at least acquire fence. ...
    (comp.programming.threads)
  • RE: Use Semaphore or mutex between kernel and user-mode ?
    ... For semaphore one way would be for a user mode program to pass you a handle ... to a semaphore object through IOCTL. ... FastMutext is not available to user mode. ... "Thierry" wrote: ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Is there a way to get the current semaphore count?
    ... Is there a way to get the semaphore ... single exception to this and make a call with a release count of zero ... for a thread, then if the message count exceeds some threshold, the ... message receiving thread could speeds up it's message processing by ...
    (microsoft.public.vc.language)