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 201-202 out of 1137
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
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;
/
*
*
/
semaphore mutex = 1;
/
*
*
/
semaphore db = 1;
/
*
*
/
int rc = 0;
/
*
# of processes reading or wanting to
*
/
{
while (TRUE) {
/
*
repeat forever
*
/
down(&mutex);
/
*
*
/
rc = rc + 1;
/
*
*
/
if (rc == 1) down(&db);
/
*
if this is the first reader ...
*
/
up(&mutex);
/
*
*
/
data
base( );
/
*
access the data
*
/
down(&mutex);
/
*
*
/
rc = rc
1;
/
*
*
/
if (rc == 0) up(&db);
/
*
if this is the last reader ...
*
/
up(&mutex);
/
*
*
/
use
data
/
*
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.
down
on the
semaphore
db
.
Subsequent readers merely increment a counter,
rc
.

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