Kbase 15667: How PROGRESS uses semaphores
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  10/05/1998 |
|
How PROGRESS uses semaphores
What is a semaphore?
A semaphore is a counter used to provide access to a shared data
object for multiple processes. A semaphore can have a value of either
1 or 0.
How does it work?
Processes use semaphores to wait for and obtain locks on any shared
resources they need to access. A process decrements a semaphore to
lock it and blocks against another process from decrementing and
locking. When the lock is released, the process increments the
semaphore to release it. The process that was blocked will then be
able to decrement their semaphore and obtain a lock. Multiple
processes waiting or sleeping on a semaphore are said to be in the
queue. If multiple processes are "blocked", the operating system
unblocks or wakes up all the processes that are waiting in the queue
or sleeping at once. Whichever process wakes up and decrements their
semaphore first is the process that will obtain the lock. The way
PROGRESS uses semaphores, there is never more than one process blocked
at one time, except for the login/logout semaphore.
What do we use semaphores for?
1> Long term waits - These include waits on record locks (RECD),
schema locks (SCH) and transaction end (TRAN). These waits are
cancellable. The user will receive a message that the record is
in use and they have the choice of waiting or pressing Ctrl-C.
2> Medium term internal resource waits - These include waits for a
database, after image or before image buffer when that buffer is in
use. This wait is not cancellable.
3> Latch waits - These are waits to lock data structures in shared
memory on single processor systems. This wait is less than a
millisecond. On multi-processor systems, spinlocks are used in
place of semaphores by setting the database startup option -spin.
A single processor machine can also use -spin 1 to reduce latch
contention.
4> Login waits - The database login and logout process is controlled
by using a semaphore. A user decrements the semaphore, locks the
entire user table, a user number is allocated to that user, the
user table is unlocked and the semaphore is incremented for the
next user.
What is the internal sequence of events to lock a record?
1> Lock the lock table (data structure)
2> Insert your entry in the table (queue yourself)
3> Unlock the lock table
4> Wait on your semaphore. This semaphore is initialized to 0. The
process tries to decrement it, but it is already at 0. This is
a "blocked" process. The current lock holder is responsible for
incrementing the semaphore for the next process in the queue. Once
the semaphore is incremented to 1, the waiting process will succeed
on its next attempt to decrement the semaphore and will obtain the
lock.
5> Lock the lock table
6> Release the resource
7> Increment the semaphore of the next waiting process, if any exists.
8> Unlock the lock table
24-Apr-96
LJF
Progress Software Technical Support Note # 15667