Modern Operating Systems by Herbert Bos ...
Modern_Operating_Systems_by_Herbert_Bos_and_Andrew_S._Tanenbaum_4th_Ed.pdf-M ODERN O PERATING S YSTEMS
Showing 164 out of 1137
Modern Operating Systems by Herbert Bos and Andrew...
Modern_Operating_Systems_by_Herbert_Bos_and_Andrew_S._Tanenbaum_4th_Ed.pdf-M ODERN O PERATING S YSTEMS
Modern Operating Systems by Herbert...
Modern_Operating_Systems_by_Herbert_Bos_and_Andrew_S._Tanenbaum_4th_Ed.pdf-M ODERN O PERATING S YSTEMS
Page 164
SEC. 2.3
INTERPROCESS COMMUNICATION
133
Two procedures are used with mutexes. When a thread (or process) needs access
to a critical region, it calls
mutex
lock
.
If the mutex is currently unlocked (mean-
ing that the critical region is available), the call succeeds and the calling thread is
free to enter the critical region.
On the other hand, if the mutex is already locked, the calling thread is blocked
until the thread in the critical region is finished and calls
mutex
unlock
. If multi-
ple threads are blocked on the mutex, one of them is chosen at random and allowed
to acquire the lock.
Because mutexes are so simple, they can easily be implemented in user space
provided that a
TSL
or
XCHG
instruction is available. The code for
mutex
lock
and
mutex
unlock
for use with a user-level threads package are shown in Fig. 2-29.
The solution with
XCHG
is essentially the same.
mutex
lock:
TSL REGISTER,MUTEX
| copy mutex to register and set mutex to 1
CMP REGISTER,#0
| was mutex zero?
JZE ok
| if it was zero, mutex was unlocked, so return
CALL thread
yield
| mutex is busy; schedule another thread
JMP mutex
lock
| try again
ok:
RET
| return to caller; critical region entered
mutex
unlock:
MOVE MUTEX,#0
| store a 0 in mutex
RET
| return to caller
Figure 2-29.
Implementation of
mutex
lock
and
mutex
unlock.
The code of
mutex
lock
is similar to the code of
enter
region
of Fig. 2-25 but
with a crucial difference. When
enter
region
fails to enter the critical region, it
keeps testing the lock repeatedly (busy waiting). Eventually, the clock runs out
and some other process is scheduled to run. Sooner or later the process holding the
lock gets to run and releases it.
With (user) threads, the situation is different because there is no clock that
stops threads that have run too long. Consequently, a thread that tries to acquire a
lock by busy waiting will loop forever and never acquire the lock because it never
allows any other thread to run and release the lock.
That is where the difference between
enter
region
and
mutex
lock
comes in.
When the later fails to acquire a lock, it calls
thread
yield
to give up the CPU to
another thread. Consequently there is no busy waiting. When the thread runs the
next time, it tests the lock again.
Since
thread
yield
is just a call to the thread scheduler in user space, it is very
fast. As a consequence, neither
mutex
lock
nor
mutex
unlock
requires any kernel
calls. Using them, user-level threads can synchronize entirely in user space using
procedures that require only a handful of instructions.


Ace your assessments! Get Better Grades
Browse thousands of Study Materials & Solutions from your Favorite Schools
Concordia University
Concordia_University
School:
Operating_Systems
Course:
Great resource for chem class. Had all the past labs and assignments
Leland P.
Santa Clara University
Introducing Study Plan
Using AI Tools to Help you understand and remember your course concepts better and faster than any other resource.
Find the best videos to learn every concept in that course from Youtube and Tiktok without searching.
Save All Relavent Videos & Materials and access anytime and anywhere
Prepare Smart and Guarantee better grades

Students also viewed documents