|
|
|
Modern Operating Systems by Herbert Bos and Andrew S. Tanenb...
Modern_Operating_Systems_by_Herbert_Bos_and_Andrew_S._Tanenbaum_4th_Ed.pdf
Showing 162-163 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 162
SEC. 2.3
INTERPROCESS COMMUNICATION
131
system briefly disabling all interrupts while it is testing the semaphore, updating it,
and putting the process to sleep, if necessary.
As all of these actions take only a
few instructions, no harm is done in disabling interrupts.
If multiple CPUs are
being used, each semaphore should be protected by a lock variable, with the
TSL
or
XCHG
instructions used to make sure that only one CPU at a time examines the
semaphore.
Be sure you understand that using
TSL
or
XCHG
to prevent several CPUs from
accessing the semaphore at the same time is quite different from the producer or
consumer busy waiting for the other to empty or fill the buffer. The semaphore op-
eration will take only a few microseconds, whereas the producer or consumer
might take arbitrarily long.
#define N 100
/
*
number of slots in the buffer
*
/
typedef int semaphore;
/
*
semaphores are a special kind of int
*
/
semaphore mutex = 1;
/
*
controls access to critical region
*
/
semaphore empty = N;
/
*
counts empty buffer slots
*
/
semaphore full = 0;
/
*
counts full buffer slots
*
/
void producer(void)
{
int item;
while (TRUE) {
/
*
TRUE is the constant 1
*
/
item = produce
item( );
/
*
generate something to put in buffer
*
/
down(&empty);
/
*
decrement empty count
*
/
down(&mutex);
/
*
enter critical region
*
/
insert
item(item);
/
*
put new item in buffer
*
/
up(&mutex);
/
*
leave critical region
*
/
up(&full);
/
*
increment count of full slots
*
/
}
}
void consumer(void)
{
int item;
while (TRUE) {
/
*
infinite loop
*
/
down(&full);
/
*
decrement full count
*
/
down(&mutex);
/
*
enter critical region
*
/
item = remove
item( );
/
*
take item from buffer
*
/
up(&mutex);
/
*
leave critical region
*
/
up(&empty);
/
*
increment count of empty slots
*
/
consume
item(item);
/
*
do something with the item
*
/
}
}
Figure 2-28.
The producer-consumer problem using semaphores.
Page 163
132
PROCESSES AND THREADS
CHAP. 2
This solution uses three semaphores: one called
full
for counting the number of
slots that are full, one called
empty
for counting the number of slots that are empty,
and one called
mutex
to make sure the producer and consumer do not access the
buffer at the same time.
Full
is initially 0,
empty
is initially equal to the number of
slots in the buffer, and
mutex
is initially 1.
Semaphores that are initialized to 1 and
used by two or more processes to ensure that only one of them can enter its critical
region at the same time are called
binary semaphores
.
If each process does a
down
just before entering its critical region and an
up
just after leaving it, mutual
exclusion is guaranteed.
Now that we have a good interprocess communication primitive at our dis-
posal, let us go back and look at the interrupt sequence of Fig. 2-5 again. In a sys-
tem using semaphores, the natural way to hide interrupts is to have a semaphore,
initially set to 0, associated with each I/O device. Just after starting an I/O device,
the managing process does a
down
on the associated semaphore, thus blocking im-
mediately. When the interrupt comes in, the interrupt handler then does an
up
on
the associated semaphore, which makes the relevant process ready to run again. In
this model, step 5 in Fig. 2-5 consists of doing an
up
on the device’s semaphore, so
that in step 6 the scheduler will be able to run the device manager.
Of course, if
several processes are now ready, the scheduler may choose to run an even more im-
portant process next. We will look at some of the algorithms used for scheduling
later on in this chapter.
In the example of Fig. 2-28, we have actually used semaphores in two different
ways. This difference is important enough to make explicit. The
mutex
semaphore
is used for mutual exclusion. It is designed to guarantee that only one process at a
time will be reading or writing the buffer and the associated variables. This mutual
exclusion is required to prevent chaos.
We will study mutual exclusion and how to
achieve it in the next section.
The other use of semaphores is for
synchronization
. The
full
and
empty
sem-
aphores are needed to guarantee that certain event sequences do or do not occur.
In
this case, they ensure that the producer stops running when the buffer is full, and
that the consumer stops running when it is empty. This use is different from mutual
exclusion.
2.3.6 Mutexes
When the semaphore’s ability to count is not needed, a simplified version of
the semaphore, called a mutex, is sometimes used. Mutexes are good only for man-
aging mutual exclusion to some shared resource or piece of code. They are easy
and efficient to implement, which makes them especially useful in thread packages
that are implemented entirely in user space.
A
mutex
is a shared variable that can be in one of two states: unlocked or
locked. Consequently, only 1 bit is required to represent it, but in practice an inte-
ger often is used, with 0 meaning unlocked and all other values meaning locked.
Ace your assessments! Get Better Grades
Browse thousands of Study Materials & Solutions from your Favorite Schools
Concordia University
Concordia_University
School:
Operating_Systems
Course:
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
lab 18.docx
lab_18.docx
Course
Course
3
Module5QuizSTA2023.d...
Module5QuizSTA2023.docx.docx
Course
Course
10
Week 7 Test Math302....
Week_7_Test_Math302.docx.docx
Course
Course
30
Chapter 1 Assigment ...
Chapter_1_Assigment_Questions.docx.docx
Course
Course
5
Week 4 tests.docx.do...
Week_4_tests.docx.docx
Course
Course
23
Week 6 tests.docx.do...
Week_6_tests.docx.docx
Course
Course
106