|
|
|
Modern Operating Systems by Herbert Bos and Andrew S. Tanenb...
Modern_Operating_Systems_by_Herbert_Bos_and_Andrew_S._Tanenbaum_4th_Ed.pdf
Showing 177-178 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 177
146
PROCESSES AND THREADS
CHAP. 2
#define N 100
/
*
number of slots in the buffer
*
/
void producer(void)
{
int item;
message m;
/
*
message buffer
*
/
while (TRUE) {
item = produce
item( );
/
*
generate something to put in buffer
*
/
receive(consumer, &m);
/
*
wait for an empty to arrive
*
/
build
message(&m, item);
/
*
construct a message to send
*
/
send(consumer, &m);
/
*
send item to consumer
*
/
}
}
void consumer(void)
{
int item, i;
message m;
for (i = 0; i < N; i++) send(producer, &m); /
*
send N empties
*
/
while (TRUE) {
receive(producer, &m);
/
*
get message containing item
*
/
item = extract
item(&m);
/
*
extract item from message
*
/
send(producer, &m);
/
*
send back empty reply
*
/
consume
item(item);
/
*
do something with the item
*
/
}
}
Figure 2-36.
The producer-consumer problem with
N
messages.
Message passing is commonly used in parallel programming systems. One
well-known message-passing system, for example, is
MPI
(
Message-Passing
Interface
). It is widely used for scientific computing. For more information about
it, see for example Gropp et al. (1994), and Snir et al. (1996).
2.3.9 Barriers
Our last synchronization mechanism is intended for groups of processes rather
than two-process producer-consumer type situations. Some applications are divi-
ded into phases and have the rule that no process may proceed into the next phase
until all processes are ready to proceed to the next phase. This behavior may be
achieved by placing a
barrier
at the end of each phase. When a process reaches
the barrier, it is blocked until all processes have reached the barrier. This allows
groups of processes to synchronize.
Barrier operation is illustrated in Fig. 2-37.
Page 178
SEC. 2.3
INTERPROCESS COMMUNICATION
147
Barrier
Barrier
Barrier
A
A
A
B
B
B
C
C
D
D
D
Time
Time
Time
Process
(a)
(b)
(c)
C
Figure 2-37.
Use of a barrier. (a) Processes approaching a barrier. (b) All proc-
esses but one blocked at the barrier. (c) When the last process arrives at the barri-
er, all of them are let through.
In Fig. 2-37(a) we see four processes approaching a barrier. What this means is
that they are just computing and have not reached the end of the current phase yet.
After a while, the first process finishes all the computing required of it during the
first phase.
It then executes the
barrier
primitive, generally by calling a library pro-
cedure. The process is then suspended.
A little later, a second and then a third
process finish the first phase and also execute the
barrier
primitive. This situation is
illustrated in Fig. 2-37(b). Finally, when the last process,
C
, hits the barrier, all the
processes are released, as shown in Fig. 2-37(c).
As an example of a problem requiring barriers, consider a common relaxation
problem in physics or engineering. There is typically a matrix that contains some
initial values. The values might represent temperatures at various points on a sheet
of metal. The idea might be to calculate how long it takes for the effect of a flame
placed at one corner to propagate throughout the sheet.
Starting with the current values, a transformation is applied to the matrix to get
the second version of the matrix, for example, by applying the laws of thermody-
namics to see what all the temperatures are
Δ
T
later. Then the process is repeated
over and over, giving the temperatures at the sample points as a function of time as
the sheet heats up. The algorithm produces a sequence of matrices over time, each
one for a given point in time.
Now imagine that the matrix is very large (for example, 1 million by 1 mil-
lion), so that parallel processes are needed (possibly on a multiprocessor) to speed
up the calculation. Different processes work on different parts of the matrix, calcu-
lating the new matrix elements from the old ones according to the laws of physics.
However, no process may start on iteration
n
+
1 until iteration
n
is complete, that
is, until all processes have finished their current work. The way to achieve this goal
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