Sie sind auf Seite 1von 43

CS 2257 - Operating Systems Laboratory

CS 2257 OPERATING SYSTEMS LAB 0 0 3 2


(Common to CSE & IT)
(Implement the follo!n" on LIN#$ o% othe% #n!& l!'e pl(tfo%m) #*e C fo% h!"h le+el
l(n",("e !mplement(t!on-
1. Write programs using the following system calls of UI! operating system"
for#$ e%ec$ getpi&$ e%it$ wait$ close$ stat$ open&ir$ rea&&ir
'. Write programs using the I() system calls of UI! operating system (open$ rea&$
write$ etc)
*. Write C programs to simulate UI! comman&s li#e ls$ grep$ etc.
+. ,i-en the list of processes$ their C.U /urst times an& arri-al times$ &isplay(print
the ,antt chart for 0C0S an& S10. 0or each of the sche&uling policies$ compute
an& print the a-erage waiting time an& a-erage turnaroun& time. (' sessions)
2. ,i-en the list of processes$ their C.U /urst times an& arri-al times$ &isplay(print
the ,antt chart for .riority an& 3oun& ro/in. 0or each of the sche&uling policies$
compute an& print the a-erage waiting time an& a-erage turnaroun& time. ('
sessions)
4. 5e-eloping 6pplication using Inter .rocess communication (using share&
memory$ pipes or message 7ueues)
8. Implement the .ro&ucer 9 Consumer pro/lem using semaphores (using UI!
system calls).
:. Implement some memory management schemes 9 I
;. Implement some memory management schemes 9 II
1<. Implement any file allocation techni7ue (=in#e&$ In&e%e& or Contiguous)
E&(mple fo% e&e%.!*e* / 0 1 2
0ree space is maintaine& as a lin#e& list of no&es with each no&e ha-ing the starting /yte
a&&ress an& the en&ing /yte a&&ress of a free /loc#. Each memory re7uest consists of the
process>i& an& the amount of storage space re7uire& in /ytes. 6llocate& memory space is
again maintaine& as a lin#e& list of no&es with each no&e ha-ing the process>i&$ starting
/yte a&&ress an& the en&ing /yte a&&ress of the allocate& space. When a process finishes
(ta#en as input) the appropriate no&e from the allocate& list shoul& /e &elete& an&
this free &is# space shoul& /e a&&e& to the free space list. ?Care shoul& /e ta#en to merge
contiguous free /loc#s into one single /loc#. This results in &eleting more than one no&e
from the free space list an& changing the start an& en& a&&ress in the appropriate no&e@. 0or
allocation use first fit$ worst fit an& /est fit.
TOTAL2 35
1
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
4(%5(%e (n5 Soft(%e %e6,!%e5 fo% ( 7(t.h of 30 *t,5ent*)
4AR89ARE2
*< .ersonal Computers
SO:T9ARE2
L!n,&2
U/untu ( )penSUSE ( 0e&ora ( 3e& Aat ( 5e/ian ( Bint )S
=inu% coul& /e loa&e& in in&i-i&ual .Cs.
(OR-
6 single ser-er coul& /e loa&e& with =inu% an& connecte& from the in&i-i&ual .Cs.
2
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
1. Process , Directory & File Management system calls
Aim :
o !rite C programs for the fork, e"e#, getpi$, e"it, !ait, stat, open$ir, rea$$ir
system #alls of %&'( operating system)
Description
* system #all is +ust !hat its name implies - a re,uest for the operating system to $o
something on behalf of the user-s program) he system #alls are fun#tions use$ in the kernel
itself) o the programmer, the system #all appears as a normal C fun#tion #all) .o!e/er
sin#e a system #all e"e#utes #o$e in the kernel, there must be a me#hanism to #hange the
mo$e of a pro#ess from user mo$e to kernel mo$e) he C #ompiler uses a pre$efine$ library
of fun#tions 0the C library1
that ha/e the names of the system #alls) he library fun#tions typi#ally in/oke an instru#tion
that #hanges the pro#ess e"e#ution mo$e to kernel mo$e an$ #auses the kernel to start
e"e#uting #o$e for system #alls) he instru#tion that #auses the mo$e #hange is often
referre$ to as an 2operating system trap2 !hi#h is a soft!are generate$ interrupt)
he library routines e"e#ute in user mo$e, but the system #all interfa#e is a spe#ial #ase of
an interrupt han$ler) he library fun#tions pass the kernel a uni,ue number per system #all in
a ma#hine $epen$ent !ay
getpid, getppid
o get the pro#ess an$ parent pro#ess i$entifi#ation number)
3eneral Synta"4

pi$5t getpi$0/oi$16
pi$5t getppi$0/oi$16
7es#ription
getpi$ returns the pro#ess '7 of the #urrent pro#ess) 0his is often use$ by routines that
generate uni,ue temporary file names)1
getppi$ returns the pro#ess '7 of the parent of the #urrent pro#ess)
fork()
#auses the %&'( system to #reate a ne! pro#ess, #alle$ the 2#hil$ pro#ess2, !ith a ne!
pro#ess '7) he #ontents of the #hil$ pro#ess are i$enti#al to the #ontents of the parent
pro#ess)
he prototype for the fork01 system #all is4
int fork01

8
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
he ne! pro#ess inherits se/eral #hara#teristi#s of the ol$ pro#ess) *mong the
#hara#teristi#s inherite$ are4
he en/ironment)
*ll signal settings)
he set user '7 an$ set group '7 status)
he time left until an alarm #lo#k signal)
he #urrent !orking $ire#tory an$ the root $ire#tory)
he file #reation mask as establishe$ !ith umask01)
he #hil$ pro#ess begins e"e#uting an$ the parent pro#ess #ontinues e"e#uting at the
return from the fork01 system #all) his is $iffi#ult to un$erstan$ at first be#ause you only
#all fork01 on#e, yet it returns t!i#e -- on#e per pro#ess) o $ifferentiate !hi#h pro#ess is
!hi#h, fork01 returns 9ero in the #hil$ pro#ess an$ non-9ero 0the #hil$-s pro#ess '71 in the
parent pro#ess)
exec system call
he %&'( system #alls that transform a e"e#utable binary file into a pro#ess are the
2e"e#2 family of system #alls) he prototypes for these #alls are4
int e"e#l0file5name, arg: ;, arg1, ))), argn<, &%LL1
#har =file5name, =arg:, =arg1, ))), =argn6
ait()
>ou #an #ontrol the e"e#ution of #hil$ pro#esses by #alling !ait01 in the parent) !ait01
for#es the parent to suspen$ e"e#ution until the #hil$ is finishe$) !ait01 returns the pro#ess
'7 of a #hil$ pro#ess that finishe$)
'f the #hil$ finishes before the parent gets aroun$ to #alling !ait01, then !hen !ait01 is
#alle$ by the parent, it !ill return imme$iately !ith the #hil$-s pro#ess '7) 0't is possible to
ha/e more that one #hil$ pro#ess by simply #alling fork01 more than on#e)1) he prototype
for the !ait01 system #all is4
int !ait0status1
int =status6
!here status is a pointer to an integer !here the %&'( system stores the /alue returne$ by
the #hil$ pro#ess) !ait01 returns the pro#ess '7 of the pro#ess that en$e$)
exit()
he e"it01 system #all en$s a pro#ess an$ returns a /alue to it parent) he prototype for
the e"it01 system #all is4
/oi$ e"it0status1
int status6
!here status is an integer bet!een : an$ 255)
?
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
&ote4 sin#e !ait01 returns the e"it status multiplie$ by 25@ 0#ontaine$ in
the upper A bits1, the status /alue is shifte$ right A bits 0$i/i$e$ by 25@1
to obtain the #orre#t /alue)
File !tat"s
stat01 ,fstat01 , lstat01
he i-no$e $ata stru#ture hol$s all the information about a file e"#ept the file-s name an$
its #ontents) Sometimes your programs nee$ to use the information in the i-no$e stru#ture to
$o some +ob) >ou #an a##ess this information !ith the stat01, lstat01 an$ fstat01 system #alls)
opendir - open a $ire#tory
S>&OBS'S
Cin#lu$e DsysEtypes)hF
Cin#lu$e D$irent)hF
7'G =open$ir0#onst #har =name16
7ESCG'B'O&
he open$ir01 fun#tion opens a $ire#tory stream #orrespon$ing to the $ire#tory name,
an$ returns a pointer to the $ire#tory stream) he stream is positione$ at the first entry in
the $ire#tory)
GE%G& H*L%E
he open$ir01 fun#tion returns a pointer to the $ire#tory stream or &%LL if an error
o##urre$)
readdir - rea$ $ire#tory entry
rea$$ir rea$s one $irent stru#ture from the $ire#tory pointe$ at by f$ into the memory area
pointe$ to by $irp) he parameter #ount is ignore$6 at most one $irent stru#ture is rea$)
int rea$$ir0unsigne$ int f$, stru#t $irent =$irp, unsigne$ int #ount16
closedir - #lose a $ire#tory
int #lose$ir07'G =$ir16
7ESCG'B'O&
he #lose$ir01 fun#tion #loses the $ire#tory stream asso#iate$ !ith $ir) he $ire#tory
stream $es#riptor $ir is not a/ailable after this #all)
5
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
#xercises:
$% 1. Program to print t&e process id %$
Algorit&m:
1) 'n#lu$e ne#essary hea$er files for using systems #alls)
2) Make ne#essary $e#laration)
8) 3et the pro#ess i$entifi#ation number an$ parent pro#ess i$entifi#ation number using
getpi$01 an$ getppi$01 system #alls
?) 7isplay the pro#ess i$Is
Cin#lu$e DsysEtypes)hF
Cin#lu$e Dunist$)hF
Cin#lu$eDst$io)hF
main01
J
int pi$,ppi$6
pi$Kgetpi$016
ppi$Kgetppi$016
printf02Ln Bro#ess '$ is M$Ln2,pi$16
printf02Ln Barent Bro#ess '$ is M$Ln2,ppi$16
N
'()P()
pro#ess '7 is 51OA
parent pro#ess '7 is 512O
$% *. Program "sing fork system call %$
Cin#lu$e DsysEtypes)hF
Cin#lu$e Dunist$)hF
Cin#lu$eDst$io)hF
main01
J
printf02Ln his is to $emonstrate the fork01216
fork016
printf02Ln*fter fork01216
N
'()P()
this is to $emonstrate fork01
after fork01
$% +. fork and pid %$
Cin#lu$e DsysEtypes)hF
Cin#lu$e Dunist$)hF
Cin#lu$eDst$io)hF
main01
@
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
J
int pi$6
pi$Kfork016
if0pi$KK:1
J
printf02Ln ' am the #hil$, my pro#ess '7 is M$ 2,getpi$0116
printf02Ln ' am the #hil$-s parent pro#ess '7 is M$ 2,getppi$0116
N
else
J
printf02Ln ' am the parent, my pro#ess '7 is M$ 2,getpi$0116
printf02Ln ' am the parent-s parent pro#ess '7 is M$ 2,getppi$0116
N
N
'()P()
i am the parent, my pro#ess '7 is 5278
i am the parent-s parent,pro#ess '7 is 512O
' am the #hil$, my pro#ess '7 is 527?
' am the #hil$-s parent, pro#ess '7 is 1
$% ,. orp&an process %$
Cin#lu$e DsysEtypes)hF
Cin#lu$e Dunist$)hF
Cin#lu$eDst$io)hF
main01
J
int pi$6
pi$Kfork016
if0pi$KK:1
J
printf02Ln ' am the #hil$, my pro#ess '7 is M$ 2,getpi$0116
printf02Ln ' am the #hil$-s parent pro#ess '7 is M$ 2,getppi$0116
sleep01:16
printf02Ln ' am the #hil$, my pro#ess '7 is M$ 2,getpi$0116
printf02Ln ' am the #hil$-s parent pro#ess '7 is M$ 2,getppi$0116
N
else
J
printf02Ln ' am the parent, my pro#ess '7 is M$ 2,getpi$0116
printf02Ln ' am the parent-s parent pro#ess '7 is M$ 2,getppi$0116
N
N
'()P()
' am the #hil$, my pro#ess '7 is 58:1
' am the parent, my pro#ess i$ is 58::
7
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
' am the parent-s parent, my pro#ess '7 is 512O
;it2-21Plo#alhost Q<R ' am the
#hil$-s parent, my pro#ess '7 is 58::
' am the #hil$, my pro#ess '$ is 58:1
' am the #hil$-s parent, my pro#ess '7 is 1
$% -. exec system call %$
Cin#lu$e DsysEtypes)hF
Cin#lu$eDst$io)hF
Cin#lu$e Dunist$)hF
main01
J
e"e#l02EbinEls2, 2EbinEls2, 2-r2, 2-t2, 2-l2, 0#har =1 :16
N
O%B%
total 8A?
-r!-r!-r-- 1 it2-21 it2-21 85O O#t 1? :A42? pol)#pp
-r!-r!-r-- 1 it2-21 it2-21 2:: 7e# A 11485 forks)#
-r!-r!-r-- 1 it2-21 it2-21 1:2 7e# 1? 1:4?A e"e#1)#
-r!-r!-r-- 1 it2-21 it2-21 2A8 7e# 1? 114:@ !ait)#
-r!-r!-r-- 1 it2-21 it2-21 2?@ 7e# 1? 11422 e"it)#
-r!-r!-r-- 1 it2-21 it2-21 21? 7e# 1? 11425 e"it1)#
-r!-r!-r-- 1 it2-21 it2-21 8O8 7e# 1? 12412 opre)#
-r!-r!-r-- 1 it2-21 it2-21 8@7 7e# 1? 12418 fileinfo)#
-r!-r!-r-- 1 it2-21 it2-21 : 7e# 1? 1242: rea$)#pp
-r!-r!-r-- 1 it2-21 it2-21 17A@ 7e# 1A :O45: poly)#pp
-r!-r!-r-- 1 it2-21 it2-21 158 7e# 21 11481 op)#
-r!-r!-r-- 1 it2-21 it2-21 : 7e# 21 11482 buf,#
-r!-r!-r-- 1 it2-21 it2-21 1O7 7e# 21 11482 buf)#
---------" 1 it2-21 it2-21 : 7e# 21 11488 fn
$% .. exec system call %$
Cin#lu$e Dunist$)hF
Cin#lu$e DsysEtypes)hF
Cin#lu$eDst$io)hF
main01
J
e"e#l02EbinE$ate2, 2$ate2, &%LL16
N
'()P()
Mon San 11 114:A45: %C 2:1:
A
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
$% /. ait system call %$
$% process sync&roni0ation %$
Cin#lu$e DsysEtypes)hF
Cin#lu$e Dunist$)hF
Cin#lu$eDst$io)hF
main01
J
int pi$,iK:6
printf02Ln Gea$y to fork216
pi$Kfork016
if0pi$KK:1
J
printf02Ln Chil$ starts 216
for0iK:6iD1:::6iTT16
printf02Ln Chil$ en$s 216
N
else
J
!ait0:16
for0iK:6iD1:::6iTT16
printf02Ln Barent pro#ess en$s 216
N
N
'()P()
rea$y to fork
#hil$ starts
#hil$ en$s
rea$y to fork
parent pro#ess en$s
$% 1. Program "sing exit system call %$
Cin#lu$e DsysEtypes)hF
Cin#lu$e Dunist$)hF
Cin#lu$eDst$io)hF
main01
J
int p6
pKfork016
if0pKK:1
J
printf02Ln Chil$ #reate$ 216
e"it0:16
printf02Ln Bro#ess en$e$ 216
O
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
N
if0pD:1
J
printf02Ln Cannnot #reate #hil$ 216
e"it0-116
printf02Ln Bro#ess en$e$ 216
N
N
'()P()
#hil$ #reate$
$% 2. Program "sing exit and ait system call %$
Cin#lu$e DsysEtypes)hF
Cin#lu$e Dunist$)hF
Cin#lu$eDst$$$io)hF
main01
J
unsigne$ int status6
if 0 fork 01 KK : 1 J E= KK : means in #hil$ =E
s#anf 02M$2, Ustatus16
e"it 0status16
N
else J E= VK : means in parent =E
!ait 0Ustatus16
printf02#hil$ e"it status K M$Ln2, status F A16
N
N
'()P()
#hil$ e"it statusK1
$% 13. Program "sing lstat() system call %$
Cin#lu$eDst$io)hF
Cin#lu$eDsysEstat)hF
main0int arg#,#har ==arg/1
J
stru#t stat statbuf6
if0lstat0arg/;1<,Ustatbuf1KK-11
printf02Error 4 #annot fin$ file information 216
else
J
printf02Ln Wile Ms 2, arg/;1<16
printf02Ln 'no$e number M$2,statbuf)st5ino16
printf02Ln %'7 M$2,statbuf)st5ui$16
1:
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
printf02Ln 3'7 M$2,statbuf)st5gi$16
printf02Ln Wile si9e in bytes M$2,statbuf)st5si9e16
N
N
'()P()
)Ea)out opre)#
file opre)#' no$e number 18A72@:%'7 OOA3'7 OOOfile si9e in bytes 8O8
$% 11. Program "sing opendir and readdir system call %$
Cin#lu$eDst$io)hF
Cin#lu$e DsysEtypes)hF
Cin#lu$e D$irent)hF
int main0int arg#, #har =arg/;<1
J
7'G =$p6
stru#t $irent =$irp6
if 0arg# VK 21 J
printf02a single argument 0the $ire#tory name1 is re,uire$Ln216
e"it0116
N
if 0 0$p K open$ir0arg/;1<11 KK &%LL1 J
printf02#an-t open MsLn2,arg/;1<16
e"it0116
N
!hile 0 0$irp K rea$$ir0$p11 VK &%LL1
printf02Ms M$Ln2,$irp-F$5name,$irp-F$5ino16
#lose$ir0$p16
e"it0:16
N
'()P()
)Ea)out
grep)# 18A7278
simu)# 18A727?
a)out 18A72@5
) 18A72?7
hi 18A7288
)) 18A7211
rem)# 18A727@
4es"lt :
hus C program !as !ritten programs using the fork, e"e#, getpi$, e"it, !ait, stat,
open$ir, rea$$ir system #alls of %&'( operating system)
11
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
*. File realated system call 5 open(), read(), rite()
Aim :
o !rite a C program using open01, rea$01, !rite an$ #lose system #alls)
Description
open()
open01 lets you open a file for rea$ing, !riting, or rea$ing an$ !riting) he prototype for the
open01 system #all is4
int open0#har =file5name, int option5flags int ;, mo$e<1
!here file5name is a pointer to the #hara#ter string that names the file, option5flags represent
the type of #hannel, an$ mo$e $efines the file-s a##ess permissions if the file is being #reate$)
read() rite()
he rea$01 system #all $oes all input an$ the !rite01 system #all $oes all output)Xhen use$
together, they pro/i$e all the tools ne#essary to $o input an$ output se,uentially)
Yoth rea$01 an$ !rite01 take three arguments) heir prototypes are4
int rea$0int file5$es#riptor, #har = buffer5pointer,unsigne$ transfer5si9e1
int !rite0int file5$es#riptor,#har = buffer5pointer, unsigne$ transfer5si9e1
!here file5$es#riptor i$entifies the 'EO #hannel, buffer5pointer points to the area in memory
!here the $ata is store$ for a rea$01 or !here the $ata is taken for a !rite01, an$ transfer5si9e
$efines the ma"imum number of #hara#ters transferre$ bet!een the file an$ the buffer) rea$01
an$ !rite01 return the number of bytes transferre$)
close()
o #lose a #hannel, use the #lose01 system #all) he prototype for the #lose01 system #all is4
int #lose0int file5$es#riptor1
!here file5$es#riptor i$entifies a #urrently open #hannel) #lose01 fails if file5$es#riptor $oes
not i$entify a #urrently open #hannel)
#xercises
$% 1. "sing creat system call %$
Cin#lu$e Dst$io)hF
Cin#lu$e DsysEtypes)hF E= $efines types use$ by sysEstat)h =E
Cin#lu$e DsysEstat)hF E= $efines S5'GE*7 U S5'XG'E =E
int main01
J
int f$6
f$ K #reat02$atafile)$at2, S5'GE*7 Z S5'XG'E16
if 0f$ KK -11
printf02Error in opening $atafile)$atLn216
else
12
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
J
printf02$atafile)$at opene$ for rea$E!rite a##essLn216
printf02$atafile)$at is #urrently emptyLn216
N
#lose0f$16
e"it 0:16
N
$% *. "sing open, rite system call %$
Cin#lu$eDf#ntl)hF
Cin#lu$eDunist$)hF
main01
J
int f$,i6
f$Kopen02test2,O5CGE*ZO5G7XGZO5*BBE&7,:@@@16
for0iK:6iD1::6iTT1
!rite0f$,2*2,116
#lose0f$16
N
'()P():
****************************************************
****************************************************
****************************************************
********************************************
$% +. "sing 6 open ,read and rite system calls %$
$% reading a content from file and displaying it to monitor %$
Cin#lu$eDst$io)hF
Cin#lu$eDf#ntl)hF
main01
J
#har buf;1::<,fn;1:<6
int f$,n6
printf02Enter file name 216
s#anf02Ms2,fn16
f$Kopen0fn,O5G7O&L>16
nKrea$0f$,buf,1::16
nK!rite01,buf,n16EE!rite to monitor
#lose0f$16
N
'()P()
enter file name open)#
Cin#lu$eDst$io)hF
Cin#lu$eDf#ntl)hF
main01
J
#har buf;1:::<,fn1;1:<,fn2;1:<6
int f$1,f$2,n6
18
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
$% ,. "sing 6 rite system calls
riting line of text t&e file %$
Cin#lu$eDst$io)hF
Cin#lu$eDf#ntl)hF
Cin#lu$eDstring)hF
main01
J
#har buf;1::<,fn;1:<6
int f$,n6
printf02Enter file name 216
s#anf02Ms2,fn16
printf02LnEnter te"t 216
gets0buf16
f$Kopen0fn,O5CGE*Z:@@@16
nK!rite0f$,buf,strlen0buf116EE!rite file
#lose0f$16
N
'()P():
enter file name open)#
enter te"t
.' V
.'n#lu$eDst$io)hF
Cin#lu$eDf#ntl)hF
main01
J
#har buf;1:::<,fn1;1:<,fn2;1:<6 [[
N
$% -. "sing 6 open ,read and rite system calls
file copy operation%$
Cin#lu$eDst$io)hF
Cin#lu$eDf#ntl)hF
main01
J
#har buf;1:::<,fn1;1:<,fn2;1:<6
int f$1,f$2,n6
printf02Enter sour#e file name 216
s#anf02Ms2,fn116
printf02Enter $estination file name 216
s#anf02Ms2,fn216
f$1Kopen0fn1,O5G7O&L>16
nKrea$0f$1,buf,1:::16
f$2Kopen0fn2,O5CGE*Z:@@@16
nK!rite0f$2,buf,n16EE!rite file
#lose0f$116
#lose0f$216
N
1?
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
'()P()
enter sour#e file name !rite)#
enter $estination file name rea$)#
Cin#lu$eDst$io)hF
Cin#lu$eDf#ntl)hF
Cin#lu$eDstring)hF
main01
J
#har buf;1::<,fn;1:<6
int f$,n6
printf02enter file name216
s#anf02Ms2,fn16
printf02Ln enter te"tLn216
s#anf02Ms2,buf16
f$Kopen0fn,O5CGE*ZO5XGO&L>,:@@@16
nK!rite0f$,buf,strlen0buf116
#lose0f$16
N
4es"lt :
hus C program !as !ritten using open01, rea$01, !rite01 an$ #lose01 system #alls)
15
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
+. !im"lation of (nix 7ommands
Aim :
o !rite a C program to simulate basi# %ni" #omman$s like ls,grep, #p,rm
Description :
%&'( #omman$s #an be simulate$ in the high le/el language using %ni" system #alls
an$ *B's a/ailable in the language) 'n a$$ition, programming language #onstru#t #an also
be use$ to a/ail the %&'( #omman$s)
#xercises :
1. !i"m"lation of ls command
Algorit&m :
1) 'n#lu$e ne#essary hea$er files for manipulating $ire#tory)
2) 7e#lare an$ initiali9e re,uire$ ob+e#ts)
8) Gea$ the $ire#tory name form the user
?) Open the $ire#tory using open$ir01 system #all an$ report error if the $ire#tory is
not a/ailable
5) Gea$ the entry a/ailable in the $ire#tory
@) 7isplay the $ire#tory entry 0ie), name of the file or sub $ire#tory)
7) Gepeat the step @ an$ 7 until all the entries !ere rea$)
$% 1. !i"m"lation of ls command %$
Cin#lu$eD$irent)hF
Cin#lu$eDst$io)hF
main01
J
#har $irname;1:<6
7'G =p6
stru#t $irent =$6
printf02Enter $ire#tory name 216
s#anf02Ms2,$irname16
pKopen$ir0$irname16
if0pKK&%LL1
J
perror02Cannot fin$ $ir)216
e"it0-116
N
!hile0$Krea$$ir0p11
printf02MsLn2,$-F$5name16
N
'()P():
enter $ire#tory name iii
)
))
f2
f1
1@
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
*. !im"lation of grep command.
Algorit&m :
1) 'n#lu$e ne#essary hea$er files
2) Make ne#essary $e#larations
8) Gea$ the file name from the user an$ open the file in the rea$ only mo$e)
?) Gea$ the pattern from the user)
5) Gea$ a line of string from the file an$ sear#h the pattern in that line)
@) 'f pattern is a/ailable, print the line)
7) Gepeat the step ? to @ till the en$ of the file)
$% *. !im"lation of grep command %$
Cin#lu$eDst$io)hF
Cin#lu$eDstring)hF
main01
J
#har fn;1:<,pat;1:<,temp;2::<6
W'LE =fp6
printf02Ln Enter file name 4 216
s#anf02Ms2,fn16
printf02Enter the pattern to be sear#he$ 4 216
s#anf02Ms2,pat16
fpKfopen0fn,2r216
!hile0Vfeof0fp11
J
fgets0temp,1:::,fp16
if0strstr0temp,pat11
printf02Ms2,temp16
N
f#lose0fp16
N
'()P():
enter file name4 file?
enter the pattern to be sear#he$4 roll
roll no per#entage gra$e
+. !im"lation of cp command
Algorit&m:
1) 'n#lu$e re,uire$ hea$er file
2) Make ne#essary $e#larations
8) Gea$ the sour#e an$ $estination file names from the user)
?) %sing rea$01 system #all, rea$ the #ontent of the file to the buffer)
5) %ing !rite01 system #all, !rite the buffer #ontent to the $estination file)
@) Close the opene$ files)
17
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
$% "sing 6 open ,read and rite system calls
file copy operation%$
Cin#lu$eDst$io)hF
Cin#lu$eDf#ntl)hF
main01
J
#har buf;1:::<,fn1;1:<,fn2;1:<6
int f$1,f$2,n6
printf02Enter sour#e file name 216
s#anf02Ms2,fn116
printf02Enter $estination file name 216
s#anf02Ms2,fn216
f$1Kopen0fn1,O5G7O&L>16
nKrea$0f$1,buf,1:::16
f$2Kopen0fn2,O5CGE*Z:@@@16
nK!rite0f$2,buf,n16EE!rite file
#lose0f$116
#lose0f$116
N
'()P():
enter sour#e file name
ls)#
enter $estination file name
fileA
;it2-2:Plo#alhost Q<R #at fileA
Cin#lu$eD$irent)hF
Cin#lu$eDst$io)hF
main01
J
#har $irname;1:<6
7'G =p6
stru#t $irent =$6
printf02enter $ire#tory name216
s#anf02Ms2,$irname16
pKopen$ir0$irname16
if0pKK&%LL1
J
perror02#annot fin$ $ire#tory216
e"it0-116
N
!hile0$Krea$$ir0p11
printf02MsLn2,$-F$5name16
N
1A
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
$% ,. to remo8e file or dir. %$
Algorit&m:
1) 'n#lu$e re,uire$ hea$er file
2) Make ne#essary $e#larations
8) Gea$ the file name from the user)
?) %sing remo/e 01 system #all remo/e the file from the user)
Cin#lu$eDst$io)hF
Cin#lu$eDf#ntl)hF
main01
J
#har fn;1:<6
printf02Enter sour#e file name 216
s#anf02Ms2,fn16
if0remo/e0fn1KK:1
printf02LnWileE7ir remo/e$216
else
printf02LnCannot be remo/e$216
N
'()P():
enter sour#e file name
file1:
fileE$ire#tory remo/e$
4es"lt :
hus C program !as !ritten to simulate some %ni" Comman$s
1O
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
,. Process !c&ed"ling 5 F7F!, !9F
Aim :
o !rite C program to simulate WCWS an$ SSW s#he$uling algorithms)
CPU/Process Scheduling
The assignment of physical processors to processes allows processors to accomplish work.
The problem of determining when processors should be assigned and to which processes is
called processor scheduling or CPU scheduling.
When more than one process is runable, the operating system must decide which one first.
The part of the operating system concerned with this decision is called the scheduler, and
algorithm it uses is called the scheduling algorithm.
First-Come-First-Served (FCFS) Scheduling
Other names of this algorithm are:
First-In-First-Out (FIFO)
Run-to-Completion
Run-Until-Done
Perhaps, First-Come-First-Served algorithm is the simplest scheduling algorithm is the
simplest scheduling algorithm. Processes are dispatched according to their arrival time on the
ready queue. Being a nonpreemptive discipline, once a process has a CPU, it runs to
completion. The FCFS scheduling is fair in the formal sense or human sense of fairness but it
is unfair in the sense that long jobs make short jobs wait and unimportant jobs make
important jobs wait.
FCFS is more predictable than most of other schemes since it offers time. FCFS scheme is
not useful in scheduling interactive users because it cannot guarantee good response time.
The code for FCFS scheduling is simple to write and understand. One of the major drawback
of this scheme is that the average time is often quite long.
Shortest-Job-First (SJF) Scheduling
Other name of this algorithm is Shortest-Process-Next (SPN).
Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process) with
the smallest estimated run-time-to-completion is run next. In other words, when CPU is
available, it is assigned to the process that has smallest next CPU burst.
The SJF scheduling is especially appropriate for batch jobs for which the run times are
known in advance. Since the SJF scheduling algorithm gives the minimum average time for a
given set of processes, it is probably optimal.
The SJF algorithm favors short jobs (or processors) at the expense of longer ones.
The obvious problem with SJF scheme is that it requires precise knowledge of how long a job
or process will run, and this information is not usually available.
The best SJF algorithm can do is to rely on user estimates of run times.
2:
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
In the production environment where the same jobs run regularly, it may be possible to
provide reasonable estimate of run time, based on the past performance of the process. But in
the development environment users rarely know how their program will execute.
Like FCFS, SJF is non preemptive therefore, it is not useful in timesharing environment in
which reasonable response time must be guaranteed.
F7F! !7:#D(;<=>
Cin#lu$eDst$io)hF
stru#t pro#ess
J
#har p&ame;1:<6
int e"5time,!t5time,st5time,en$5time6
Np;1:<6
main01
J
int n,i,+,k6
float a/g!aittimeK:):,a/gurn*roun$imeK:):6
float totalXaitimeK:):6
int totalE"e#imeK:,totalurn*roun$imeK:6
printf02Ln enter number of pro#ess216
s#anf02M$2,Un16
p;:<)st5timeK:6
p;:<)!t5timeK:6
printf02Ln enter pro#ess name216
s#anf02Ms2,p;i<)p&ame16
printf02enter Yurst time216
s#anf02M$2,Up;i<)e"5time16
if0iKK:1 p;i<)en$5timeKp;i<)e"5time6
else
J
p;i<)!t5timeKp;i-1<)en$5time6
p;i<)st5timeKp;i-1<)en$5time6
p;i<)en$5timeKp;i<)st5timeTp;i<)e"5time6
N
N
for0+K:6+Dn6+TT1
J
totalE"e#imeTKp;+<)e"5time6
totalXaitimeTKp;+<)!t5time6
N
totalurn*roun$imeKtotalE"e#imeTtotalXaitime6
a/g!aittimeK0float1totalXaitimeEn6

a/gurn*roun$imeK0float1totalurn*roun$imeEn6
printf02LnLn &ame Yurst Start En$ Xait imeLn216
for0kK:6kDn6kTT1
printf02LnMs LtM$ LtM$ LtM$ LtM$2,p;k<)p&ame,p;k<)e"5time,p;k<)st5time,p;k
<)en$5time,p;k<)!t5time16
21
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
printf02Ln*/erage Xaiting ime Mf2,a/g!aittime16
printf02Ln */erage urn *roun$ ime Mf2,a/gurn*roun$ime16
N
!AMP;# <=P()$ '()P():
enter number of pro#ess 5
enter pro#ess name p1
enter Yurst time8
enter pro#ess name p2
enter Yurst time2
enter pro#ess name p8
enter Yurst time1
enter pro#ess name p?
enter Yurst time?
enter pro#ess namep5
enter Yurst time2
&ame Yurst Start En$ Xait ime
p1 8 : 8 :
p2 2 8 5 8
p8 1 5 @ 5
p? ? @ 1: @
p5 2 1: 12 1:
*/erage Xaiting ime ?)A:::::
*/erage urn *roun$ ime 7)2:::::
!&ortest 9o? First !c&ed"ling
Cin#lu$eDst$io)hF
stru#t pro#ess
J
#har pname;1:<6
int e"5time,!t5time,st5time,en$5time6
Np;1:<,temp6
main01
J
int n,i,+,k6
float a/g!aittimeK:):,a/gturnaroun$timeK:):6
22
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
float total!aittimeK:):6
int totale"e#timeK:,totalturnaroun$timeK:6
printf02Ln enter the number of pro#ess216
s#anf02M$2,Un16
p;:<)st5timeK:6
p;:<)!t5timeK:6
for0iK:6iDn6iTT1
J
printf02Ln enter pro#ess name216
s#anf02Ms2,p;i<)pname16
printf02enter burst time216
s#anf02M$2,Up;i<)e"5time16
N
for0iK:6iDn6iTT1
for0+K:6+Dn6+TT1
J
tempKp;i<6p;i<Kp;+<6p;+<Ktemp6
N
N
p;:<)!t5timeK:6
p;:<)st5timeK:6
p;:<)en$5timeKp;:<)e"5time6
for0+K:6+Dn6+TT1
J
if0+F:1
J
p;+<)!t5timeKp;+-1<)en$5time6
p;+<)st5timeKp;+-1<)en$5time6
p;+<)en$5timeKp;+<)st5timeTp;+<)e"5time6
N
totale"e#timeTKp;+<)e"5time6
total!aittimeTKp;+<)!t5time6
N
a/g!aittimeK0float1total!aittimeEn6
totalturnaroun$timeKtotale"e#timeTtotal!aittime6
a/gturnaroun$timeK0float1totalturnaroun$timeEn6
printf02LnLnname brust start en$!aittimeLn216
for0kK:6kDn6kTT1
printf02LnMsLtM$LtM$LtM$LtM$2,p;k<)pname,p;k<)e"5time,p;k<)st5time,p;k<)en$5time
,p;k<)!t5time16
printf02Lna/erage !aiting time Mf2,a/g!aittime16
printf02Ln a/erage turnaroun$time Mf2,a/gturnaroun$time16
N
28
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
!AMP;# <=P() $ '()P():
enter the number of pro#ess?

enter pro#ess namep1
enter burst time8

enter pro#ess namep2
enter burst time@

enter pro#ess namep8
enter burst time?

enter pro#ess namep?
enter burst time2


name burst start en$ !aittime
p? 2 : 2 :
p1 8 2 5 2
p8 ? 5 O 5
p2 @ O 15 O

a/erage !aiting time ?)::::::
a/erage turnaroun$time 7)75::::
4es"lt :
hus C program !as !ritten for simulating WCWS an$ SSW s#he$uling algorithms)
2?
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
-. 7P( !7:#D(;<=> P4<P'4<)@, 4'(=D 4'A<=
Aim :
o !rite a C program for simulating Briority an$ Goun$ Gobin s#he$uling algorithms)
Priority Scheduling
The basic idea is straightforward: each process is assigned a priority, and priority is allowed
to run. Equal-Priority processes are scheduled in FCFS order. The shortest-Job-First (SJF)
algorithm is a special case of general priority scheduling algorithm.
An SJF algorithm is simply a priority algorithm where the priority is the inverse of the
(predicted) next CPU burst. That is, the longer the CPU burst, the lower the priority and vice
versa.
Priority can be defined either internally or externally. Internally defined priorities use some
measurable quantities or qualities to compute priority of a process.
Examples of Internal priorities are
Time limits.
Memory requirements.
File requirements,
for example, number of open files.
CPU Vs I/O requirements.
Externally defined priorities are set by criteria that are external to operating system such as
The importance of process.
Type or amount of funds being paid for computer use.
The department sponsoring the work.
Politics.
Priority scheduling can be either preemptive or non preemptive
A preemptive priority algorithm will preemptive the CPU if the priority of the newly
arrival process is higher than the priority of the currently running process.
A non-preemptive priority algorithm will simply put the new process at the head of
the ready queue.
A major problem with priority scheduling is indefinite blocking or starvation. A solution to
the problem of indefinite blockage of the low-priority process is aging. Aging is a technique
of gradually increasing the priority of processes that wait in the system for a long period of
time.
Round Robin Scheduling
One of the oldest, simplest, fairest and most widely used algorithm is round robin (RR).
In the round robin scheduling, processes are dispatched in a FIFO manner but are given a
limited amount of CPU time called a time-slice or a quantum.
25
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
If a process does not complete before its CPU-time expires, the CPU is preempted and given
to the next process waiting in a queue. The preempted process is then placed at the back of
the ready list.
Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in
time-sharing environments in which the system needs to guarantee reasonable response times
for interactive users.
The only interesting issue with round robin scheme is the length of the quantum. Setting the
quantum too short causes too many context switches and lower the CPU efficiency. On the
other hand, setting the quantum too long may cause poor response time and appoximates
FCFS.
In any event, the average waiting time under round robin scheduling is often quite long.
Priority !c&ed"ling
Cin#lu$eDst$io)hF
stru#t pro#ess
J
#har pname;1:<6
int e"5time,!t5time,st5time,en$5time,turn5time,priority6
Np;1:<,temp6
main01
J
int n,i,+,k6
float a/g!aittimeK:):,a/gturnaroun$timeK:):6
float total!aittimeK:):6
int totale"e#timeK:,totalturnaroun$timeK:6
printf02Lnenter number of pro#ess216
s#anf02M$2,Un16
p;:<)st5timeK:6
p;:<)!t5timeK:6
for0iK:6iDn6iTT1
J
printf02Lnenter pro#ess name216
s#anf02Ms2,p;i<)pname16
printf02Lnenter pro#ess priority : !ith highest priority216
s#anf02M$2,Up;i<)priority16
printf02enter burst time216
s#anf02M$2,Up;i<)e"5time16
N
for0iK:6iDn6iTT1
for0+K:6+Dn6+TT1
J
if0p;i<)priorityDp;+<)priority1
J
tempKp;i<6
p;i<Kp;+<6
p;+<Ktemp6
N
N
2@
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
if0+KK:1
J
p;+<)!t5timeK:6
p;+<)st5timeK:6
p;+<)en$5timeKp;+<)e"5time6
p;+<)turn5timeKp;+<)e"5timeTp;+<)!t5time6
N
if0+F:1
J
p;+<)!t5timeKp;+-1<)en$5time6
p;+<)st5timeKp;+-1<)en$5time6
p;+<)en$5timeKp;+<)st5timeTp;+<)e"5time6
p;+<)turn5timeKp;+<)e"5timeTp;+<)!t5time6
N
totale"e#timeTKp;+<)e"5time6
total!aittimeTKp;+<)!t5time6
totalturnaroun$timeTKp;+<)turn5time6
N
a/g!aittimeK0float1total!aittimeEn6
a/gturnaroun$timeK0float1totalturnaroun$timeEn6
printf02LnLnname burst start en$ !aittimeLn216
for0kK:6kDn6kTT1
printf02LnMsLtM$LtM$LtM$LtM$LtM$2,p;k<)pname,p;k<)e"5time,p;k<)st5time,p;k<)en$5
time,p;k<)!t5time16 87,1 A7M
printf02Lna/erage!aitingtimeMf2,a/g!aittime16
printf02Lna/erageturnaroun$timeMf2,a/gturnaroun$time16
N
O%B%t4
Enter the number of pro#esses ?
Enter pro#ess name p1
Enter pro#ess priority : !ith highest priority 2
Enter burst time 8
Enter pro#ess name p2
Enter pro#ess priority : !ith highest priority ?
Enter burst time @
Enter pro#ess name p8
Enter pro#ess priority : !ith highest priority 1
Enter burst time?
Enter pro#ess name p?
Enter pro#ess priority : !ith highest priority8
27
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
Enter burst time2
name burst start en$ !aittime
p8 ? : ? :
p1 8 ? 7 ?
p? 2 7 O 7
p2 @ O 15 O
a/erage !aiting time 5)::::::
a/erage turnaroun$time A)75::::
$$ 4'(=D 4'A<= !7:#D(;<=>
Cin#lu$eDst$io)hF
stru#t pro#ess
J
#har B&ame;1:<6
int e"5time,!t5time,st5time,en$5time,turn5time6
int rem5time,#omplete$5time6
Np;1:<,temp6
int ,uant5time6
main01
J
int n,i,+,k6
float a/gXaitimeK:):,a/gurn*roun$imeK:):6
float totalXaitimeK:):6
int totalE"e#imeK:,totalurn*roun$imeK:6
int fs,fe6
printf02LnEnter number of pro#ess216
s#anf02M$2,Un16
for0iK:6iDn6iTT1
J
printf02LnEnter pro#ess name216
s#anf02Ms2,p;i<)B&ame16
printf02Enter Yurst time 216
s#anf02M$2,Up;i<)e"5time16
p;i<)rem5timeKp;i<)e"5time6
p;i<)#omplete$5timeK:6
N
printf02LnEnter ,uantum time 216
s#anf02M$2,U,uant5time16
printf02Ln name start en$ busrt rem #ompl216
E= for first pro#ess start an$ the !ait time to be : =E
+K:6
fsKp;+<)st5timeK:6
p;+<)!t5timeK:6
if0p;+<)e"5timeF,uant5time1
J p;+<)en$5timeK,uant5time6
2A
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
p;+<)#omplete$5timeK,uant5time6
N
else
Jp;+<)en$5timeKp;+<)e"5time6
p;+<)#omplete$5timeKp;+<)e"5time6
p;+<)!t5timeKp;+<)en$5time-p;+<)rem5time6
p;+<)turn5timeKp;+<)!t5timeTp;+<)e"5time6
N
p;+<)rem5timeKp;+<)e"5time-p;+<)#omplete$5time6
printf02LnMsLtM$LtM$LtM$LtM$Lt
M$2,p;+<)B&ame,p;+<)st5time,p;+<)en$5time,p;+<)e"5time,p;+<)rem5time,p;+<)#omplete$5time16
feKp;+<)en$5time6
+TT6
!hile0+Dn1
J
p;+<)st5timeKfe6
if0p;+<)rem5timeF,uant5time1
J
p;+<)en$5timeKp;+<)st5timeT,uant5time6
p;+<)rem5time-K,uant5time6
p;+<)#omplete$5timeTK,uant5time6
feTK,uant5time6
printf02LnMsLtM$LtM$LtM$LtM$Lt
M$2,p;+<)B&ame,p;+<)st5time,p;+<)en$5time,p;+<)e"5time,p;+<)rem5time,p;+<)#omplete$5time16
N
else if0p;+<)rem5timeF:1
J
p;+<)en$5timeKp;+<)st5timeTp;+<)rem5time6
p;+<)!t5timeKfe-p;+<)#omplete$5time6
p;+<)#omplete$5timeTKp;+<)rem5time6
p;+<)turn5timeKp;+<)!t5timeTp;+<)e"5time6
feTKp;+<)rem5time6
p;+<)rem5timeK:6
printf02LnMsLtM$LtM$LtM$LtM$Lt
M$2,p;+<)B&ame,p;+<)st5time,p;+<)en$5time,p;+<)e"5time,p;+<)rem5time,p;+<)#omplete$5time16
N
+TT6
if0+KKn1
J
for0kK:6kDn6kTT1
J
if0p;k<)rem5timeF:1 E=break for the inner for loop =E
J +Kk6
break6 N
N
if0kFn1 E= breaks from the outer !hile loop =E
break6
2O
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
N
N
printf02Ln Bro#ess &ame Xaiting ime urn *roun$ ime216
for0iK:6iDn6iTT1
J printf02LnMsLtLtM$LtLtM$2,p;i<)B&ame,p;i<)!t5time,p;i<)turn5time16
totalXaitimeTKp;i<)!t5time6
totalurn*roun$imeTKp;i<)turn5time6
N
a/gXaitimeK0float1totalXaitimeEn6
a/gurn*roun$imeK0float1totalurn*roun$imeEn6
printf02Ln*/erage !aiting time Mf2,a/gXaitime16
printf02Ln*/erage turn aroun$ time Mf2,a/gurn*roun$ime16
N
'()P():
enter number of pro#ess?
enter pro#ess name p1
enter burst time 8
enter pro#ess namep2
enter burst time@
enter pro#ess namep8
enter burst time?
enter pro#ess namep?
enter burst time2
enter ,uantum time2
name start en$ burst rem #omp
p1 : 2 8 1 2
p2 2 ? @ ? 2
p8 ? @ ? 2 2
p? @ A 2 : 2
p1 A O 8 : 8
p2 O 11 @ 2 ?
p8 11 18 ? : ?
p2 18 15 @ : @
pro#essname !aitingtime turnaroun$time
p1 @ O
p2 O 15
p8 O 18
p? @ A
a/erage !aiting time7)5:::::
a/erage turn aroun$ time 11)25::::
4es"lt :
hus C program !as !ritten for simulating WCWS an$ SSW s#he$uling algorithms)
8:
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
.. <nter Process comm"nication ("sing s&ared memory, pipes or
message B"e"es
Aim
o !rite a C program to implement inter-pro#ess #ommuni#ation using pipes, share$ memory
an$ message ,ueues)
Description
he Linu" 'BC 0'nter-pro#ess #ommuni#ation1 fa#ilities pro/i$e a metho$ for multiple
pro#esses to #ommuni#ate !ith one another)
he types of inter pro#ess #ommuni#ation are4
1) Signals - Sent by other pro#esses or the kernel to a spe#ifi# pro#ess to in$i#ate /arious
#on$itions)
2) Bipes - %nname$ pipes set up by the shell normally !ith the 2Z2 #hara#ter to route
output from one program to the input of another)
8) W'WOS - &ame$ pipes operating on the basis of first $ata in, first $ata out)
?) Message ,ueues - Message ,ueues are a me#hanism set up to allo! one or more
pro#esses to !rite messages that #an be rea$ by one or more other pro#esses)
5) Semaphores - Counters that are use$ to #ontrol a##ess to share$ resour#es) hese
#ounters are use$ as a lo#king me#hanism to pre/ent more than one pro#ess from
using the resour#e at a time)
@) Share$ memory - he mapping of a memory area to be share$ by multiple pro#esses)
Message ,ueues, semaphores, an$ share$ memory #an be a##esse$ by the pro#esses if they
ha/e a##ess permission to the resour#e as set up by the ob+e#t-s #reator) he pro#ess must
pass an i$entifier to the kernel to be able to get the a##ess)
P<P#!
he Linu" 'BC 0'nter-pro#ess #ommuni#ation1 fa#ilities pro/i$e a metho$ for multiple
pro#esses to #ommuni#ate !ith one another) here are se/eral metho$s of 'BC a/ailable to
Linu")
Simply put, a pipe is a metho$ of #onne#ting the standard output of one pro#ess to the
standard input of another) Bipes are the el$est of the 'BC tools, ha/ing been aroun$ sin#e the
earliest in#arnations of the %&'( operating system) hey pro/i$e a metho$ of one-!ay
#ommuni#ations 0hen#e the term half-$uple"1 bet!een pro#esses)
o #reate a simple pipe !ith C, !e make use of the pipe01 system #all) 't takes a single
argument, !hi#h is an array of t!o integers, an$ if su##essful, the array !ill #ontain t!o ne!
file $es#riptors to be use$ for the pipeline) *fter #reating a pipe, the pro#ess typi#ally spa!ns
a ne! pro#ess 0remember the #hil$ inherits open file $es#riptors1)
BGOO>BE4 int pipe0 int f$;2< 16
81
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
GE%G&S4 : on su##ess
-1 on error
&OES4 f$;:< is set up for rea$ing, f$;1< is set up for !riting
!&ared Memory
Share$ memory allo!s one or more pro#esses to #ommuni#ate /ia memory that appears in all
of their /irtual a$$ress spa#es) he pages of the /irtual memory is referen#e$ by page table
entries in ea#h of the sharing pro#esses- page tables) 't $oes not ha/e to be at the same a$$ress
in all of the pro#esses- /irtual memory)
S>&OBS'S
Cin#lu$e DsysEip#)hF
Cin#lu$e DsysEshm)hF
shmget - allo#ates a share$ memory segment
int shmget0key5t key, si9e5t si9e, int shmflg16
shmget01 returns the i$entifier of the share$ memory segment asso#i-
ate$ !ith the /alue of the argument key)
/oi$ =shmat0int shmi$, #onst /oi$ =shma$$r, int shmflg16

he fun#tion shmat atta#hes the share$ memory segment i$entifie$ by shmi$ to the
a$$ress spa#e of the #alling pro#ess) he atta#hing a$$ress is spe#ifie$ by shma$$r !ith
one of the follo!ing #riteria4
int shm$t0#onst /oi$ =shma$$r16
he fun#tion shm$t $eta#hes the share$ memory segment lo#ate$ at the a$$ress
spe#ifie$ by shma$$r from the a$$ress spa#e of the #alling pro#ess)
Message C"e"es
Message ,ueues #an be best $es#ribe$ as an internal linke$ list !ithin the kernel-s a$$ressing
spa#e) Messages #an be sent to the ,ueue in or$er an$ retrie/e$ from the ,ueue in se/eral
$ifferent !ays) Ea#h message ,ueue 0of #ourse1 is uni,uely i$entifie$ by an 'BC i$entifier)
Message ,ueues allo! one or more pro#esses to !rite messages, !hi#h !ill be rea$ by one or
more rea$ing pro#esses) Linu" maintains a list of message ,ueues, the msg,ue /e#tor6 ea#h
element of !hi#h points to a ms,i$5$s $ata stru#ture that fully $es#ribes the message ,ueue)
Xhen message ,ueues are #reate$ a ne! ms,i$5$s $ata stru#ture is allo#ate$ from system
memory an$ inserte$ into the /e#tor)
&*ME
msgget - get a message ,ueue i$entifier
S>&OBS'S
Cin#lu$e DsysEtypes)hF
Cin#lu$e DsysEip#)hF
Cin#lu$e DsysEmsg)hF
82
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
int msgget0key5t key, int msgflg16
7ESCG'B'O&
he fun#tion returns the message ,ueue i$entifier asso#iate$ !ith the /alue of the key
argument)
MS3S&7 U MS3GCH
o sen$ or re#ei/e a message, the #alling pro#ess allo#ates a stru#ture of the follo!ing
general form4
stru#t msgbuf J
long mtype6 E= message type, must be F : =E
#har mte"t;1<6 E= message $ata =E
N6
Cin#lu$e DsysEtypes)hF
Cin#lu$e DsysEip#)hF
Cin#lu$e DsysEmsg)hF
int msgsn$0int ms,i$, stru#t msgbuf =msgp, si9e5t msgs9, int msgflg16
he msgsn$ system #all appen$s a #opy of the message pointe$ to by msgp to the
message ,ueue !hose i$entifier is spe#ifie$ by ms,i$)
ssi9e5t msgr#/0int ms,i$, stru#t msgbuf =msgp, si9e5t msgs9, long msg-
typ, int msgflg16
he system #all msgr#/ rea$s a message from the message ,ueue spe#ifie$ by ms,i$
into the msgbuf pointe$ to by the msgp argument, remo/ing the rea$ message from the
,ueue)
#xercise Programs :
$% 1. <P7 (!<=> P<P#! %$
Cin#lu$eDst$io)hF
main01
J
int f$;2<,#hil$6
#har a;1:<6
printf02LnEnter the string to enter into the pipe 216
s#anf02Ms2,a16
pipe0f$16
88
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
#hil$Kfork016
if0V#hil$1
J
#lose0f$;:<16
!rite0f$;1<,a,516
!ait0:16
N
else
J
#lose0f$;1<16
rea$0f$;:<,a,516
printf02Lnhe string re#ei/e$ from the pipe is MsLn2,a16
N
return :6
N
'()P():
enter the string to enter into the pipe hello
the string re#ei/e$ from the pipe is hello
$% *. <P7 (!<=> !:A4#D M#M#'4@ %$
Cin#lu$eDst$io)hF
Cin#lu$eDsysEshm)hF
Cin#lu$eDsysEip#)hF
main01
J
int #hil$, shmi$,i6
#har =shmptr6
#hil$Kfork016
if0V#hil$1
J
shmi$Kshmget02:?1,82,:@@@Z'BC5CGE*16
shmptrKshmat0shmi$,:,:16
printf02LnBarent !riting)))))))))Ln216
for0iK:6iD1:6iTT1
J
shmptr;i<K-a-Ti6
put#har0shmptr;i<16
N
printf02LnMs2,shmptr16
!ait0&%LL16
N
else
J
shmi$Kshmget02:?1,82,:@@@Z'BC5CGE*16
shmptrKshmat0shmi$,:,:16
8?
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
printf02LnGea$ing from #hil$)))))))))Ln216
for0iK:6iD1:6iTT1
put#har0shmptr;i<16
shm$t0&%LL16
shm#tl0shmi$,'BC5GM'7,&%LL16
N
return :6
N
'()P():
parent !riting)))
rea$ing from #hil$)))
ab#$efghi+ab#$efghi+
ab#$efghi+
$% +. a. <P7 (!<=> M#!!A># C(#(#!
6 !#=D M#!!A># %$
Cin#lu$e DsysEtypes)hF
Cin#lu$e DsysEip#)hF
Cin#lu$e DsysEmsg)hF
Cin#lu$e Dst$io)hF
Cin#lu$e Dstring)hF
C$efine MS3S\ 12A
E= 7e#lare the message stru#ture) =E
type$ef stru#t msgbuf J
long mtype6
#har mte"t;MS3S\<6
N message5buf6
main01
J
int ms,i$6
int msgflg K 'BC5CGE* Z :@@@6
key5t key6
message5buf sbuf6
si9e5t buf5length6
E= 3et the message ,ueue i$ for the 2name2 128?, !hi#h !as #reate$ by
= the ser/er) =E
key K 128?6
ms,i$ K msgget0key, msgflg 16
E= Xe-ll sen$ message type 1 =E

sbuf)mtype K 16
0/oi$1 str#py0sbuf)mte"t, 27i$ you get this]216
buf5length K strlen0sbuf)mte"t1 6
85
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory

E= Sen$ a message) =E
msgsn$0ms,i$, Usbuf, buf5length, 'BC5&OX*'16
printf02Message4 L2MsL2 SentLn2, sbuf)mte"t16
N
'()P():
message42$i$ you get there]2 sent
$%+ .?. <P7 (!<=> M#!!A># C(#(#!
6 4#7#<D# M#!!A># %$
Cin#lu$e DsysEtypes)hF
Cin#lu$e DsysEip#)hF
Cin#lu$e DsysEmsg)hF
Cin#lu$e Dst$io)hF
C$efine MS3S\ 12A
E= 7e#lare the message stru#ture) =E
type$ef stru#t msgbuf J
long mtype6
#har mte"t;MS3S\<6
N message5buf6
main01
J
int ms,i$6
key5t key6
message5buf rbuf6
E= 3et the message ,ueue i$ for the 2name2 128?, !hi#h !as #reate$ by the ser/er) =E
key K 128?6
ms,i$ K msgget0key, :@@@16
E= Ge#ei/e an ans!er of message type 1 =E
msgr#/0ms,i$, Urbuf, MS3S\, 1, :16

E=Brint the ans!er) =E
printf02MsLn2, rbuf)mte"t16
e"it0:16
N
'()P():
message42$i$ you get there]2 re#ei/e$
4es"lt :
8@
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
hus C program !as program !ritten to implement 'BC using pipes, share$ memory an$
message ,ueues)
/. Prod"cer 5 7ons"mer pro?lem "sing semap&ores
Aim :
o !rite a C program to implement pro$u#er #onsumer problem using semaphore)
!emap&ores
Semaphore is a location in memory whose value can be tested and set by more than one
process. The test and set operation is, so far as each process is concerned, uninterruptible or
atomic; once started nothing can stop it. The result of the test and set operation is the addition
of the current value of the semaphore and the set value, which can be positive or negative.
Depending on the result of the test and set operation one process may have to sleep until the
semphore's value is changed by another process. Semaphores can be used to implement
critical regions, areas of critical code that only one process at a time should be executing.
Say you had many cooperating processes reading records from and writing records to a single
data file. You would want that file access to be strictly coordinated. You could use a
semaphore with an initial value of 1 and, around the file operating code, put two semaphore
operations, the first to test and decrement the semaphore's value and the second to test and
increment it. The first process to access the file would try to decrement the semaphore's value
and it would succeed, the semaphore's value now being 0. This process can now go ahead and
use the data file but if another process wishing to use it now tries to decrement the
semaphore's value it would fail as the result would be -1. That process will be suspended until
the first process has finished with the data file. When the first process has finished with the
data file it will increment the semaphore's value, making it 1 again. Now the waiting process
can be woken and this time its attempt to increment the semaphore will succeed.
* semaphore is represente$ by an anonymous stru#ture in#lu$ing the follo!ing members4
unsigne$ short sem/al6 E= semaphore /alue =E
unsigne$ short sem9#nt6 E= C !aiting for 9ero =E
unsigne$ short semn#nt6 E= C !aiting for in#rease =E
pi$5t sempi$6 E= pro#ess that $i$ last op =E
he fun#tion semop performs operations on sele#te$ members of the semaphore set
in$i#ate$ by semi$) Ea#h of the nsops elements in the array pointe$ to by sops spe#ifies an
operation to be performe$ on a semaphore by a stru#t sembuf in#lu$ing the follo!ing
members4
unsigne$ short sem5num6 E= semaphore number =E
short sem5op6 E= semaphore operation =E
short sem5flg6 E= operation flags =E
Cin#lu$e DsysEtypes)hF
Cin#lu$e DsysEip#)hF
Cin#lu$e DsysEsem)hF
int semop0int semi$, stru#t sembuf =sops, unsigne$ nsops16
SEM3E4
87
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
semget - get a semaphore set i$entifier
S>&OBS'S
Cin#lu$e DsysEtypes)hF
Cin#lu$e DsysEip#)hF
Cin#lu$e DsysEsem)hF
int semget0key5t key, int nsems, int semflg16
7ESCG'B'O&
his fun#tion returns the semaphore set i$entifier asso#iate$ !ith the
argument key)
sem#tl - semaphore #ontrol operations
S>&OBS'S
Cin#lu$e DsysEtypes)hF
Cin#lu$e DsysEip#)hF
Cin#lu$e DsysEsem)hF
int sem#tl0int semi$, int semnum, int #m$, )))16
7ESCG'B'O&
he fun#tion sem#tl performs the #ontrol operation spe#ifie$ by #m$ on the semaphore
set i$entifie$ by semi$, or on the semnum-th semaphore of that set) 0Semaphores are
numbere$ starting at :)1
$% program to demonstrate a ?asic prod"cer6cons"mer implementation. %$
Cin#lu$e Dst$io)hF E= stan$ar$ 'EO routines) =E
Cin#lu$e Dst$lib)hF E= ran$01 an$ sran$01 fun#tions =E
Cin#lu$e Dunist$)hF E= fork01, et#) =E
Cin#lu$e Dtime)hF E= nanosleep01, et#) =E
Cin#lu$e DsysEtypes)hF E= /arious type $efinitions) =E
Cin#lu$e DsysEip#)hF E= general SysH 'BC stru#tures =E
Cin#lu$e DsysEsem)hF E= semaphore fun#tions an$ stru#ts) =E
C$efine &%M5LOOBS 1: E= number of loops to perform) =E
int main0int arg#, #har= arg/;<1
J
int sem5set5i$6 E= '7 of the semaphore set) =E
int #hil$5pi$6 E= B'7 of our #hil$ pro#ess) =E
int i6 E= #ounter for loop operation) =E
stru#t sembuf sem5op6 E= stru#ture for semaphore ops) =E
int r#6 E= return /alue of system #alls) =E
stru#t timespe# $elay6 E= use$ for !asting time) =E
E= #reate a pri/ate semaphore set !ith one semaphore in it, =E
8A
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
E= !ith a##ess only to the o!ner) =E
sem5set5i$ K semget0'BC5BG'H*E, 1, :@::16
printf02semaphore set #reate$, semaphore set i$ -M$-)Ln2, sem5set5i$16
r# K sem#tl0sem5set5i$, :, SEH*L, :16
E= fork-off a #hil$ pro#ess, an$ start a pro$u#erE#onsumer +ob) =E
#hil$5pi$ K fork016
s!it#h 0#hil$5pi$1 J
#ase :4 E= #hil$ pro#ess here =E
for 0iK:6 iD&%M5LOOBS6 iTT1 J
E= blo#k on the semaphore, unless it-s /alue is non-negati/e) =E
sem5op)sem5num K :6
sem5op)sem5op K -16
sem5op)sem5flg K :6
semop0sem5set5i$, Usem5op, 116
printf02#onsumer4 -M$-Ln2, i16
fflush0st$out16
N
break6
$efault4 E= parent pro#ess here =E
for 0iK:6 iD&%M5LOOBS6 iTT1 J
printf02pro$u#er4 -M$-Ln2, i16
fflush0st$out16
E= in#rease the /alue of the semaphore by 1) =E
sem5op)sem5num K :6
sem5op)sem5op K 16
sem5op)sem5flg K :6
semop0sem5set5i$, Usem5op, 116
E= pause e"e#ution for a little bit, to allo! the =E
E= #hil$ pro#ess to run an$ han$le some re,uests) =E
E= this is $one about 25M of the time) =E
if 0ran$01 F 8=0G*&75M*(E?11 J
$elay)t/5se# K :6
$elay)t/5nse# K 1:6
nanosleep0U$elay, &%LL16
N
N
break6
N
return :6
N
'()P():
Semaphore set #reate$ ,semaphore set i$ -25AA751-
Bro$u#er4:
Consumer4:
Bro$u#er41
Bro$u#er42
Consumer41
Consumer42
8O
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
Bro$u#er48
Consumer48
Bro$u#er4?
Consumer4?
Bro$u#er45
Bro$u#er4@
Bro$u#er47
Consumer45
Consumer4@
Consumer47
Bro$u#er4A
Bro$u#er4O
Consumer4A
Consumer4O
4es"lt :
?:
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
hus C program !as !ritten to implement pro$u#er #onsumer problem using
semaphores)
1. Memory Management !c&eme 5 <
( :!%*t :!t (n5 Be*t :!t Memo%; Allo.(t!on St%(te"!e* -
A!m 2
o implement memory allo#ation algorithms first fit an$ best fit)
Description :
'n an en/ironment that supports $ynami# memory allo#ation, the memory manager must keep
a re#or$ of the usage of ea#h allo#atable blo#k of memory) his re#or$ #oul$ be kept by using
almost any $ata stru#ture that implements linke$ lists) *n ob/ious implementation is to
$efine a free list of blo#k $es#riptors, !ith ea#h $es#riptor #ontaining a pointer to the ne"t
$es#riptor, a pointer to the blo#k, an$ the length of the blo#k) he memory manager keeps a
free list pointer an$ inserts entries into the list in some or$er #on$u#i/e to its allo#ation
strategy) * number of strategies are use$ to allo#ate spa#e to the pro#esses that are #ompeting
for memory)
Aest Fit
he allo#ator pla#es a pro#ess in the smallest blo#k of unallo#ate$ memory in !hi#h it !ill
fit)
Broblems4
't re,uires an e"pensi/e sear#h of the entire free list to fin$ the best
hole)
More importantly, it lea$s to the #reation of lots of little holes that are
not big enough to satisfy any re,uests) his situation is #alle$ fragmentation, an$ is a
problem for all memory-management strategies, although it is parti#ularly ba$ for
best-fit)
Solution4 One !ay to a/oi$ making little holes is to gi/e the #lient a bigger blo#k than it
aske$ for) Wor e"ample, !e might roun$ all re,uests up to the ne"t larger multiple of @?
bytes) hat $oesn-t make the fragmentation go a!ay, it +ust hi$es it)
%nusable spa#e in the form of holes is #alle$ e"ternal fragmentation
%nusable spa#e in the form of holes is #alle$ e"ternal fragmentation
Eorst Fit
?1
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
he memory manager pla#es pro#ess in the largest blo#k of unallo#ate$ memory a/ailable)
he i$ea is that this pla#ement !ill #reate the largest hole after the allo#ations, thus
in#reasing the possibility that, #ompare$ to best fit, another pro#ess #an use the hole #reate$
as a result of e"ternal fragmentation)
First Fit
*nother strategy is first fit, !hi#h simply s#ans the free list until a large enough hole is
foun$) 7espite the name, first-fit is generally better than best-fit be#ause it lea$s to less
fragmentation)
Broblems4
Small holes ten$ to a##umulate near the beginning of the free list,
making the memory allo#ator sear#h farther an$ farther ea#h time)
Solution4 &e"t Wit
=ext Fit
he first fit approa#h ten$s to fragment the blo#ks near the beginning of the list !ithout
#onsi$ering blo#ks further $o!n the list) &e"t fit is a /ariant of the first-fit strategy) he
problem of small holes a##umulating is sol/e$ !ith ne"t fit algorithm, !hi#h starts ea#h
sear#h !here the last one left off, !rapping aroun$ to the beginning !hen the en$ of the list
is rea#he$ 0a form of one-!ay ele/ator1
4es"lt:
hus C program !as !ritten to implement first fit an$ best fit allo#ation strategies)
1) Memory Management !c&eme 5 <<
( Paging and Page replacement algorit&m 5 F<F')
*im 4
o !rite a C program for implementing paging s#heme an$ W'WO page repla#ement
algorithm
Paging
Yasi# i$ea4 allo#ate physi#al memory to pro#esses in fi"e$ si9e #hunks #alle$ page
frames) Bresent abstra#tion to appli#ation of a single linear a$$ress spa#e) 'nsi$e ma#hine,
break a$$ress spa#e of appli#ation up into fi"e$ si9e #hunks #alle$ pages) Bages an$ page
frames are same si9e) Store pages in page frames) Xhen pro#ess generates an a$$ress,
$ynami#ally translate to the physi#al page frame !hi#h hol$s $ata for that page)
?2
Lab Manual Meenakshi College of Engineering, Chennai
CS 2257 - Operating Systems Laboratory
So, a /irtual a$$ress no! #onsists of t!o pie#es4 a page number an$ an offset !ithin
that page) Bage si9es are typi#ally po!ers of 26 this simplifies e"tra#tion of page numbers
an$ offsets) o a##ess a pie#e of $ata at a gi/en a$$ress, system automati#ally $oes the
follo!ing4
o E"tra#ts page number)
o E"tra#ts offset)
o ranslate page number to physi#al page frame i$)
o *##esses $ata at offset in physi#al page frame)
First-In, First-Out (FIFO)
First-in, first-out is as easy to implement as Random Replacement, and
although its performance is equally unreliable or worse, claims , its
behaior does follow a predictable pattern! Rather than choosing a ictim
page at random, the oldest page (or first-in) is the first to be remoed!
"onceptually compares FIFO to a limited si#e queue, with items being
added to the queue at the tail! $hen the queue fills (all of the physical
memory has been allocated), the first page to enter is pushed out of head of
the queue! %imilar to Random Replacement, FIFO blatantly ignores trends,
and although it produces less page faults, still does not ta&e adantage of
locality trends unless by coincidence as pages moe along the queue !
?8
Lab Manual Meenakshi College of Engineering, Chennai

Das könnte Ihnen auch gefallen