UI Lab Manual
UI Lab Manual
ADI
TYAI
NSTI
TUTEOFTECHNOLOGYANDMANAGEMENT
(
AnAut
onomousI
nst
it
uti
on)
TEKKALI
Depar
tmentofcomput
ersci
enceandengi
neer
ing
LABMANUAL
Uni
xInt
ernal
sLab
Pr
epar
edby
:
Sr
i.Ch.Chandr
aSekhar
,M.
Tech(
CSE)
Assi
stantPr
ofessor
,Dept
.of
.IT,
AITAM
AR16
I
NDEX
S.
NO CONTENTS PAGENO.
1 SchemeofCour
seWor
k 1
2 I
ntr
oduct
ionAboutLab 4
3 Gui
del
i
nest
otheSt
udent
s 5
4 Li
stofExper
iment
s 6
5 Sol
uti
onsf
orUni
xInt
ernal
sLab 7
7 Ref
erences 41
AR16
SchemeofCour
seWor
k
Cour
seDet
ail
s:
Cour
seTi
tl
e: Uni
xInt
ernal
sLab
Cour
seCode: 13I
T3101 L T P C 0 0 3 1.
5
Pr
ogr
am: I
IIB.
Tech
Speci
ali
zat
ion: I
nfor
mat
ionTechnol
ogy
Semest
er:I
Feat
uresofOper
ati
ngSystem,UNIXut
il
it
ies,
Comput
erNet
wor
king,
Pr
erequi
sit
es:
Fi
lesyst
em andst
orage,
CPr ogr
amming.
Cour
sestowhi
chi t
Uni
xPr
ogr
ammi
ng,
Shel
lPr
ogr
ammi
ng,
Net
wor
kPr
ogr
ammi
ng
isaPrer
equi
sit
e?
Cour
seObj
ect
ivi
ti
es:
1 Under
standt
hebasi
cUni
xcommands.
2 Under
standt
hebasi
cshel
lpr
ogr
ammi
ngconcept
s.
3 Under
standt
hel
i
brar
yfunct
ionsandsy
stem cal
l
sinUni
x.
Descr
ibet
heUni
xpr
ocessst
ruct
ureandv
ari
oust
ypesofsi
gnal
sforhandl
i
ngof
4
pr
ocess.
Discusst
heworki
ngmechani
sm ofI
PCusi
ngpi
pes,
semaphor
esmessagequeues
5
andsharedmemory.
Cour
seOut
comes:
1 Runv
ari
ousUni
xcommandsonast
andar
dUni
xOper
ati
ngsy
stem
2 Dev
elopshel
lscr
ipt
stoaut
omat
ecommont
asks.
3 Exper
imentwi
thv
ari
ousl
i
brar
yfunct
ionsandsy
stem cal
l
sinUni
xoper
ati
ngsy
stem.
4 Wr
it
ePr
ogr
amsr
elat
edt
opr
ocesscont
rol
andsi
gnal
handl
i
ng.
5 I
mpl
ementI
PCusi
ngpi
pes,
semaphor
es,
messagequeuesandshar
edmemor
y.
3
AR16
Pr
ogr
am Out
comes(
POs)
:
Att
heendoft
hepr
ogr
amme,
thest
udent
sinUni
xInt
ernal
sLabwi
l
lbeabl
eto:
Appl
ytheknowledgeofmat hemati
cs,sci
ence,engineer
ingfundamental
s,andan
PO1
engi
neer
ingspeci
ali
zat
iont
ot hesol
uti
onofcompl exengineer
ingpr
oblems.
I
denti
fy,f
ormulate,review researchli
teratur
e,andanalyzecompl
exengineer
ing
PO2 pr
oblems r eaching subst antiat
ed concl usi
ons using fi
rst pr
inci
ples of
mathemati
cs,naturalsciences,andengineeri
ngsci
ences.
Design solut
ions for compl ex engineer
ing problems and design sy st
em
components orpr ocesses thatmeett he specif
ied needs wit
h appropri
ate
PO3
consider
ati
on forthepubl i
chealth and safet
y,and thecultur
al,soci
etal
,and
envi
ronmentalconsi
derati
ons.
Applyr easoningi
nformedbyt hecontext
ualknowledgetoassesssocietal
,heal
th,
PO6 safety,legalandcul
turali
ssuesandtheconsequentresponsi
bil
i
tiesr
elev
anttothe
professionalengi
neeri
ngpracti
ce.
Underst
andt heimpactofthepr
ofessi
onalengi
neeri
ngsolut
ionsi
nsociet
aland
PO7 envi
ronmentalcontext
s,and demonstrat
et he knowl
edge of,and need f
or
sust
ainabl
edev el
opment.
Appl
yet
hicalpr
inci
plesandcommi tt
opr
ofessi
onalet
hicsandr
esponsi
bil
i
tiesand
PO8
nor
msoftheengineeri
ngpract
ice.
Funct
ionef
fecti
vel
yasanindivi
dual
,andasamemberorl
eaderi
ndi
ver
set
eams,
PO9
andinmult
idi
sci
pli
nar
yset
tings.
Communicateeffecti
vel
yoncompl exengineeringacti
vi
ti
eswiththeengineeri
ng
communit
yandwi t
hsocietyatlar
ge,suchas, beingabl
etocomprehendandwr i
te
PO10
eff
ect
ivereport
sanddesi gndocument at
ion,makeef fect
ivepr
esentat
ions,and
gi
veandreceiv
eclearinst
ructi
ons.
Demonstrateknowledgeandunderstandi
ngoftheengi
neer
ingandmanagement
PO11 pr
inci
plesandapplytheset
oone’sownwor k,asamemberandleaderi
nat
eam,to
managepr oj
ect
sandi nmult
idi
sci
pli
naryenvi
ronment
s.
Recogni
ze the need for
,and havet he pr
eparati
on and abil
it
yt o engage in
PO12
i
ndependentandli
fe-
longlear
ningi
nthebroadestcontextoft
echnologi
calchange.
4
AR16
Pr
ogr
am Speci
fi
cOut
comes(
PSOs)
:
Byt
hecompl
eti
onofI
nfor
mat
ionTechnol
ogypr
ogr
am t
hest
udentwi
l
lbeabl
eto:
Execut
ethemathemat icalf
oundati
ons,al
gori
thmi
cpr
inci
plesofengi
neer
ing
PSO1 toidenti
fy,f
ormulate,design and i
nvest
igatet
he i
ntr
icat
e probl
ems of
I
nformati
onTechnology.
Compr ehendt
heprinci
plesandappl
i
cat
ionsoft
hesof
twar
eandhar
dwar
e
PSO2
facet
sofcomputersyst
ems.
Appl
yknowledgei
nvar
iousdomai
nst
oident
if
yresear
chgapsandpr
ovi
de
PSO3
sol
uti
ons.
Cour
seAr
ti
cul
ati
onMat
ri
x:
CO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1PSO2 PSO3
CO1 3 2 3 3 3 - 2 1 2 3 2 1 3 2 3
CO2 3 2 3 3 3 - 2 1 2 3 2 1 2 3 2
CO3 3 2 3 3 3 - 2 1 2 3 2 1 2 2 2
CO4 3 2 3 3 3 - 2 1 2 3 2 1 3 2 3
CO5 3 2 3 3 3 - 2 1 2 2 2 1 2 3 2
Not
e:Entercorr
elat
ionlevels1:Sl
ight(
Low)2:
Moder
ate(
Medi
um)3:
Subst
ant
ial
(Hi
gh)
I
fthereisnocorrelat
ion,put“-
”
AssessmentMet
hods: Obser
vat
ion/
Recor
d/I
nter
nal
Exam /EndExam
I
nter
nalExam:
25Marks-DaytoDaywor
k( 10)+Recor
d(5)+Test(10)
Ext
ernal
Exam:50Mar
ks-Descri
pti
on&Program (
20)+Output(
20)+Viva-
Voi
ce(
10)
5
AR16
I
ntr
oduct
ionAboutLab
Ther
ear e33sy stemsi nstall
edint hisLab.Thisl abisv eryusefulforstudent s
becausetheyarefamiliarwithdiffer
entOper ati
ngSyst
emssot hattheycanexecutet heir
programsindi f
ferentpr ogr
ammi ngenv i
ronments.Eachst udenthasasepar at
el ogin
becausestudentscansav etheirwor kintheirownaccounts.Eachst udentworkissaf e
andsecur efrom otherst udents.Sy st
emsar eprovi
dedf orstudentsint he1:1r atio.
Systemsareassignednumber sandsamesy stem i
sall
ottedforstudent
swhent heydot he
l
ab.Thehar dwar
eandsof twareconfigur
ationsareasfol
lows:
Sy
stem:
Component Det
ails
Manuf act
urer&Model
: Hewlett
-Packar
d& HPCompaqPr
o4300SFFPC
Processor
: I
ntel
(R)Cor
e(TM)i
5-2500CPU@ 3.
30GHz–4Cor
es
Memor y(
RAM) : 4.
00GBDDR3
HardDi sk :
500GBSEAGATE
Mouse
: HPOpt
ical
Mouse
Key
boar
d
: HPPS/
2
Moni
tor
: CompaqLED18.
5
Net
wor
kInt
erf
aceCar
d: 10/100/
1000Mbps-Br
oadcom Net
Link(
TM)Gi
gabi
t
Ether
net
Sof
twar
e’s:
Syst
em Soft
war e:CentOS7LinuxOSandWi ndows7Pr
ofessi
onal
(64-
bitOper
ati
ngSy
stem)
MSOf f
ice2007
.
NETFr amework4. 7.
2
Anti
vi
rus:SymantecEndpointProt
ect
ion11.
0
AdobeFamily(
AdobeReader11)
MacromediaFlash8
SPSS
OpenSour
ceSof
twar
e’s:
JDK8
Put
tySui
te(
Connect
ingwi
ndowst
oLi
nux
)
Ecl
i
pse
Andr
oidSDK
Jav
a™2,
Micr
oEdi
ti J2ME™)pl
on( atf
orm
ApacheNet
BeansI
DE
6
AR16
Notepad++
DevC++5. 1
CodeBl ocks16
St
arUML5. 0
Python2.7.9,3.6andIDLE
WinRAR
Pl
ay er
s: WindowsMedi aPl
ayer
11,
VLCPl
ayer
Browsers:Chr ome,Fi
ref
ox
7
AR16
Gui
del
i
nest
otheSt
udent
s
1.At
tendancei
nthel
abi
smandat
ory
2.Useofanysor
tofdi
git
algadgeti
sst
ri
ctl
ypr
ohi
bit
eddur
ingl
absessi
ons.
3.St
udent
sshoul
dcomewi
tht
hor
oughpr
epar
ati
onf
ort
heexper
imentt
obeconduct
ed.
4.St
udent
swi
l
lnotbeper
mit
tedt
oat
tendt
hel
abor
ator
yunl
esst
heybr
ingt
hepr
act
ical
r
ecor
dful
l
ycompl
etedi
nal
lrespect
sper
tai
ningt
otheexper
imentconduct
edi
nthe
pr
evi
ouscl
ass.
5.Pr
act
ical
recor
dshoul
dbeneat
lymai
ntai
ned.
6.Theyshoul
dobt
aint
hesi
gnat
ureoft
hest
aff
-i
n-char
gei
ntheobser
vat
ionbookaf
ter
compl
eti
ngeachexper
iment
.
7.Theor
yregar
dingeachexper
imentshoul
dbewr
it
teni
nthepr
act
icalr
ecor
dbef
ore
pr
ocedur
einy
ourownwor
ds.
8.Askl
abt
echni
ci
anf
orassi
stancei
fyouhav
eanypr
obl
em.
9.Sav
eyourcl
asswor
k,assi
gnment
sinsy
stem.
10.
Do notdownl
oad ori
nst
allsof
twar
e wi
thoutt
he assi
stance oft
he l
abor
ator
y
t
echni
cian.
11.
Donotal
tert
heconf
igur
ati
onoft
hesy
stem.
12.
Tur
nof
fthesy
stemsaf
teruse.
8
AR16
Uni
xInt
ernal
sLab
Li
stofExper
iment
s
Page.
S.
No. Nameoft
heExper
iment
No.
Uset hecutcommandont heout putofal ongdi r
ect orylist i
ngi nor dert o
1.
di
spl ayonl yt hef il
eper missions.Thenpi pet hisout putt osor tanduni qto 9
fi
lt
eroutanydoubl el ines.Thenuset hewct ocountt hedi fferentper mi ssion
ty
pesi nt hisdi rect ory.
Uset hecatcommandt ocr eat eaf ilecont ainingthef ol
lowi ngdat a.Cal lit
2. my tabl e.Sor tt hemy tabl e.Swapf ields2and3ofmy table.Cal li
tmy t able. 11
Printt henewf ilemy tabl e.
a)Wr i
t eashel lscr i
ptt ocheckwhet hert heuseri sel
igi bl
ef orv oteornotby
3.
usingdat eandawkcommands 13
b)Wr iteashel lpr ogram whi chpr intswhet hert hesy stem i sheav ilyloaded,
medi al l
yl oadedorl owl yl oadedbyusi ngwhocommand
a)Wr iteashel lpr ogr am whi chi nfor msassoonasaspeci fi
eduserwhose
namei sgi venal ongt hecommandl inei sloggedi ntot hesy st em.
4. b)Wr i
t eapr ogr am whi chr eadsaf ilenameand3di r
ect orynamesal ongt he 16
commandl ineandpr int“ YES”i fthef i
leisf oundi nanyoft hedi rector i
es
otherwi sepr int ”notf ound” .
5.
a)Wr iteasy st em pr ogr am tor emov eaf i
leusi ngremov esy stem cal l 17
b)Wr i
t easy st em pr ogr am tor enameaf i
leusi ngrenamesy st em cal l
a)Wr iteasy st em pr ogr am tol inkaf ileusingl inksystem cal l 18
6.
b)Wr i
t easy st em pr ogr am toopenaf ileandunl i
nkit
7.
a)Wr iteasy st em pr ogr am thatcr eat esachi ldusi ngfor k()sy st em call 19
b)Wr i
t easy st em pr ogr am thatcr eat esaOr phan.
a)Wr iteasy st em pr ogr am thatcr eat esaZombi e
8. b)Wr iteasy st em pr ogr am t hatwai tsf oritschi ldtot ermi nat eusi ngwai t() 21
system
9.
Wr i
teaUNI Xsy st em pr ogram t hatdemonst r
at essystem cal ls 23
(i
)kill( i
i)raise( iii
)alar m( iv)pause( v)abor t( vi
)sl
eep
10. Wr i
teaCpr ogr am t osi mulatepr oducerandconsumerpr obl em usi ngpi pes. 28
11.
Wr i
te UNI X sy stem pr ogram or pr ograms t o demonst ratet he I PC 30
mechani sm semaphor es-ori
ent edsy st em calls.
12.
Wr i
te UNI X sy stem pr ogram or pr ograms t o demonst ratet he I PC 32
mechani sm Messagequeues- orientedsy stem cal l
s.
13.
Wr i
te UNI X sy stem pr ogram or pr ograms t o demonst ratet he I PC 34
mechani sm shar edmemor y-or i
ent edsy stem cal l
s.
Additi
onalExperiments
1. Writeashellscri
pt(Smal lCalcul
ator
)t hatadds,subtracts,mul
ti
pli
esand
di
videsthegi
ventwoi nt
egers.
36
WriteaCProgram t
hatill
ustr
atesthefoll
owing
2. a)Two-waycommuni cati
on(Simplex)withsinglepi
pe.
b)Two-waycommuni cati
on( Dupl
ex)withuni-di
rect
ionalpi
pes.
37
c)Two-waycommuni cati
on(Simplex)withbi-di
rect
ional
pipes.
Openendedpr ograms
1. Wr
it
eashel
lscri
pttor
ever
set herowsandcol
umnsofamat
ri
x. 39
2. Wr
it
ea C program t
hatil
lustr
atestheusesofthemkdir,opendi
r 40
9
AR16
,r
eaddi
r,cl
osedi
r,andrmdirAPIs.
3. Writ
eaCProgram thati
l
lustr
atesf
il
elocki
ngusi
ngsemaphor
es 43
10
AR16
Uni
xInt
ernal
sLab
1.Aim:Uset hecutcommandont heoutputofal ongdir
ector
ylist
inginordertodi
splay
onlythefi
lepermi
ssi
ons.Thenpipet
hisoutputtosortanduniqt ofi
lt
eroutanydouble
l
ines.Thenusethewctocountt
hediff
erentpermissi
ontypesinthisdi
rect
ory.
Pr
ogr
am:
$ls-
l
drwxr-
xr-
x.2r ootroot 87Jul 2812: 42demo
drwxr-
xr-
x.2r ootroot 6Jul 2313: 56Desktop
drwxr-
xr-
x.3r ootroot 20Aug615: 59Document s
drwxr-
xr-
x.10r ootroot 4096Aug615: 43Downloads
-r
w-r-
-r
--
.1r ootroot 101Jul 3015: 31f 1
-r
w-r-
-r
--
.1r ootroot 28Jul 2810: 30f 2
-r
w-r-
-r
--
.1r ootroot 20Jul 3015: 24f il
e1
-r
w-r-
-r
--
.1r ootroot 22Jul 3015: 24f il
e2
-r
w-r-
-r
--
.1r ootroot 14Jul 3015: 24f il
e3
-r
w-r-
-r
--
.1r ootroot 12Jul 3015: 25f il
e4
-r
w-r-
-r
--
.1r ootroot 72Jul 2811: 26f il
e5
drwxr-
xr-
x.2r ootroot 6Jul 2313: 56Music
$ls-l
|cut-d"
"-f
1
total
-rw-r
--
r--
.
-rw--
--
---
.
-rw-r
--
r--
.
-rw-r
--
r--
.
-rw-r
--
r--
.
-rwxr-
-r-
-.
-rw-r
--
r--
.
-rw-r
--
r--
.
drwxr-xr-
x.
drwxr-xr-
x.
drwxr-xr-
x.
drwxr-xr-
x.
$ls-
l|
cut-d"
"-f
1|sor
t
drwxr-
xr-
x.
drwxr-
xr-
x.
drwxr-
xr-
x.
drwxr-
xr-
x.
drwxr-
xr-
x.
drwxr-
xr-
x.
drwxr-
xr-
x.
drwxr-
xr-
x.
drwxr-
xr-
x.
drwxr-
xr-
x.
-r
w---
--
--
.
-r
w-r-
-r
--
.
11
AR16
-
rw-
r-
-r
--
.
-
rw-
r-
-r
--
.
-
rw-
r-
-r
--
.
-
rw-
r-
-r
--
.
-
rw-
r-
-r
--
.
-
rw-
r-
-r
--
.
$l
s-l
|
cut- d"
"-f1|sor
t|
uni
q
drwxr-xr-
x.
-rw--
--
---
.
-rw-r
--
r--
.
-rwxr-
-r-
-.
total
$l
s-l
|
cut-d""-f
1|sor
t|
uni
q|head-
4
drwxr-
xr-
x.
-r
w---
--
--
.
-r
w-r-
-r
--
.
-r
wxr—r--
.
$l
s-l
|
cut-d"
"-f
1|sor
t|
uni
q|head-
4|wc-
l
4
12
AR16
2. Ai
m:Us ethecatcommandtocr
eat
eafi
l
econt
aini
ngthefol
l
owi
ngdata.Cal
litmy
tabl
e.
Sortthemy tabl
e.Swapfi
elds2and3ofmyt
able.Cal
litmyt
abl
e.Printt
henew fi
l
e
my t
able.
Descr
ipt
ion:
a)Cr
eateafil
emy
tabl
econtai
ningt
hef
oll
owi
ngdat
a.Usetabst
osepar
ate
thef
iel
ds.
1425 Ravi IT 15.
65
4320 Ramu ECE 26.
27
6830 Sit
a CSE 36.
15
1450 Raju IT 21.
86
b)Usethecatcommandt odisplaythefi
l
e,my t
able.
c)Usethesortcommandt osor tthefi
lemytabl
eaccordi
ngtot
hefi
rstfi
eld.
Cal
lthesortedfi
l
emyt able(samename)
d)Usethecutandpastecommandst oswapf i
elds2and3ofmyt
able.Call
it
mytable(samename)
e)Pri
ntthenewf i
lemy
t abl
e
Pr
ogr
am:
a)Cr
eat
ingthefi
lemytable.Uset
abstosepar
atethef
iel
ds.
$cat>my t
able
1425 Ravi I
T 15.
65
4320 Ramu ECE 26.
27
6830 Si
ta CSE 36.
15
1450 Raju I
T 21.
86
^
d
b)Di
spl
ayi
ngthef
il
emy t
abl
econt
ents:
$catmyt
able
1425 Ravi IT 15.
65
4320 Ramu ECE 26.
27
6830 Si
ta CSE 36.
15
1450 Raj
u IT 21.
86
c)Sort
ingthefi
lemy
table:
$sortmy t
able
1425 Ravi I
T 15.
65
1450 Raju I
T 21.
86
4320 Ramu ECE 26.
27
6830 Sit
a CSE 36.
15
d)Swapi
ngt
hef
iel
ds2and3
$cut–f1my
tsbl
e>fil
e1
$cut-
f2myt
able>f
ie2
$cut-
f3myt
able>f
il
e3
$cut-
f4myt
able>f
il
e4
13
AR16
$pastef
il
e1fil
e3f
il
e2fi
le4>mytabl
e
$catmytabl
e
1425 I
T Rav
i 15.
65
1450 I
T Raj
u 21.
86
4320 ECE Ramu 26.
27
6830 CSE Sita 36.
15
e)Pr
int
ingt
hef
il
emy
tabl
e:
$lprmy
tabl
e
1425 I
T Ravi 15.
65
1450 I
T Raj
u 21.
86
4320 ECE Ramu 26.
27
6830 CSE Si
ta 36.
15
14
AR16
3.a)Tocheckwhet
hert
heuseri
sel
i
gibl
eforv
oteornot
.
Aim:Wr
it
eashellscr
iptt
ocheckwhet
hert
heuseri
sel
i
gibl
eforv
oteornotby
usi
ngdat
eandawkcommands.
Pr
ogr
am:
a)$vivot
e1.sh
echo"Entert
heage"
readage
i
f[$age-ge18]
then
echo"Userisel
igi
bleforvot
e"
else
echo"UserisNoteli
gibl
eforvot
e"
fi
Out
put
:
$shv ote1.sh
or
$./
vote1.sh
EntertheAge
18
Useriseligibl
eforv
ote
$shv ote1.
sh
or
$./
vote1.sh
EntertheAge
17
UserisNoteligi
blef
orv
ote
b)$v
ivot e2.
sh
echo" Entert
hebirt
hy ear"
readby ear
cyear=`date|awk'{
pri
nt$6} '
`
age=`expr$cyear-$by ear
`
i
f[$age- ge18]
then
echo" Userisel
igi
bleforvote"
else
echo" UserisNoteli
gibleforvot
e"
fi
Out
put
:
$shvot
e2.sh
or
$./
vot
e2.sh
15
AR16
Entert
hebir
thyear
1983
Userisel
i
gibl
eforvot
e
Out
put
:
$shv ote2.
sh
or
$./
vote2.sh
Enterthebirt
hy ear
2010
UserisNotel i
gibl
eforv
ote
c)
$vi
vot e3.
sh
echo" Entert
hebirthyear"
readby ear
cyear=`whoam i|awk'{pri
nt$3}'
|
awk-
F"-
"'
{pr
int$1}
'`
age=`expr$cyear-$by ear
`
i
f[$age- ge18]
then
echo" Userisel
igibl
eforvote"
else
echo" UserisNoteligi
bleforvot
e"
fi
Out
put
:
$shv ote3.sh
Enterthebi r
thyear
1983
Userisel i
giblef
orvot
e
$shv ote3.sh
Enterthebi r
thyear
2010
Noteligibl
ef orvot
e
16
AR16
3.b)Tocheckst
hesy
stem st
atuswhet
hert
hesy
stem i
sheav
il
yloaded,
medi
uml
yloaded
orl
owl
yloaded.
Ai
m:Wr i
teashellpr
ogram whi
chpri
ntst
hesyst
em i
sheav
il
yloaded,medi
uml
y
l
oadedorl
owl
yloadedbyusi
ngwhocommand.
Pr
ogr
am:
$v i
syst
eml oad.sh
N=who| wc- l
i
f[$N- gt100]
then
echo"System isHeav
ilyloaded"
eli
f[$N-gt50- a$N-l
t100]
then
echo"System isMedi
uml yloaded"
else
echo"Sy st
em isLowlyloaded"
fi
Out
put
:
$shload.
sh
Syst
em isLowl
yl oaded
$shload.
sh
Syst
em ismediuml yl
oaded
$shload.
sh
Syst
em isheav
ilyloaded
17
AR16
4.a)Toi
nfor
m assoonasaspeci
fi
eduserwhosenamei
sloggedi
ntot
hesy
stem.
Ai
m: Wr
it
eashel
lpr
ogr
am whi
chinf
ormsassoonasaspeci
fi
eduserwhosenamei
s
gi
venal
ongt
hecommandli
nei
sloggedi
ntot
hesystem.
Pr
ogram:
$v i
user
logi
n.sh
user1=`
who| grep$1| cut-d""-f
1`
if[$1==$user 1]
then
echo"Useri
sl oggedintothesy stem"
else
echo"Useri
sNotl oggedint othesy st
em"
fi
Out
put
:
$shuser.
sh16a51a1201
Useri
sloggedint
othesyst
em
$shuser
.sh16a51a1251
Useri
sNotloggedint
othesyst
em
4.b)Tor
eadaf
il
enameand3di
rect
orynamesandt
ochecki
fthef
il
eisf
oundornotf
ound
i
nanyoft
hedi
rect
ori
es”
.
Aim:Wri
teaprogr
am whi
chreadsaf
il
enameand3dir
ect
orynamesalongthe
commandlineandpr
int“
YES”ift
hef
il
eisf
oundi
nanyoft
hedirect
ori
esother
wisepr
int
”
notf
ound”
.
Pr
ogram:
$v i
searchfil
eyes.sh
echo" Enterfi
lename"
readfn
forvarin$1$2$3
do
if[`l
s$v ar|
grep$f n`]
then
echo" Yes,the$fnisf
ound,
iti
sin$v
ardir
ector
y "
else
echo" NotFound, t
hegiv
en$fni
snotin$vardi
rector
y"
fi
done
Out
put
:
$shfile.
shdemoDeskt opPubl
ic
Enterfil
ename
fi
le1
Yes,thefil
e1isfound,
iti
sindemodirector
y
NotFound, thegi
venfi
le1i
snotinDesktopdirect
ory
NotFound, thegi
venfi
le1i
snotinPubli
cdirector
y
5.a)Tor
emov
eaf
il
e.
18
AR16
Ai
m:Wr
it
easy
stem pr
ogr
am t
oremov
eaf
il
eusi
ngr
emov
esy
stem cal
l
Pr
ogram:
$v iremov e.c
#incl ude<st dio.h>
#incl ude<sy s/ stat.
h>
#incl ude<f cntl.h>
mai n( )
{
inta;
a=r emov e("/home/ uni
x/f
il
exy
z")
;
if(
a>=0)
printf("remov ed");
else
printf("notr emov ed");
}
Out
put
:
$gcc-oa.outr
emov
e.c
$./
a.out
removed
5.b)Tor
enameaf
il
e
Ai
m: Wr iteasy stem pr ogram t
orenameaf
il
eusi
ngr
enamesy
stem cal
l
Pr
ogram:
$v irename. c
#incl ude<stdio.h>
#incl ude<sy s/stat
.h>
#incl ude<fcntl.h>
mai n( )
{
inta;
a=r ename( "
oldfil
e",
"newfil
e")
;
if(
a<0)
printf("notrenamed" )
;
else
printf("renamed" );
}
Out
put
:
$gcc-oa.outr
ename.
c
$./a.
out
renamed
6.a)Tol
i
nkaf
il
e
Ai
m:Wr
it
easy
stem pr
ogr
am t
oli
nkaf
il
eusi
ngl
i
nksy
stem cal
l
19
AR16
Pr
ogr
am:
$vi l
ink.
c
#include<stdio.h>
#include<sys/st at
.h>
#include<fcntl.
h>
#include<unistd.h>
mai n()
{
i
nta;
a=link("
fi
le1",
"li
nkfil
e")
;
i
f(a<0)
printf(
"fi
l
esar enotl i
nked"
);
el
se
printf(
"fi
l
esar elinked")
;
}
Out
put
:
$gcc-oa.outl
ink.
c
$.
/a.out
fi
l
esar el
inked
6.b)Toopenaf
il
eandunl
i
nki
t
Ai
m:Wri
teasy
stem pr
ogr
am t
oopenaf
il
eandunl
i
nki
t
Pr
ogr
am:
$v
iunl
i
nk.
c
#include<stdi
o. h>
#include<sys/st at
.h>
#include<fcntl
.h>
mai n()
{
i
ntf ;
f=open( "
fil
e1",00400);
f=unl i
nk("
fil
e1");
i
f(f<0)
printf
("f
il
esar enotunl i
nked"
);
else
printf
("f
il
esar eunl i
nked")
;
}
Out
put
:
$gcc-
oa.
outunl
i
nk.
c
$.
/a.
out
fi
l
esareunl
i
nked
20
AR16
7.a)Tocr
eat
eachi
l
d
Ai
m: Wr i
teasystem program t
hatcreatesachil
dusingf ork(
)sy
stem cal
l
Pr
ogram:
#include<st
dio.h>
mai n()
{
intpid;
pid=fork(
);
if(
pid==0)
{
printf
("i
am t
hechi l
dandmypr ocessidi
s%d\n",
getpid()
);
printf
("t
hechild'
sparentprocessidis%d\n",
getppi
d( )
);
}
else
{
printf
("i
am t
hepar ent,
mypr ocessidis%d\n",
getpid()
);
printf
("t
heparentsparentprocessidis%d\n",
getppid()
);
}
}
Out
put:
$gccprocessfork.
c
$./
a.out
iam thechil
dandmypr ocessi
dis5257
thechil
d'sparentprocessidis5256
iam theparent,mypr ocessidis5256
theparentsparentprocessidis3219
7.b)TocreateanOrphan.
Aim:Writ
easy st
em progr
am t
hatcr
eat
esanOr
phan.
Progr
am:
$vior
phan.c
#i
nclude<stdio.h>
#i
nclude<unistd.h>
main()
{
i
ntpid;
pr
intf
("Iam theor i
ginalprocesswi thpid%dandppi d%d\ n",
getpid()
,getppid()
);
pi
d=fork()
; /*creati
ngachi ldthr
ead,chil
dandpar entcont i
nuef rom here*
/
i
f(
pid!=0) /*pidisnon- zero,
soitmustbet hepar ent
* /
{
pr
intf
("Iam thepar entprocesswi thpid%dandppi d%d\ n",
getpid(),
getppid()
);
pr
intf
("mychi l
dspi dis%d\ n",
pid);
}
el
se /*pidiszero,soitmustbet hechild*/
{
sl
eep(5); /*makesur et hatt
heparentteri
minatesf i
rst*/
pr
intf
("Iam thechi l
dpr ocesswi t
hpi d%dandppi d%d\ n",
getpid(),
getppid())
;
21
AR16
}
pr
int
f("
pid%dt
eri
minat
es\
n",
get
pid(
)); /
*bot
hpar
entandchi
l
dexecut
ethi
s*/
}
Out
put :
$gccor phan.c
$./a.out
Iam t heori
ginalprocesswi
thpid3578andppid3219
Iam t heparentprocesswit
hpid3578andppid3219
mychi l
dspidis3579
pid3578t eri
mi nat
es
$Iam t hechildprocesswit
hpid3579andppid1
pid3579t eri
mi nat
es
22
AR16
8.a)Tocr
eat
esaZombi
e
Ai
m:Wr
it
easy
stem pr
ogr
am t
hatcr
eat
esaZombi
e
Pr
ogr
am:
$vizombie.
c
#i
nclude<stdio.h>
#i
nclude<stdli
b.h>
#i
nclude<unistd.h>
i
ntmai n(
)
{
i
ntpid;
pr
intf(
"i
am theor igi
nalprocesswithpi
d%dandppi d%d\n"
,get
pid(
),
get
ppi
d())
;
pi
d=fork(
); /*cr
eati
ngachildthr
ead,chi
ldandpar
entconti
nuefr
om her
e*/
i
f(
pid!=0) /*pidisnon-zer
o,soitmustbethepar
ent*/
{
pri
ntf(
"i
am theparentprocesswi thpi d%dandppi d%d\ n",
getpi
d()
,get
ppi
d()
);
sl
eep(15);
/*makesur ethatthepar entteri
mi natesfi
rst*
/
pri
ntf(
"mypar ent
spidis%d\ n"
,getpid())
;
}
el
se /*pidi
szero,soitmustbet hechild*/
pri
ntf(
"i
am thechildprocesswithpi d%dt er
mi nat
es\ n"
,getpi
d()
);
exi
t(0)
;
}
Out
put:
$gcczombie.c
$./
a.out
i
am theori
ginalpr
ocesswi
thpi
d6347andppi
d3219
i
am t
hepar
entpr
ocesswi
thpi
d6347andppi
d3219
i
am t
hechi
l
dpr
ocesswi
thpi
d6348t
ermi
nat
es
mypar
ent
spi
dis6347
23
AR16
8.b)Towai
tfori
tschi
l
dtot
ermi
nat
e
Ai
m:Wr
it
easy
stem pr
ogr
am t
hatwai
tsf
ori
tschi
l
dtot
ermi
nat
eusi
ngwai
t(
)sy
stem
Pr
ogram:
#include<stdi o.h>
#include<sy s/types.h>
#include<uni std.h>
#include<stdl i
b.h>
mai n()
{
pid_tpid;intst atus;
pid=f ork();
if
(pid==0)
{
sl
eep( 10) ;
pri
ntf("Iam Child\
n")
;
exi
t(0);
}
else
{
wai t
(&status) ;
printf
("Iam par ent\n")
;
}
}
Out
put
:
Chi
ld
Iam par
ent
24
AR16
9.Todemonst
rat
eaboutsy
stem cal
l
s
Ai
m:Wr
it
eaUNI
Xsy
stem pr
ogr
am t
hatdemonst
rat
essy
stem cal
l
s
Pr
ogr
am:
(
i)ki
l
l
$vikill
.c
#include<stdio.h>
#include<wai t
.h>
#include<signal.h>
i
ntmai n()
{
i
ntst at;
pi
d_ tpid;
i
f( (
pid=f or
k())==0)
while(1);
el
se
{
ki
ll(
pid, SIGI
NT) ;
wait(&stat);
i
f( WIFSI GNALED( st
at)
)
psignal(WTERMSI G(st
at)
,"Chi
l
dter
minat
edduet
o")
;
}
}
Out
put
:
$gcc-oa.
outki
ll
.c
$./a.
out
Chil
dter
minat
edduet
o:I
nter
rupt
(
ii
)rai
se
$vi raise.c
#incl ude<st dio.h>
#incl ude<si gnal.h>
l
ongpr ev _
fact,i;
voidSI Ghandler (
int)
;
voidSI Ghandler (
intsig)
{
printf("\nReceivedaSI GUSR1.Theansweri
s%l
d!=%l
d\n"
,
i
-1, prev _f
act);
exit(0);
}
v
oidmai
n(v
oid)
{
l
ongf
act
;
25
AR16
printf("Factorial
Comput at
ion:\
n\n");
signal (
SI GUSR1, SI
Ghandler)
;
for( prev _f
act=i =1;;i
++, pr
ev_fact=fact
){
fact=pr ev_fact*i;
i
f( fact<0)
raise(SI GUSR1) ;
elsei f(i %3==0)
printf(" %l d!=%ld\n"
,i,f
act);
}
}
Out
put
:
$gcc- oa.outr aise.c
raise.c: I
nf unction‘SIGhandl
er’
:
raise.c:12:6:war ning:incompat
ibl
eimpl
i
citdecl
arat
ionofbui
l
t-i
nfunct
ion‘
exi
t’
[enabl edbydef ault]
exit(0);
^
$./ a.out
Fact orialComput ati
on:
3!=6
6!=720
9!=362880
12!=479001600
15!=1307674368000
18!=6402373705728000
Recei
vedaSI
GUSR1.Theansweri
s20!=2432902008176640000
(
ii
i)al
arm
$vi al
arm.c
#include<stdi
o.h>
#include<si
gnal.h>
i
ntmai n(
)
{
alarm(5);
whi l
e(1)
{
printf
("Hel
loWor ld\
n")
;
}
return0;
}
Out
put
:
$gccalar
m.c
$./
a.out
Hel
loWorld
Hel
loWorld
26
AR16
.
Hel
loWorld
Hel
loWorld <-
-
Al
arm cl
ock
$
(
iv)pause
$vi pause.c
#include<stdio.
h>
#include<signal
.h>
i
ntmai n()
{
printf(
"HellWorld\n"
);
pause( );
printf(
"I
T-AITAM- TEKKALI
\n"
);
}
Out
put
:
$gccpause.
c
$./a.
out
HellWor
ld
<-
-her
eaf
terpaused,
then
^C
$
(
v)abor
t
$vi abort
.c
#include<stdio.
h>
#include<signal
.h>
voidmai n()
{
printf(
"Hell
oWor ld\
n")
;
abor t
();
printf(
"I
T-AITAM-TEKKALI
\n"
);
}
Out
put
:
$gccabort
.c
$./a.
out
Hell
oWorld
Abort
ed(coredumped)
27
AR16
(
vi)sl
eep
a)$v
isl
eep1.
c
#i
nclude<stdi
o.h>
#i
nclude<si
gnal.h>
i
ntmai n(
)
{
pr
int
f("Hel
loWor ld\
n")
;
sl
eep(2);
}
Out
put
:
$gccfir
st.
c
$./
a.out
Hel
loWor l
d
<-
-af
ter2secondssl
eep,
promptwi
l
lappear
$
b)$v
isl
eep5.
c
#i
nclude<stdio.
h>
#i
nclude<signal
.h>
main()
{
pr
intf
("Hell
oWor ld\
n")
;
sl
eep(5);
pr
intf
("I
T-AITAM-TEKKALI
\n"
);
}
Out
put
:
$gccsleep.
c
$./a.
out
Hell
oWor l
d
<-
-af
ter5secondssl
eep,
then
I
T-AITAM-TEKKALI
$
c)$v isl
eepinfi
loop.c
#include<stdio.
h>
#include<signal
.h>
i
ntmai n(
)
{
whi l
e(1)
{
printf
("Hell
oWor ld\n"
);
sleep(2);
}
return0;
}
28
AR16
Out
put
:
$gccfir
st.
c
$./a.
out
Hell
oWor l
d
Hell
oWor l
d
Hell
oWor l
d
Hell
oWor l
d
^C
<-
-Sendi
ngasi
gnal
:ct
rl
-c
$
29
AR16
10.
Tosi
mul
atepr
oducerandconsumerpr
obl
em usi
ngpi
pes.
Ai
m:Wr
it
eaCpr
ogr
am t
osi
mul
atepr
oducerandconsumerpr
obl
em usi
ngpi
pes.
Descr
ipt
ion:Thef
ir
sti
sthedat
apr
oducer
.Itcr
eat
est
hepi
peandt
heni
nvokest
hechi
l
d,
t
hedataconsumer.
Pr
ogr
am:
/
*****
*****
*******
*****
*****
*****
*****
******
*
*Thisprogram demonst r
atesaproducerand*
*consumert hatcommuni catevi
aapi pe. *
***
*****
*******
*****
*****
*****
*****
******
***/
#i
ncl
ude
#i
ncl
ude
#i
ncl
ude
/
*****
*********
*****
** *
*****
*******
* *
*****
**
* producer *
***
*******
* *
*****
********
*****
*********
****
*Wr i
teinformati
oni ntothepipewhose *
*writ
e-endi sgiv
enbypi pe_wr i
te_end.*
***
*******
* *
*****
********
*****
*********
****/
v
oi dproducer(FI
LE* pi
pe_wri
te_end)
{
i
nti;
for(
i=1; i<=100; i
++){
fpr
intf
(pipe_wri
te_end,"
%d" ,
i);
}
fcl
ose(pipe_wri
te_end);
exit
(0);
}
/
******
******
******
*****
*******
*******
*****
*
* consumer *
****
******
*****
******
*****
* *
*******
*****
***
*Readi nf
ormationfrom thepipewhose *
*read-endisgivenbypipe_read_end, *
*andcopyi ttothestandardout put. *
****
******
*****
******
*****
* *
*******
*****
***/
v
oidconsumer
(FI
LE*
pipe_
read_
end)
{
i
ntn,
k;
whi l
e(1){
intn=f scanf(pi
pe_read_
end,"
%d"
,&k);
if(n==1)pr intf(
"consumer:
got%d\n"
,k)
;
elsebr eak;
}
fclose(pipe_read_end);
exit(0)
;
}
30
AR16
/
*****
*******
*****
*****
*****
******
*****
*****
* main *
***
*******
*****
*****
*****
******
*****
*****
**
*Buil
dt heproducerandconsumerand *
*waitforthem tofi
nish. *
***
*******
*****
*****
*****
******
*****
*****
**/
i
ntmain()
{
pi
d_tproducer
_id,consumer_
id;
i
ntpd[2];
FI
LE*pipe_wri
te_end,*pi
pe_
read_end;
/
*--
--
--
--
--
--
--
--
-*
*Buil
dthepipe.*
*-
--
--
--
--
--
--
--
--
*/
pi
pe(
pd);
pi
pe_
read_end=f
dopen(
pd[
0],
"r"
);
pi
pe_
writ
e_end=f
dopen(
pd[
1],
"w");
/
*--
--
--
--
--
--
---
--
--
-*
*Forktheproducer
.*
*-
--
--
--
--
---
--
--
--
--
*/
producer_id=fork(
);
i
f(producer_i
d==0){
fcl
ose(pipe_r
ead_end);
producer(pi
pe_wri
te_end)
;
}
/
*--
--
--
--
--
--
---
--
--
-*
*Forktheconsumer
.*
*-
--
--
--
--
---
--
--
--
--
*/
consumer_id=fork()
;
i
f(consumer_i
d==0){
fcl
ose(pi
pe_wri
te_end);
consumer(pi
pe_read_end)
;
}
/
*--
--
--
--
--
--
--
--
--
--
--
---
-*
*Waitforbothtofini
sh.*
*-
--
--
--
--
--
--
--
--
--
---
--
--
*/
f
close(
pipe_
read_end)
;
f
close(
pipe_
writ
e_end);
wait
(NULL);
wait
(NULL);
r
etur
n0;
}
31
AR16
11.
Aim:Wr
it
eUNI
Xsy
stem pr
ogr
am orpr
ogr
amst
odemonst
rat
etheI
PCmechani
sm semaphor
es-
or
ient
edsy
stem cal
l
s.
/*Fil
ename: shm_ writ
e_cnt
r.
c*/
#incl
ude<stdio.h>
#incl
ude<sys/ipc.h>
#incl
ude<sys/shm. h>
#incl
ude<sys/types.h>
#incl
ude<stri
ng. h>
#incl
ude<errno.h>
#incl
ude<stdli
b.h>
#incl
ude<unistd.h>
#incl
ude<stri
ng. h>
#defi
neSHM_ KEY0x12345
str
uctshmseg{
intcntr;
intwrit
e_complete;
intread_complet
e;
};
voidshared_memor y
_cnt
r_i
ncr
ement
(i
ntpi
d,st
ructshmseg*
shmp,
intt
otal
_count
);
i
ntmai n(intargc,char*argv[
]){
i
ntshmi d;
structshmseg* shmp;
char* bufptr;
i
ntt otal_count;
i
ntsl eep_ t
ime;
pid_tpid;
i
f( argc!=2)
total_count=10000;
else{
tot
al _
count=at oi(
argv[1]
);
i
f(total_count<10000)
tot
al _
count=10000;
}
printf(
"TotalCountis%d\ n",total_
count)
;
shmi d=shmget (SHM_ KEY, sizeof(
str
uctshmseg)
,0644|
IPC_
CREAT)
;
i
f(shmid==-1){
perr
or(
"Sharedmemor
y")
;
ret
urn1;
}
//At t
acht othesegmenttogetapoint
ert
oit
.
shmp=shmat (
shmid,NULL,0)
;
i
f( shmp==( void*
)-1){
perr
or("Sharedmemor yatt
ach")
;
ret
urn1;
}
shmp- >cntr=0;
pid=f ork()
;
/*ParentProcess-Wr
iti
ngOnce*
/
i
f( pi
d>0){
shar
ed_memor y_
cnt
r_i
ncr
ement
(pi
d,shmp,
tot
al_
count
);
}elseif(
pid==0){
32
AR16
shar ed_memory_
cntr_
incr
ement
(pi
d,shmp,
tot
al_
count
);
return0;
}else{
per r
or("
ForkFai
l
ure\n"
);
return1;
}
while(shmp->r
ead_complet
e!=1)
sl
eep( 1);
i
f(shmdt(shmp)==-
1){
perr
or(
"shmdt")
;
ret
urn1;
}
i
f( shmct l
(shmid,I
PC_RMID,
0)==-1){
perror(
"shmctl"
);
ret
ur n1;
}
printf
("Writi
ngProcess:
Complet
e\n"
);
return0;
}
/
*Incrementt hecount erofsharedmemor ybytotal
_counti
nstepsof1*/
v
oidshar ed_memor y_
cntr_i
ncrement(
intpid,str
uctshmseg*shmp,intt
otal
_count
){
i
ntcnt r;
i
ntnumt i
mes;
i
ntsleep_ ti
me;
cntr=shmp- >cntr
;
shmp- >writ
e_ complete=0;
i
f( pi
d==0)
print
f("
SHM_ WRITE: CHILD:Nowwr i
ting\n")
;
el
sei f(pid>0)
print
f("
SHM_ WRITE: PARENT: Nowwr it
ing\n"
);
//pri
ntf("
SHM_ CNTRi s%d\n",shmp->cntr)
;
/
*Incrementthecounteri
nshar
edmemor ybytot
al_
counti
nst
epsof1*
/
f
or(numtimes=0; numtimes<t
otal
_count
;numti
mes++){
cntr+=1;
shmp->cnt
r=cntr;
/*Sleepingforasecondf
orev
eryt
housand*
/
sleep_ti
me=cnt r%1000;
i
f( sl
eep_ti
me==0)
sleep(1)
;
}
shmp- >writ
e_complete=1;
if(pi
d==0)
print
f (
"SHM_ WRITE:CHILD:Wr i
ti
ngDone\n"
);
elseif(pid>0)
print
f (
"SHM_ WRITE:PARENT: Writ
ingDone\
n")
;
return;
}
TotalCountis10000
SHM_ WRI TE:PARENT: Nowwr it
ing
SHM_ WRI TE:CHILD:Nowwr i
ti
ng
SHM_ WRI TE:PARENT: Writ
ingDone
SHM_ WRI TE:CHILD:Writ
ingDone
Writi
ngPr ocess:Complet
e
33
AR16
12Aim:Writ
eUNI Xsyst
em pr
ogr
am orpr
ogr
amst
odemonst
rat
etheI
PCmechani
sm
Messagequeues-
ori
ent
edsyst
em cal
l
s.
/*Fi
l
ename: msgq_send.
c*/
#i
ncl
ude<st
dio.h>
#i
ncl
ude<st
ri
ng. h>
#i
ncl
ude<st
dli
b.h>
#i
ncl
ude<er
rno.h>
#i
ncl
ude<st
ri
ng. h>
#i
ncl
ude<sy
s/types.h>
#i
ncl
ude<sy
s/ipc.h>
#i
ncl
ude<sy
s/msg. h>
#defi
nePERMS0644
str
uctmy_msgbuf{
longmtype;
charmtext[
200]
;
};
i
ntmai n(
void){
str
uctmy _msgbufbuf
;
i
ntmsqi d;
i
ntlen;
key_tkey;
syst
em( "t
ouchmsgq.t
xt"
);
i
f((
key=f t
ok("msgq.
txt
",'
B'
))==-
1){
perr
or("
ftok"
);
exi
t(1)
;
}
i
f((msqi d=msgget(
key,PERMS| I
PC_CREAT))==-1){
perror("
msgget"
);
exit
(1);
}
pri
ntf
("messagequeue:r
eadyt osendmessages.\n"
);
pri
ntf
("Enterl
i
nesoftext
,^Dt oquit
:\n"
);
buf.
mt ype=1;/*wedon'treal
lycareinthi
scase*/
whil
e(fgets( buf.mtext,si
zeofbuf.mtext,stdin)!
=NULL){
len=st rlen(buf.mt ext
);
/*remov enewl i
neatend, ifi
texist
s* /
if(buf.
mt ext[
len-1]=='\n'
)buf.mtext[
len-1]='\0'
;
if(msgsnd( msqi d,&buf,l
en+1,0)==- 1)/ *+1for'\
0'*/
perror(
" msgsnd" );
}
str
cpy(buf .
mt ext,"end");
l
en=st rlen( buf
.mt ext);
i
f(msgsnd( msqi d,&buf ,l
en+1,0)==-1)/ *+1f or'
\0'*/
perr
or("msgsnd" );
i
f( msgctl(
msqid,I
PC_RMID,NULL)==-1){
per
ror("
msgctl"
);
exi
t(1);
}
print
f("messagequeue:
donesendingmessages.
\n"
);
retur
n0;
}
34
AR16
messagequeue:readyt
osendmessages.
Enterl
i
nesoftext
, ^
Dtoquit
:
thi
sisli
ne1
thi
sisli
ne2
messagequeue:donesendi
ngmessages.
35
AR16
13Aim:
Wri
teUNIXsyst
em pr
ogram orpr
ogr
amst
odemonst
rat
etheI
PCmechani
sm
shar
ed memor y
-or
ient
edsyst
em call
s.
/*Fi l
ename: shm_
wri
te.
c*/
#i
ncl
ude<stdio.h>
#i
ncl
ude<sys/ipc.h>
#i
ncl
ude<sys/shm. h>
#i
ncl
ude<sys/types.h>
#i
ncl
ude<stri
ng. h>
#i
ncl
ude<errno.h>
#i
ncl
ude<stdli
b.h>
#i
ncl
ude<unistd.h>
#i
ncl
ude<stri
ng. h>
#def
ineBUF_
SIZE1024
#def
ineSHM_KEY0x1234
str
uctshmseg{
intcnt;
intcomplete;
charbuf[BUF_SI
ZE];
};
i
ntfil
l_
buffer
(char*bufpt
r,i
ntsi
ze)
;
i
ntmai n(i
ntar gc,char*argv
[]){
i
ntshmi d,numt imes;
str
uctshmseg* shmp;
char* bufptr
;
i
ntspaceav ail
abl e;
shmid=shmget (SHM_KEY, si
zeof
(st
ructshmseg)
,0644|
IPC_
CREAT)
;
i
f(shmi d==- 1){
perror("Shar edmemor y"
);
return1;
}
//At t
achtothesegmenttogetapoint
ert
oit
.
shmp=shmat (
shmid,NULL,0)
;
i
f( shmp==( void*
)-1){
perr
or(
"Sharedmemor yatt
ach")
;
ret
urn1;
}
i
f(shmdt
(shmp)==-
1){
36
AR16
perr
or(
"shmdt
");
ret
urn1;
}
i
f( shmct l
(shmid,I
PC_RMID,
0)==-1){
perror(
"shmctl"
);
ret
ur n1;
}
printf
("Writi
ngProcess:
Complet
e\n"
);
return0;
}
i
ntf
il
l_
buff
er(
char*buf
ptr
,intsi
ze){
st
ati
ccharch='A'
;
i
ntfi
ll
ed_
count;
//
printf
("sizeis%d\ n",si
ze);
memset ( bufpt
r,ch,size-1);
bufptr[
size-1]='\0';
i
f(ch>122)
ch=65;
i
f(( ch>=65)&&( ch<=122)){
if((ch>=91)&&( ch<=96)){
ch=65;
}
}
fi
l
led_count=st r
len(bufpt
r);
//pri
ntf
("buf
fercountis:%d\n"
,fi
l
led_count
);
//pri
ntf
("buf
ferfi
ll
edis:%s\n"
,buf
ptr);
ch++;
retur
nfil
led_count
;
}
Writ
ingProcess:SharedMemor yWr i
te:Wrot
e1023by
tes
Writ
ingProcess:SharedMemor yWr i
te:Wrot
e1023by
tes
Writ
ingProcess:SharedMemor yWr i
te:Wrot
e1023by
tes
Writ
ingProcess:SharedMemor yWr i
te:Wrot
e1023by
tes
Writ
ingProcess:SharedMemor yWr i
te:Wrot
e1023by
tes
Writ
ingProcess:Wrote5t i
mes
Writ
ingProcess:Complete
37
AR16
Addi
ti
onal
Exper
iment
s
1)Tocr
eat
esmal
lcal
cul
atort
hatadds,
subt
ract
s,mul
ti
pli
esanddi
vi
des.
Ai
m:Wr it
eashel lscri
pt(
smallcalculator)t
hatadds,subtracts,multi
pli
esanddi vi
desthe
gi
ventwointegers.
Therearedivisi
onopti
ons:oner eturnsthequoti
entandt heot herret
urnsremainder.
Thescriptrequir
es3argument s:Theoper ati
ontobeusedandt wointegernumbers.
Theoptionsar eadd(-
a),
subtract( -
s),multi
ply(
-m),quotient(-
c)andr emainder)
-r
).
Al
gor
it
hm:
St
ep1: Start
St
ep2: reada, b,opti
on
St
ep3: i
f( opt
ion==1)then
Result
=a+b
Printr
esul t
St
ep4: i
f( opt
ion==2)then
Result
=a- b
Printr
esul t
St
ep5: i
f(opti
on==3)t hen
Result
=a* b
Printr
esul t
St
ep6: i
f(opti
on==4)t hen
Result
=a%b
Printr
esul t
St
ep7: i
f(opti
on==5)
Result
=a/ b
Printr
esul t
Elseif
Print‘
Invali
doperati
on’
St
ep8: Stop
Pr
ogr
am:
echo“EnterFi
rstValue“
readx
echo“EnterSecondValue“
ready
while[
$q–ne0]
do
echo“Enter–aforadding”
echo“Enter–sforsubtracti
on”
echo“Enter–m formultipl
i
cati
on”
echo“Enter–cforQuot i
ent”
echo“Enter–rforreminder”
reads
case$sin
-a)p=`
expr$x+$y `
Echo“Sum =$p”
;
;
-b)p=`
expr$x-$y `
38
AR16
Echo“
diff
erence=$p”
;
;
-
m)p=`expr$x\*$y`
Echo“
Product=$p”
;
;
-
c)p=`
expr$x/$y `
Echo“
quotient=$p”
;
;
-
r)p=`
expr$x%$y `
Echo“
reminder=$p”
;
;
Out
put
:
Ent
erFirstVal
ue: 4
Ent
erSecondValue: 5
Ent
erthechoice m
Theresulti
s 20
Ent
erFirstVal
ue: 8
Ent
erSecondValue: 2
Ent
erthechoice r
Theresulti
s 4
Vi
va-
VoceQuest
ions:
1.Expl
ainthesynt
axofswitchcasestatement?
2.Whichcommandi susedforli
nkingaf i
l
e?
3.Whatdoy oumeanbylinkingafil
e?
4.Whichcommandi susedfordisplay
ingtime?
5.Whichcommandi susedtosearchapat ter
ningi
venf
il
e?
6.Expl
ainsynt
axofcatcommand?
39
AR16
2)Toi
ll
ustr
atestheSi
mplex,Duplex&Ful lDuplexwayofcommuni cati
on.
Ai
m:Wri
teCpr ogr
am t
hatil
lust
ratesthefollowing
a)Two-waycommuni cati
on( Si
mplex)wi t
hsingl
epi pe.
b)Two-waycommuni cati
on( Dupl
ex)wi t
huni-di
recti
onalpipes.
c)Two-waycommuni cati
on(Fullduplex)wit
hbi-di
recti
onal
pipes.
Pr
ogr
am:
#include<st dio.
h>
#include<fcnt l.
h>
mai n()
{
intpfd1[2],
pf d2[2],
p,
x1,x2;
charmsg1[ ]="from parent:
hai hel
lo"
;charmsg2[]
="f
rom chi
l
d:howru"
;charbuf
[20]
;
x1=st r
len(msg1) ;
x2=st r
len(msg2) ;
pipe(pfd1);
pipe(pfd2);
p=fork();
if(
p==0) //f
rom par enttochild
{
close(pfd1[1]);
read(pfd1[0],buf,x
1);print
f("
\nchil
drecievedi
s%s",
buf);
}
else
{
close(pfd1[0]);
wr i
te(pfd1[1],
msg1, x1);
}
if(
p==0) //f
rom chi l
dt oparent
{
close(pfd2[0]);
wr i
te(pfd2[1],
msg2, x2);
}
else
{
close(pfd2[1]);
read(pfd2[0],buf,x
2);print
f("
\nparentreci
evedi
s%s",buf
);
}
}
40
AR16
Openendedpr
ogr
ams
1)Tor
ever
set
her
owsandcol
umnsofamat
ri
x.
Ai
m:Wr
it
eashel
lscr
iptt
orev
erset
her
owsandcol
umnsofamat
ri
x.
Al
gor
it
hm
St
ep1:Start
St
ep2:readm1[ ]
,
m2[ ]
,a,
b
St
ep3:fori=1toa
forj=1tob
printm1[i
][
j]
St
ep4:fori=1tob
forj=1toa
m2[ i
][
j]
=m1[i]
[j
]
St
ep5:printm2[i
][
j]
St
ep6:Stop
Pr
ogr
am:
41
AR16
Out
put
:
Ent
erNumberofrows: 2
Ent
erNumberofcolumns: 3
Ent
erel
ements: 1 2 3
4 5 6
Tr
ansposeofaMat
ri
x 1 4
2 5
3 6
Vi
va-
VoceQuest
ions:
1.Whatar
ethethreemodesofv iedit
or?
2.Whi
chcommandsar eusedtoperfor
m copy&past
eoper
ati
onsi
nvi
edi
tor
?
3.Whati
skernel
?
4.Whatar
ethevariousshel
lresponsi
bil
i
ties?
5.Whatar
ethevariousshel
lcommands?
42
AR16
2)I
ll
ust
rat
est
heusesoft
hemkdi
r,opendi
r,r
eaddi
r,cl
osedi
r,
andr
mdi
rAPI
s.
Aim:
Writ
eaCpr
ogr
am t
hati
l
lust
rat
est
heusesoft
hemkdi
r,opendi
r,r
eaddi
r,cl
osedi
r,
and
rmdi
rAPI
s.
Pr
ogr
am:
#include<st dio. h>
#include<f cnt l
.h>
#include<di rent .h>
mai n()
{
chard[ 10];intc, op; DIR* e;
structdirent* sd;
printf
("**menu* *\n1. createdi r\n2.removedir\
n3. r
eaddir\nenterurchoi
ce"
);
scanf (
"%d" ,&op) ;
swi t
ch(op)
{
case1: pri
nt f("ent erdi rname\ n");scanf(
"%s",
&d);
c=mkdi r(d,777) ;
if
(c==1)
printf
("dirisnotcr eat ed");el
se
printf
("diriscr eat ed" );break;
case2: pri
nt f("ent erdi rname\ n");scanf(
"%s",
&d);
c=rmdi r(d);
if
(c==1)
printf
("dirisnotr emov ed");else
printf
("dirisr emov ed");break;
case3: pri
nt f("ent erdi rnamet oopen" );scanf
("%s",
&d);
e=opendi r
(d);
if
(e==NULL)
printf
("dirdoesnotexi st"
);else
{
printf
("direxist \n" );whi l
e((sd=r eaddir(
e))!
=NULL)print
f("
%s\t"
,sd->d_
name);
}
closedir(e);
break;
}
}
Out
put
:
$cc–op10. cp10$./p10
**Menu* *
1.creat
edir
2.remov edi
r
3.readdirenterurchoi
ce
enterdirnameex3
di
ri screat
ed
43
AR16
3) Fi
l
elocki
ngusi
ngsemaphor
es.
Ai
m:Wr
it
eaCPr
ogr
am t
hati
l
lust
rat
esf
il
elocki
ngusi
ngsemaphor
es
Al
gor
it
hm:
St
ep1:
Start
St
ep2:
readi ntsemi dkey_tkey
St
ep3:
createauni onsemun
St
ep4:
if(
(key =ftok(“semdemo. c”,
’
J’)
)==-1)
Perror (“ft
ok” )
St
ep5:
createasemaphor esetwith1semaphor e
If(
(semi d=semget (key,
1,
0666|I
PC_ CREAT))
==-
1)
Perror (“semget ”)
St
ep6:
ini
til
iaz esemaphor e#0t o1
Arg.val =1
St
ep7:
if(
semct l
(semid,0,
SETVAL,arg)==-1)
Perror (semct l”
)
St
ep8:
Stop
Pr
ogr
am:
#include<st dio.h>
#include<sy s/ fi
le.h>
#include<er ror.h>
#include<sy s/ sem. h>
#def i
neMAXBUF100
#def i
neKEY1216
#def i
neSEQFI LE“ suhri
tfi
l
e”
i
ntsemi d,fd;
voidmy _l
ock( i
nt);
voidmy _unlock( int);
unionsemnum
{
i
ntv al;
structsemi d_ds* buf;
short*ar ray;
}arg;
i
ntmai n(])
{
i
ntchi l
d, i,
n, pid,seqno;
charbuf f
[MAXBUF+1] ;
pid=get pi
d( );
i
f((semi d=semget (KEY,1,I
PC_CREAT|
0666)
)==-
1)
{
perror(“semget ”);
exit(
1);
}
arg.val
=1;
i
f(semct l
(semi d,
0,SETVAL,arg)
<0)
44
AR16
per r
or(“semct l
”);
i
f((fd=open( SEQFI LE,2))<0)
{
per r
or(“open” );
exit(1);
}
pid=get pid();
for(i
=0;i<2;i++)
{
my _lock( f
d) ;
l
seek( fd,01,0) ;
i
f((n=read( fd,buff,MAXBUF) )<0)
{
per r
or(“read” );
exit(1);
}
printf(
“pid:%d, Seqno: %d\ n” ,pi
d,seqno)
;
seqno++;
spr i
ntf(
buf f,”%d\n” ,seqno) ;
n=st rl
en(buf f);
l
seek( fd,01,0) ;
i
f(wr it
e(fd,buf f
,n)!=n)
{
per r
or(“wr i
te”);
exit(1);
}
sleep(1) ;
my _unlock( fd);
}
}
voidmy _lock( i
ntf d)
{
structsembuf fsbuf =(0,-1, 0);
i
f(semop( semi d, &sbuf ,1)==0)
printf(
“Locki ng: Resour ce…\ n”)
;
else
printf(
“Errori nLock\ n”
);
}
voidmy _unl ock(intfd)
{
structsembuf fsbuf =(0,1, 0) ;
i
f(semop( semi d, &sbuf ,1)==0)
printf(
“UnLocki ng: Resour ce…\ n”)
;
else
printf(
“Errori nUnl ock\n” )
;
}
45
AR16
Out
put
:
$ccfork.c
$./a.
outfork.
c
I
tisachildprocess
I
tisparentprocess:chi
l
dpi
dis:
16281
Vi
va-
VoceQuest
ions:
1.Whatissemaphor e?
2.Whatarethet wotypesoflocking?Explai
ndiff
erence?
3.Whatisfil
elocki
ng&r ecordlocking?
4.Whichfuncti
onisusedf orfi
lelockingexpl
ainwithsynt
ax?
5.Expl
ainfcntl
funct
ionusedf orrecor dl
ocki
ng?
46
AR16
Ref
erences:
TextBooks:
1.Advancedprogr
ammingintheUni
xenvir
onment,
W.Ri
char
dStev
ens,
StephenA.
Rango,
Addisi
on-Wesl
eyprof
essi
onal
,2ndEdit
ion.
2.YourUnixtheul
ti
mategui
de,Sumi
tabhaDas,TMH,2ndEdi
ti
on.
Ref
erenceBooks:
1.AdvancedUnixProgr
amming,Dr.N.B.
Venkateswarl
u,B.S.
Publi
cati
ons,
2Edit
ion.
2.Uni
xNet workProgr
amming,I
nterProcessCommuni cati
on,W.R.Stev
ens,
PHI /Pear
son,
2
ndEditi
on.
3.Uni
xInternal
stheNewFr
ontier
s, U.
Vahali
a,PearsonEducati
on.
4.I
nter
-Pr
ocessCommuni
cat
ion-npt
el
ht
tps:
//npt
el.
ac.
in/
cour
ses/
106108101/
pdf
/Lect
ure_
Not
es/
Mod%207_
LN.
pdf
47