|
|
|
Modern Operating Systems by Herbert Bos and Andrew S. Tanenb...
Modern_Operating_Systems_by_Herbert_Bos_and_Andrew_S._Tanenbaum_4th_Ed.pdf
Showing 201-202 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 201
170
PROCESSES AND THREADS
CHAP. 2
#define N
5
/
*
number of philosophers
*
/
#define LEFT
(i+N
−
1)%N
/
*
number of i’s left neighbor
*
/
#define RIGHT
(i+1)%N
/
*
number of i’s right neighbor
*
/
#define THINKING
0
/
*
philosopher is thinking
*
/
#define HUNGRY
1
/
*
philosopher is trying to get forks
*
/
#define EATING
2
/
*
philosopher is eating
*
/
typedef int semaphore;
/
*
semaphores are a special kind of int
*
/
int state[N];
/
*
array to keep track of everyone’s state
*
/
semaphore mutex = 1;
/
*
mutual exclusion for critical regions
*
/
semaphore s[N];
/
*
one semaphore per philosopher
*
/
void philosopher(int i)
/
*
i: philosopher number, from 0 to N
−
1
*
/
{
while (TRUE) {
/
*
repeat forever
*
/
think( );
/
*
philosopher is thinking
*
/
take
forks(i);
/
*
acquire two forks or block
*
/
eat( );
/
*
yum-yum, spaghetti
*
/
put
forks(i);
/
*
put both forks back on table
*
/
}
}
void take
forks(int i)
/
*
i: philosopher number, from 0 to N
−
1
*
/
{
down(&mutex);
/
*
enter critical region
*
/
state[i] = HUNGRY;
/
*
record fact that philosopher i is hungry
*
/
test(i);
/
*
try to acquire 2 forks
*
/
up(&mutex);
/
*
exit critical region
*
/
down(&s[i]);
/
*
block if forks were not acquired
*
/
}
void put
forks(i)
/
*
i: philosopher number, from 0 to N
−
1
*
/
{
down(&mutex);
/
*
enter critical region
*
/
state[i] = THINKING;
/
*
philosopher has finished eating
*
/
test(LEFT);
/
*
see if left neighbor can now eat
*
/
test(RIGHT);
/
*
see if right neighbor can now eat
*
/
up(&mutex);
/
*
exit critical region
*
/
}
void test(i) /
*
i: philosopher number, from 0 to N
−
1
*
/
{
if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING) {
state[i] = EATING;
up(&s[i]);
}
}
Figure 2-47.
A solution to the dining philosophers problem.
Page 202
SEC. 2.5
CLASSICAL IPC PROBLEMS
171
2.5.2 The Readers and Writers Problem
The dining philosophers problem is useful for modeling processes that are
competing for exclusive access to a limited number of resources, such as I/O de-
vices. Another famous problem is the readers and writers problem (Courtois et al.,
1971), which models access to a database. Imagine, for example, an airline reser-
vation system, with many competing processes wishing to read and write it.
It is
acceptable to have multiple processes reading the database at the same time, but if
one process is updating (writing) the database, no other processes may have access
to the database, not even readers. The question is how do you program the readers
and the writers?
One solution is shown in Fig. 2-48.
typedef int semaphore;
/
*
use your imagination
*
/
semaphore mutex = 1;
/
*
controls access to rc
*
/
semaphore db = 1;
/
*
controls access to the database
*
/
int rc = 0;
/
*
# of processes reading or wanting to
*
/
void reader(void)
{
while (TRUE) {
/
*
repeat forever
*
/
down(&mutex);
/
*
get exclusive access to rc
*
/
rc = rc + 1;
/
*
one reader more now
*
/
if (rc == 1) down(&db);
/
*
if this is the first reader ...
*
/
up(&mutex);
/
*
release exclusive access to rc
*
/
read
data
base( );
/
*
access the data
*
/
down(&mutex);
/
*
get exclusive access to rc
*
/
rc = rc
−
1;
/
*
one reader fewer now
*
/
if (rc == 0) up(&db);
/
*
if this is the last reader ...
*
/
up(&mutex);
/
*
release exclusive access to rc
*
/
use
data
read( );
/
*
noncritical region
*
/
}
}
void writer(void)
{
while (TRUE) {
/
*
repeat forever
*
/
think
up
data( );
/
*
noncritical region
*
/
down(&db);
/
*
get exclusive access
*
/
write
data
base( );
/
*
update the data
*
/
up(&db);
/
*
release exclusive access
*
/
}
}
Figure 2-48.
A solution to the readers and writers problem.
In this solution, the first reader to get access to the database does a
down
on the
semaphore
db
.
Subsequent readers merely increment a counter,
rc
.
As readers
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