Operating Systems - CS604 Power Point Slides Lecture 07
Transcript of Operating Systems - CS604 Power Point Slides Lecture 07
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
1/39
Operating
SystemsLecture 7
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
2/39
January 27, 2016 Copyright Virtual University of Paistan
Agenda for Today
!evie" of previous lecture #he waitan$ execsyste% calls an$
sa%ple co$e
Cooperating processes
Pro$ucer&consu%er pro'le%
(nterprocess co%%unication )(PC* an$process synchroni+ation
!ecap of the lecture
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
3/39
January 27, 2016 Copyright Virtual University of Paistan
Review of Lecture 6
che$ulers )long&, an$ short&,
an$ %e$iu%&ter%*
-ispatcher
Process creation an$ ter%ination
forkan$ exit syste% calls
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
4/39
January 27, 2016 Copyright Virtual University of Paistan
wait()
#he waitsyste% call suspen$s thecalling process until one of its
i%%e$iate chil$ren ter%inates, or until
a chil$ that is 'eing trace$ stops'ecause it has hit an event of interest.
waitreturns pre%aturely if a signal is
receive$. (f all chil$ren processesstoppe$ or ter%inate$ prior to the call
on wait, return is i%%e$iate.
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
5/39
January 27, 2016 Copyright Virtual University of Paistan
Synopsis of wait()
#include
#include
pid_t wait(int *stat_loc);
:/usr/include/sys/types.h
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
6/39
January 27, 2016 Copyright Virtual University of Paistan
wait() ...
(f the call is successful, theprocess (- of the ter%inating chil$is returne$.
(f parent ter%inates all its chil$renhave assigne$ as their ne"
parent, the initprocess. #hus thechil$ren still have a parent tocollect their status an$ e/ecution
statistics.
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
7/39
January 27, 2016 Copyright Virtual University of Paistan
wait() ...
Zombieprocessa process thathas ter%inate$ 'ut "hose e/it
status has not yet 'een receive$'y its parent process or 'y init.
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
8/39
January 27, 2016 Copyright Virtual University of Paistan
Sample Codefork
#include void main()
{
int pid, status;
pid !o"();
i!(pid $%) { p"int!(&!o" !ailed'n);
eit(%);
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
9/39
January 27, 2016 Copyright Virtual University of Paistan
Sample Codefork
i!(pid +) { /* hild */ p"int!(&hild he"e-'n);
eit(+);
else { /* a"ent */
wait(status);
p"int!(&0ell done id-'n); eit(+);
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
10/39
January 27, 2016 Copyright Virtual University of Paistan
Semantics of fork
P
P
fork
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
11/39
January 27, 2016 Copyright Virtual University of Paistan
e!ec()
#ypically the execsyste% call is use$
after a forksyste% call 'y one of the
t"o processes to replace the process
%e%ory space "ith a ne" e/ecuta'le
progra%.
#he ne" process i%age isconstructe$ fro% an or$inary,
e/ecuta'le file.
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
12/39
January 27, 2016 Copyright Virtual University of Paistan
e!ec()
#here can 'e no return fro% a
successful exec'ecause the
calling process i%age is overlai$'y the ne" process i%age
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
13/39
January 27, 2016 Copyright Virtual University of Paistan
Synopsis of e!ec()
inclu$e 3unist$.h4int e/eclp )const char 5file, const
char 5arg0, ..., const char 5argn,
)char 5*0*
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
14/39
January 27, 2016 Copyright Virtual University of Paistan
Sample Codefork
and exec#include void main()
{
int pid, status;
pid !o"();
i!(pid $%) { p"int!(&!o" !ailed'n);
eit(%);
S l C d
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
15/39
January 27, 2016 Copyright Virtual University of Paistan
Sample Codefork
and exec i!(pid +) { /* hild */ i! (eeclp(&/1in/ls, &ls, 2344)< +) {
p"int!(&eec !ailed'n);
eit(%);
else { /* a"ent */
wait(status);
p"int!(&0ell done id-'n);
eit(+);
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
16/39
January 27, 2016 Copyright Virtual University of Paistan
Semantics of fork
P
P
!o"
pa"ent
child
P
P
pa"ent
child
eec ls
P
ls
pa"ent
child
1 2
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
17/39
January 27, 2016 Copyright Virtual University of Paistan
Cooperating "rocesses
Independentprocess cannotaffect or 'e affecte$ 'y the
e/ecution of another process.Cooperatingprocess can affect or
'e affecte$ 'y the e/ecution of
another process
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
18/39
January 27, 2016 Copyright Virtual University of Paistan
Cooperating "rocesses
8$vantages of process cooperation (nfor%ation sharing
Co%putation spee$&up
9o$ularity
Convenience
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
19/39
January 27, 2016 Copyright Virtual University of Paistan
Para$ig% for cooperating processes,
producerprocess pro$uces
infor%ation that is consu%e$ 'y aconsumerprocess.
unbounded-bufferplaces no practical
li%it on the si+e of the 'uffer
bounded-bufferassu%es that there is
a fi/e$ 'uffer si+e
"roducer#Consumer
"ro$lem
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
20/39
January 27, 2016 Copyright Virtual University of Paistan
%ounded#%uffer "ro$lem
ProducerProducer ConsumerConsumer
:%pty Pool
;ull Pool
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
21/39
January 27, 2016 Copyright Virtual University of Paistan
%ounded#%uffer Solution
hare$ $ata#de!ine 536678_97 %+
typede! st"uct {
. . . item;
item 1u!!e"536678_97=;
int in +;int out +;
olution is correct, 'ut can only use
:&1 ele%ents
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
22/39
January 27, 2016 Copyright Virtual University of Paistan
"roducer "rocess
item net"oduced;
while (%) {
while (((in %) ? 536678_97) out); /* do nothin@ */
1u!!e"in= net"oduced;
in (in %) ? 536678_97;
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
23/39
January 27, 2016 Copyright Virtual University of Paistan
Consumer "rocess
item netonsumed;
while (%) {
while (in out); /* do nothin@ */
netonsumed 1u!!e"out=;
out (out %) ? 536678_97;
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
24/39
January 27, 2016 Copyright Virtual University of Paistan
&nterprocess
Communication (&"C)9echanis% for processes to
co%%unicate an$ to synchroni+etheir actions.
9essage syste% ? processes
co%%unicate "ith each other"ithout resorting to share$varia'les.
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
25/39
January 27, 2016 Copyright Virtual University of Paistan
(PC facility provi$es t"o operations@
Send )message* ? %essage si+e
fi/e$ or varia'le
Receive )message*
&nterprocess
Communication (&"C)
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
26/39
January 27, 2016 Copyright Virtual University of Paistan
(f Pan$ Q"ish to co%%unicate,
they nee$ to@esta'lish a communicationlink
'et"een the%
e/change %essages via
sen$Areceive
&nterprocess
Communication (&"C)
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
27/39
January 27, 2016 Copyright Virtual University of Paistan
(%ple%entation of co%%unication
linphysical)e.g., share$ %e%ory,
har$"are 'us* logical)e.g., logical properties*
&nterprocess
Communication (&"C)
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
28/39
January 27, 2016 Copyright Virtual University of Paistan
&mplementation
'uestions
Bo" are lins esta'lishe$
Can a lin 'e associate$ "ith %orethan t"o processes
Bo" %any lins can there 'e
'et"een every pair of
co%%unicating processes
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
29/39
January 27, 2016 Copyright Virtual University of Paistan
&mplementation
'uestions
Dhat is the capacity of a lin
(s the si+e of a %essage that thelin can acco%%o$ate fi/e$ or
varia'le
(s a lin uni$irectional or 'i&
$irectional
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
30/39
January 27, 2016 Copyright Virtual University of Paistan
irect Communication
Processes %ust na%e each other
e/plicitly@
send)P, message* ? sen$ a%essage to process P
Receive )Q, message* ? receive a%essage fro% process E
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
31/39
January 27, 2016 Copyright Virtual University of Paistan
irect Communication
Properties of co%%unication lin
Lins are esta'lishe$ auto%atically.
8 lin is associate$ "ith e/actly onepair of co%%unicating processes.
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
32/39
January 27, 2016 Copyright Virtual University of Paistan
&ndirect Communication
9essages are $irecte$ an$receive$ fro% %ail'o/es )also
referre$ to as ports*.:ach %ail'o/ has a uniFue i$.
Processes can co%%unicate only if
they share a %ail'o/.
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
33/39
January 27, 2016 Copyright Virtual University of Paistan
&ndirect Communication
Properties of co%%unication linLin esta'lishe$ only if processes
share a co%%on %ail'o/
8 lin %ay 'e associate$ "ith %anyprocesses.
:ach pair of processes %ay shareseveral co%%unication lins.
Lin %ay 'e uni$irectionalor 'i&$irectional.
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
34/39
January 27, 2016 Copyright Virtual University of Paistan
Gperations create a ne" %ail'o/
sen$ an$ receive %essages through
%ail'o/
$estroy a %ail'o/
Pri%itives are $efine$ as@send )A, message*
receive )A, message*
&ndirect Communication
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
35/39
January 27, 2016 Copyright Virtual University of Paistan
Mailbox sharing
P1, P2,an$P3share %ail'o/ 8.
P1, sen$s P2an$P3receive.
Dho gets the %essage
&ndirect Communication
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
36/39
January 27, 2016 Copyright Virtual University of Paistan
Solutions8llo" a lin to 'e associate$ "ith
at %ost t"o processes.8llo" only one process at a ti%e
to e/ecute a receive operation.
8llo" the syste% to selectar'itrarily the receiver. en$er isnotifie$ "ho the receiver "as.
&ndirect Communication
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
37/39
January 27, 2016 Copyright Virtual University of Paistan
Sync*roni+ation
9essage passing %ay 'e either'locing or non&'locing.
Blockingis consi$ere$synchronous
Non-blockingis consi$ere$
asynchronous sendan$ receivepri%itives %ay
'e either 'locing or non&'locing.
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
38/39
January 27, 2016 Copyright Virtual University of Paistan
%uffering
Eueue of %essages attache$ to thelin i%ple%ente$ in one of three "ays.
Zero capacity? Ho %essages
en$er %ust "ait for receiver
Bounded capacity? n%essagesen$er %ust "ait if lin full.
Unbounded capacity? infinite length
en$er never "aits.
-
7/25/2019 Operating Systems - CS604 Power Point Slides Lecture 07
39/39
J 27 2016 C i ht Vi t l U i
Recap of Lecture
!evie" of previous lecture #he waitan$ execsyste% call an$
sa%ple co$e
Cooperating processes Pro$ucer&consu%er pro'le%
a%ple co$es
(nterprocess co%%unication )(PC* an$
process synchroni+ation
!ecap of the lecture