Upload
mayur-patil
View
242
Download
0
Embed Size (px)
Citation preview
7/25/2019 chap03 from OSD Crowley
1/58
01/29/16 Crowley OS Chap. 3 1
The Operating System Interface
Chapter 3
7/25/2019 chap03 from OSD Crowley
2/58
01/29/16 Crowley OS Chap. 3 2
ey concepts in chapter 3
!System calls
! "ile an# I/O system
$hierarchical file naming
$file interface% open& rea#& write& lsee'& close
$file (ers)s open file
$#e(ices as files *in naming an# in interface+
! ,rocess$operations% create& e-it& wait
! Shell
7/25/2019 chap03 from OSD Crowley
3/58
01/29/16 Crowley OS Chap. 3 3
The OS e(el Str)ct)re
*chapters 20+
*chapter 3+
*chapter 2+
7/25/2019 chap03 from OSD Crowley
4/58
01/29/16 Crowley OS Chap. 3
System calls
! special machine instr)ction
$that ca)ses an interr)pt
$(ario)s names% syscall& trap& s(c
! s)ally not generate# 4y 5s
$4)t in assem4ly lang)age f)nctions
7/25/2019 chap03 from OSD Crowley
5/58
01/29/16 Crowley OS Chap. 3
System call flow of control
7/25/2019 chap03 from OSD Crowley
6/58
01/29/16 Crowley OS Chap. 3 6
5ierarchical file naming systems
! tree of #irectories an# files
$#irectory% contains file an# #irectory names
! O4ects *files an# #irectories+ are name#with path names
$later% other 'in#s of o4ects *e.g. #e(ices+
! ,ath names contains a component name foreach #irectory in the path
7/25/2019 chap03 from OSD Crowley
7/58
7/25/2019 chap03 from OSD Crowley
8/58
01/29/16 Crowley OS Chap. 3 8
"ile an# I/O system calls
! int open*char name& int flags+
! int rea#*int fi#& char 4)ffer& int co)nt+
! int write*int fi#& char 4)ffer& int co)nt+
! int lsee'*int fi#& int offset& int from+
! int close*int fi#+! int )nlin'*char name+
7/25/2019 chap03 from OSD Crowley
9/58
01/29/16 Crowley OS Chap. 3 9
Steps in )sing a file
7/25/2019 chap03 from OSD Crowley
10/58
01/29/16 Crowley OS Chap. 3 10
"iles (ers)s open files
! File% passi(e container of 4ytes on #is'
! Open file% acti(e so)rce *or sin'+ of 4ytes in
a r)nning program
$)s)ally connecte# to a file
$4)t can 4e connecte# to a #e(ice or another
process
7/25/2019 chap03 from OSD Crowley
11/58
01/29/16 Crowley OS Chap. 3 11
"iles an# open files
7/25/2019 chap03 from OSD Crowley
12/58
01/29/16 Crowley OS Chap. 3 12
OS o4ects an# operations
7/25/2019 chap03 from OSD Crowley
13/58
01/29/16 Crowley OS Chap. 3 13
"ile copy enum { Reading=0, Writing=1, ReadAndWrite=2,
ReadWriteFile=0644 };
void FileCop! "#ar $ %romFile, "#ar $ toFile & { int %romF' = open! %romFile, Reading &;
i%! %romF' ( 0 & {
"err (( )*rror opening ) (( %romFile (( endl;
return; }
int toF' = "reat! toFile, ReadWriteFile &;
i%! toF' ( 0 & {
"err (( )*rror opening ) (( toFile (( endl; "lo+e! %romF' &; return; }
#ile! 1 & {
"#ar "#; int n = read! %romF', -"#, 1 &;
i%! n (= 0 & .rea/;
n = rite! toF', -"#, 1 &;
i%! n ( 0 & { "err (( )*rror riting ) (( toFile (( endl;
return; }
}
"lo+e! %romF' &; "lo+e! toF' &;
}
7/25/2019 chap03 from OSD Crowley
14/58
01/29/16 Crowley OS Chap. 3 1
"ile re(erse *1 of 2+ enum { Reading=0, Writing=1, ReadAndWrite=2 };
enum {ee/Fromeginning=0,ee/FromCurrent=1,ee/From*nd=2};
void Rever+e! "#ar $ %romFile, "#ar $ revFile & {
int %romF' = open! %romFile, Reading &; i%! %romF' ( 0 & {
"err (( )*rror opening ) (( %romFile (( endl;
return;
}
move t#e internal %ile pointer +o t#e ne3t "#ara"ter
read ill .e t#e la+t "#ara"ter o% t#e %ile
int ret l+ee/! %romF', 1, ee/From*nd &;
i%! ret ( 0 & {
"err (( )*rror +ee/ing on ) (( %romFile (( endl;
"lo+e! %romF' &;
return;
}
int revF' = "reat! revFile, 0 &; i%! revF' ( 0 & {
"err (( )*rror "reating ) (( revFile (( endl; "lo+e! %romF' &;
return;
}
7/25/2019 chap03 from OSD Crowley
15/58
01/29/16 Crowley OS Chap. 3 1
"ile re(erse *2 of 2+ #ile! 1 & {
"#ar "#;
int n = read! %romF', -"#, 1 &; i%! n ( 0 & {
"err (( )*rror reading ) (( %romFile (( endl;
return;
}
n = rite! revF', -"#, 1 &;
i%! n ( 0 & {
"err (( )*rror riting ) (( revFile (( endl; return;
}
e3it t#e loop i% l+ee/ return+ an error5
#e e3pe"ted error i+ t#at t#e "omputed o%%+et ill
.e negative5
i%! l+ee/!%romF', 2, ee/FromCurrent& ( 0 &
.rea/;
}
"lo+e! %romF' &;
"lo+e! revF' &;
}
7/25/2019 chap03 from OSD Crowley
16/58
01/29/16 Crowley OS Chap. 3 16
:e(ersing a file
7/25/2019 chap03 from OSD Crowley
17/58
01/29/16 Crowley OS Chap. 3 17
;esign techni
7/25/2019 chap03 from OSD Crowley
18/58
01/29/16 Crowley OS Chap. 3 18
>eta#ata
! >eta#ata #escri4es the file rather than
4eing the #ata in file itself$also calle# metainformation
! ?-amples of meta#ata
$@ho owns the file$@ho can )se the file an# how
$@hen the file was create#& last )se#& last
mo#ifie#! int stat*char name& StatInfo statInfo+
$this calls ret)rns the file meta#ata
7/25/2019 chap03 from OSD Crowley
19/58
01/29/16 Crowley OS Chap. 3 19
Aaming OS o4ects
! "ile naming system names files *an#
#irectories+
$4)t why limit it to that
! Other OS o4ects nee# names%
$processes
$#e(ices
$I,C% message
7/25/2019 chap03 from OSD Crowley
20/58
01/29/16 Crowley OS Chap. 3 20
>apping names to o4ects
7/25/2019 chap03 from OSD Crowley
21/58
01/29/16 Crowley OS Chap. 3 21
;e(ices as files
! ;e(ices are name# as files
$they can 4e opene# as files& to create open files
$they can 4e )se# as 4yte streams% so)rces of4ytes an# sin's for 4ytes
! ?-amples
$copy some"ile /#e(/tty17$copy a"ile /#e(/tape01
7/25/2019 chap03 from OSD Crowley
22/58
01/29/16 Crowley OS Chap. 3 22
The process concept
! Program% a static& algorithmic #escription&consists of instr)ctions
7 int main!& {
int i, prod=1;
%or!i=0; i(100; 88i& prod = prod$i;
}
! Process% #ynamic& consists of instr)ctione-ec)tions
7/25/2019 chap03 from OSD Crowley
23/58
01/29/16 Crowley OS Chap. 3 23
Simple create process void Create9ro"e++1! void & {
int pid1 = impleCreate9ro"e++! )"ompiler) &;
i%! pid1 ( 0 & {
"err (( )Could not "reate pro"e++ :)"ompiler:)
(( endl;
return; }
int pid2 = impleCreate9ro"e++! )editor) &;
i%! pid2 ( 0 & { "err (( )Could not "reate pro"e++ :)editor:)
(( endl;
return; }
Wait until t#e are .ot# "ompleted5
impleWait! pid1 &;
impleWait! pid2 &; )"ompiler) and )editor) al+o end . ma/ing
imple*3it ++tem "all+
imple*3it!&;
}
7/25/2019 chap03 from OSD Crowley
24/58
01/29/16 Crowley OS Chap. 3 2
,rocess system calls
! int Create,rocess*
char progAame& int argc& char arg(B +
$progAame is the program to r)n in the process
$ret)rns a process i#entifier *pi#+
! (oi# ?-it*int ret)rnCo#e+
$e-its the process that e-ec)tes the e-it system
calls
! int @ait*int pi#+
$waits for a chil# process to e-it
C t
7/25/2019 chap03 from OSD Crowley
25/58
01/29/16 Crowley OS Chap. 3 2
Create process void Create9ro"e++2! void & {
+tati" "#ar $ arg.
= { )"ompiler), )%ileoCompile), !"#ar $& 0 };
int pid1 = Create9ro"e++! )"ompiler), , arg. &;
i%! pid1 ( 0 & {
"err (( )Could not "reate pro"e++ :)"ompiler:)
(( endl;
return;
}
"#ar $ argv;
argv = )editor); argv = )%ileo*dit);
argv = !"#ar $& 0;
int pid2 = Create9ro"e++! )editor), , argv &;
i%! pid2 ( 0 & {
"err (( )Could not "reate pro"e++ :)"ompiler:)
(( endl;
return;
}
!void& Wait! pid1 &;
!void& Wait! pid2 &;
*3it! 0 &;
}
7/25/2019 chap03 from OSD Crowley
26/58
01/29/16 Crowley OS Chap. 3 26
5ow arg)ments are passe#
7/25/2019 chap03 from OSD Crowley
27/58
01/29/16 Crowley OS Chap. 3 27
,rint arg)ments #i+ program rite+ out it argument+5
?in"lude (io+tream5#@
void main! int arg", "#ar $ argv< > & {
int i;
%or! i = 0; i ( arg"; 88i & {
"out (( argv (( ) );
}
"out (( ):n);
}
7/25/2019 chap03 from OSD Crowley
28/58
01/29/16 Crowley OS Chap. 3 28
process hierarchy
7/25/2019 chap03 from OSD Crowley
29/58
01/29/16 Crowley OS Chap. 3 29
Interprocess comm)nication
*I,C+
! >any metho#s ha(e 4een )se#% messages&
pipes& soc'ets& remote proce#)re call& etc.
! >essages an# message
7/25/2019 chap03 from OSD Crowley
30/58
01/29/16 Crowley OS Chap. 3 30
?-ample of
message passing paths
7/25/2019 chap03 from OSD Crowley
31/58
01/29/16 Crowley OS Chap. 3 31
>essage passing system calls
! int Create>essageD)e)e* (oi# +
$ret)rns a message
7/25/2019 chap03 from OSD Crowley
32/58
01/29/16 Crowley OS Chap. 3 32
>essage% file sen#er *1 of 2+
void end+go! int m+gBBid, int m+g0=0, int
m+g1=0, int m+g2=0 & { int m+g;
m+g = m+g0; m+g = m+g1; m+g = m+g2;
!void&ende++age! m+gBBid, m+g &;
}
enum { Reading=0, Writing=1, ReadAndWrite=2 };
enum{ FileoEpen=1, endueue=2, Re"eiveueue= };void main! int arg", "#ar $ argv< > & {
int %romF' = open! argv, Reading &;
i%! %romF' ( 0 & {
"err (( )Could not open %ile
(( argv (( endl;
e3it! 1 &; }
int toB = atoi!argv&;
> fil # *2 f 2+
7/25/2019 chap03 from OSD Crowley
33/58
01/29/16 Crowley OS Chap. 3 33
>essage% file sen#er *2 of 2+ #ile! 1 & {
"#ar "#;
int n = read! %romF', -"#, 1 &; i%! n (= 0 &
.rea/;
end+go! toB, "# &;
}
"lo+e! %romF' &;
end+go! toB, 0 &;
int m+g;
int %romB = atoi!argv
7/25/2019 chap03 from OSD Crowley
34/58
01/29/16 Crowley OS Chap. 3 3
>essage% file recei(er enum{ endueue=1, Re"eiveueue=2 };
void main! int arg", "#ar $ argv< > & {
+tart t#e "ount at Gero5 int "ount = 0;
int m+g;
int %romB = atoi!argv&;
#ile! 1 & {
Re"eivee++age! %romB, m+g &;
i%! m+g == 0 &
.rea/;
An me++age it# nonGero "ontent
i+ a "#ara"ter to "ount5
88"ount;
} end t#e "ount .a"/ to t#e +ender5
int toB = atoi!argv t t *1 f 2+
7/25/2019 chap03 from OSD Crowley
35/58
01/29/16 Crowley OS Chap. 3 3
>essage% start processes *1 of 2+ int Create9ro"e++Wit#Arg+!"#ar $ progBname,
"#ar $ arg1=0, "#ar $ arg2=0, "#ar $ arg=0& {
"#ar $arg+;
arg+ = progBname;
arg+ = arg1;
arg+ = arg2;
arg+ = arg;
arg+ = 0;
int arg" = 4; i%! arg == 0& arg";
i%! arg2 == 0& arg";
i%! arg1 == 0& arg";
return Create9ro"e++! progBname, arg", arg+ &;
}
"#ar $ itoa! int n & { "#ar $ re+ult = ne "#ar;
+print%! re+ult, )Id), n &;
return re+ult;
}
> t t *2 f 2+
7/25/2019 chap03 from OSD Crowley
36/58
01/29/16 Crowley OS Chap. 3 36
>essage% start processes *2 of 2+ void main! int arg", "#ar $ argv< > & {
Create t#e me++age ueue+ t#e pro"e++e+ ill u+e5
int 1 = Createe++ageueue!&;
int 2 = Createe++ageueue!&;
Create t#e to pro"e++e+, +ending ea"# t#e
identi%ier %or t#e me++age ueue+ it ill u+e5
int pid1 = Create9ro"e++Wit#Arg+! )Fileend),
)Fileoend), itoa!1&, itoa!2& &;
int pid2 = Create9ro"e++Wit#Arg+! )FileRe"eive), itoa!1&, itoa!2& &;
Wait %or t#e to pro"e++e+ to "omplete5
int ret1 = ait! pid1 &;
int ret2 = ait! pid2 &;
We do not u+e t#e return "ode ret1 and ret2
in t#i+ e3ample5 'e+tro t#e me++age ueue+5
'e+troe++ageueue! 1 &;
'e+troe++ageueue! 2 &;
*3it! 0 &;
}
7/25/2019 chap03 from OSD Crowley
37/58
01/29/16 Crowley OS Chap. 3 37
O4ects for
sen#ing a file with messages
7/25/2019 chap03 from OSD Crowley
38/58
01/29/16 Crowley OS Chap. 3 38
AIEstyle process creation
!int for'*+$creates an e-act copy of the calling process
! int e-ec(*char progAame& char arg(B +
$r)ns a new program in the calling process$#estroying the ol# program
! int e-it*int retCo#e+
$e-its the calling process! int wait*int retCo#e+
$waits for any e-ite# chil#& ret)rns its pi#
7/25/2019 chap03 from OSD Crowley
39/58
01/29/16 Crowley OS Chap. 3 39
AIE for'
C t *AIE t l +
7/25/2019 chap03 from OSD Crowley
40/58
01/29/16 Crowley OS Chap. 3 0
Create process *AIEstyle+ void Create9ro"e++! void & {
int pid1, pid2;
"#ar $argv = {)"ompiler), )%ileoCompile), 0}; pid1 = %or/!&;
i%! pid1 == 0 & { C#ild pro"e++ "ode .egin+ #ere
e3e"v! )"ompiler), argv &; e3e"ute "ompiler
C#ild pro"e++ "ode end+ #ere5
e3e"v doe+ not return
} 9arent e3e"ute+ #ere .e"au+e pid1 J= 0
argv = )editor);
argv = )%ileo*dit);
argv = 0;
i%! !pid2 = %or/!&& == 0 &
e3e"v! )editor), argv &;int reta, ret.;
int pida = ait! -reta &;
int pid. = ait! -ret. &;
}
7/25/2019 chap03 from OSD Crowley
41/58
01/29/16 Crowley OS Chap. 3 1
Stan#ar# inp)t an# o)tp)t
! >ost programs are filters%
$one inp)t stream *stan#ar# inp)t+
$some processing
$one o)tp)t stream *stan#ar# o)tp)t+
! So the OS starts a program o)t with two open
files& stan#ar# inp)t an# stan#ar# o)tp)t
grep #elveti"a (%ontKi+t @#elvKi+t
7/25/2019 chap03 from OSD Crowley
42/58
01/29/16 Crowley OS Chap. 3 2
:e#irection of stan#ar# inp)t an#
o)tp)t
7/25/2019 chap03 from OSD Crowley
43/58
01/29/16 Crowley OS Chap. 3 3
,ipes
! ,ipe% another I,C mechanism
$)ses the familiar file interface
$not a special interface *li'e messages+
! Connects an open file of one process to an
open file of another process
$Often )se# to connect the stan#ar# o)tp)t of
one process to the stan#ar# inp)t of another
process
7/25/2019 chap03 from OSD Crowley
44/58
01/29/16 Crowley OS Chap. 3
>essages an# pipes compare#
,i fil #
7/25/2019 chap03 from OSD Crowley
45/58
01/29/16 Crowley OS Chap. 3
,ipe% file sen#er enum { Reading=0, Writing=1, ReadAndWrite=2 };
void main! int arg", "#ar $ argv< > & {
int %romF' = open! argv, Reading &; int toBpipe = open! argv, Writing &;
#ile! 1 & {
"#ar "#;
int n = read! %romF', -"#, 1 &;
i%! n == 0 & .rea/;
rite! toBpipe, -"#, 1 &;
}
"lo+e! %romF' &;
"lo+e! toBpipe &;
int n, %romBpipe = open! argv, Reading &;
int n i+ %our .te+ long, +o e read %our .te+
read! %romBpipe, -n, 4 &;
"lo+e! %romBpipe &;
"out (( n (( ) "#ara"ter+:n);
e3it! 0 &;
}
,i fil i
7/25/2019 chap03 from OSD Crowley
46/58
01/29/16 Crowley OS Chap. 3 6
,ipe% file recei(er enum { Reading=0, Writing=1, ReadAndWrite=2 };
void main! int arg", "#ar $ argv< > & {
int "ount = 0; #e %ir+t argument i+ t#e pipe to read %rom5
int %romBpipe = open! argv, Reading &;
#ile! 1 & {
"#ar "#;
int n = read! %romBpipe, -"#, 1 &;
i%! n == 0 &
.rea/;
88"ount;
}
"lo+e! %romBpipe &;
+end t#e "ount .a"/ to t#e +ender5 int toBpipe = open! argv, Writing &;
rite! toBpipe, -"ount, 4 &;
"lo+e! toBpipe &;
e3it! 0 &;
}
,i t
7/25/2019 chap03 from OSD Crowley
47/58
01/29/16 Crowley OS Chap. 3 7
,ipe% create processes void main! int arg", "#ar $ argv< > & {
int pid1 = Create9ro"e++Wit#Arg+!)Fileend),
)Fileoend), )9ipeoRe"eiver), )9ipeoender)&; int pid2 = Create9ro"e++Wit#Arg+! )FileRe"eive),
)9ipeoRe"eiver), )9ipeoender) &;
int ret1 = ait! pid1 &;
int ret2 = ait! pid2 &;
e3it! 0 &;
}
7/25/2019 chap03 from OSD Crowley
48/58
01/29/16 Crowley OS Chap. 3 8
>ore on naming
! @e ha(e seen three naming systems$Global character namesin the file system%
name# pipes
$Process-local names*file i#entifiers+%
anonymo)s pipes
$Global integer namespic'e# 4y the OS%
message
7/25/2019 chap03 from OSD Crowley
49/58
01/29/16 Crowley OS Chap. 3 9
;esign techni
7/25/2019 chap03 from OSD Crowley
50/58
01/29/16 Crowley OS Chap. 3 0
OS e-amples
! AIE *TT& Fell a4s+$Fasis of most mo#ern OSes
! >ach *C>+
$>icro'ernel$:esearch system& now wi#ely )se#
! >S/;OS *>icrosoft+
$Aot a f)ll OS
OS l
7/25/2019 chap03 from OSD Crowley
51/58
01/29/16 Crowley OS Chap. 3 1
>ore OS e-amples
! @in#ows AT *>icrosoft+$S)ccessor to >S/;OS
! OS/2 *IF>+
! >acintosh OS *pple+$Inno(ations in the GI
$To 4e replace# 4y :hapso#y *>ach+
7/25/2019 chap03 from OSD Crowley
52/58
01/29/16 Crowley OS Chap. 3 2
Shell% an OS interface
! Interacti(e access to the OS system calls
$copy from"ile to"ile
!Contains a simple programming lang)age
! ,op)lari=e# 4y AIE
$Fefore AIE% HC& OS Cs *comman#
lang)ages+
$Fo)rne shell& C shell *csh+& orn shell *'sh+&
Fo)rneagain shell *4ash+& etc.
7/25/2019 chap03 from OSD Crowley
53/58
01/29/16 Crowley OS Chap. 3 3
Two (iews of a shell
Shell% glo4als
7/25/2019 chap03 from OSD Crowley
54/58
01/29/16 Crowley OS Chap. 3
Shell% glo4als ?in"lude (io+tream5#@
+ome "on+tant+
ma3imum +iGe o% an one argument"on+t int ARLMN* H0
ma3imum num.er o% argument+
"on+t int OARL 20
to/en tpe+ returned . getWord!&
"on+t int RMOL 1
"on+t int MOR*'MR 2
"on+t int EPR*'MR
"on+t int O*WKMO* 4
de%ine t#e argv +tru"ture
"#ar $argv; +pa"e %or argv ve"tor
"#ar arg+; +pa"e %or argument+
Shell *1 of 3+
7/25/2019 chap03 from OSD Crowley
55/58
01/29/16 Crowley OS Chap. 3
Shell *1 of 3+ void main! int arg"ount, "#ar $argument+< > & {
int a+MnRedir, a+EutRedir;
"#ar inRedir, outRedir; ea"# iteration ill par+e one "ommand
#ile! 1 & {
di+pla t#e prompt
"out (( )Q );
o %ar e #ave not +een an redire"tion+
a+MnRedir = 0;
a+EutRedir = 0; et up +ome ot#er varia.le+5
int arg" = 0;
int done = 0;
"#ar ord;
Read one line %rom t#e u+er5
#ile! Jdone & { getWord get+ one ord %rom t#e line5
int argpe = getWord!ord&;
getWord return+ t#e tpe o% t#e ord it read
Shell *2 of 3+
7/25/2019 chap03 from OSD Crowley
56/58
01/29/16 Crowley OS Chap. 3 6
Shell *2 of 3+ +it"#! argpe & {
"a+e MOR*'MR
a+MnRedir = 1;
!void&getWord!inRedir&; .rea/;
"a+e EPR*'MR
a+EutRedir = 1;
!void&getWord!outRedir&;
.rea/;
"a+e RMOL
+tr"p!arg+ = -arg+ = OPKK;
i%! +tr"mp!arg+, )logout)& == 0 &
.rea/;
Shell *3 of 3+
7/25/2019 chap03 from OSD Crowley
57/58
01/29/16 Crowley OS Chap. 3 7
Shell *3 of 3+ i%! %or/!& == 0 & {
i%! a+MnRedir & {
"lo+e!0&; "lo+e +tandard input
open!inRedir, 0&; reopen a+ redire"t %ile } i%! a+EutRedir & {
"lo+e!1&; "lo+e +tandard output
enum { P+erWrite=0SHH };
"reat!outRedir, P+erWrite&; }
"#ar "md;
+tr"p!"md, )5)&; +tr"at!"md, arg+&; e3e"v!"md, -argv&;
+tr"p!"md, ).in)&; +tr"at!"md, arg+&;
e3e"v!"md, -argv&;
"out (( )C#ild "ould not e3e" :)) (( arg+
(( ):):n);
e3it!1&;
} int +tatu+; !void& ait!-+tatu+&;
}
"out (( )#ell e3iting5:n);
}
;esign techni
7/25/2019 chap03 from OSD Crowley
58/58
01/29/16 C l OS Ch 3 8
;esign techni& CO:F
! It is )sef)l for a program to ha(e 4oth