Semaphore
Semaphore
• One character is read from the keyboard one keystroke at a time, stored
in variable chin.
•And sent out to the screen for display.
Let’s assume, two overlapping processes P1 and P2 use simple routine.
Process P1 Process P2
. .
chin=getchar(); .
. chin=getchar();
chout=chin; chout=chin;
putchar(chout); .
. putchar(chout);
P1’s input is lost; for both P1 and P2 the same character is sent to the screen.
Race Condition
Race condition: The situation where several
processes access – and manipulate shared
data concurrently. The final value of the
shared data depends upon which process
finishes last.
Example:
Process A wants to write
to the directory, 7 read into
its local variable
Interrupt
Process B reads in 7,
writes to slot 7, updates in
to 8
Process A resumes, also
writes to slot 7 and updates
it to 8
atomic
...
Note: there are many different names for these two operations
down: wait, semWait, P (Proberen)
up: signal, semSignal, V (Vergogen)
MUTEX
• Short form for “Mutual exclusion object.”
• Allows multiple threads for sharing the same
resource. The resource can be a file.
• A mutex must be locked from other threads,
when any thread that needs the resource;
when the data is no longer used/needed, the
mutex is set to unlocked ‘only by the thread
that locked it’; thus it has ownership concept.
Mutexes
...
...