OS FInal Practical PDF
OS FInal Practical PDF
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
act
ical
No:
-1 Pr
ocessCommuni
cat
ion. Dat
e:-
03-
07-
18
Questi
on:
-(i
)Gi
vesol
uti
ont
othepr
oducer
–consumerpr
obl
em usi
ng
shar
edmemor y
.
Sy
ntax:
/
/pr
oducerconsumerpr
obl
em
cl
assQ{
i
ntn;
bool
eanv
alueset
=fal
se;
sy
nchr
oni
zedi
ntget
(){
whi
l
e(!
val
ueset
)
t
ry{
wai
t(
);
}
cat
ch(
Int
err
upt
edExcept
ione)
{
Sy
stem.
out
.pr
int
ln(
"I
nter
rupt
edExcept
ioncaught
");
}
Sy
stem.
out
.pr
int
ln(
"Got
:"
+n)
;
v
alueset
=fal
se;
not
if
y()
;
r
etur
nn;
}
sy
nchr
oni
zedv
oidput
(i
ntn)
{
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
whi
l
e(v
alueset
)
t
ry{
wai
t(
);
}
cat
ch(
Int
err
upt
edEx
cept
ione)
{
Sy
stem.
out
.pr
int
ln(
"I
nter
rupt
edExcept
ioncaught
");
}
t
his.
n=n;
v
alueset
=tr
ue;
Sy
stem.
out
.pr
int
ln(
"Put
:"
+n)
;
not
if
y()
;
}
}
cl
assPr
oduceri
mpl
ement
sRunnabl
e{
Qq;
Pr
oducer
(Qq)
{
t
his.
q=q;
newThr
ead(
thi
s,
"Pr
oducer
").
star
t()
;
}
publ
i
cvoi
drun(
){
i
nti
=0;
whi
l
e(t
rue)
{
q.
put
(i
++)
;
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
}
}
cl
assConsumeri
mpl
ement
sRunnabl
e{
Qq;
Consumer
(Qq)
{
t
his.
q=q;
newThr
ead(
thi
s,
"Consumer
").
star
t()
;
}
publ
i
cvoi
drun(
){
whi
l
e(t
rue)
{
q.
get
();
}
}
}
cl
assPCFi
xed{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
Qq=newQ(
);
newPr
oducer
(q)
;
newConsumer
(q)
;
Sy
stem.
out
.pr
int
ln(
"pr
essct
rl
+ct
ost
op"
);
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Out
put
:
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
act
ical
No:
-1 Pr
ocessCommuni
cat
ion. Dat
e:-
10-
07-
18
Quest
i -(
on: i
i
)Gi
vesol
uti
ont
othepr
oducer
–consumerpr
obl
em
usi
ngmessagepassi
ng
cl
assI
tem{
v
olat
il
eintcont
ent
;
publ
i
csy
nchr
oni
zedv
oidset
Cont
ent
(i
ntcont
ent
)
{
t
his.
cont
ent
=cont
ent
;
}
publ
i
csy
nchr
oni
zedi
ntget
Cont
ent
()
{
r
etur
ncont
ent
;
}
}
cl
assPr
oduceri
mpl
ement
sRunnabl
e{
I
tem i
tem;
Pr
oducer
(I
tem i
tem)
{
t
his.
it
em=i
tem;
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
publ
i
cIt
em get
It
em(
){
r
etur
nit
em;
}
publ
i
cvoi
dset
It
em(
It
em i
tem)
{
t
his.
it
em=i
tem;
}
publ
i
cvoi
drun(
){
i
nti
=0;
sy
nchr
oni
zed(
thi
s)
{
whi
l
e(t
rue)
{
++i
;
i
f(i
==10)r
etur
n;
Sy
stem.
out
.pr
int
ln(
"Put
ti
ngt
hev
aluei
nit
em"
+i)
;
i
tem.
set
Cont
ent
(i
);
not
if
y()
;
t
ry{
wai
t(
);
}
cat
ch(
Int
err
upt
edExcept
ione)
{
e.
pri
ntSt
ackTr
ace(
);
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
}
}
}
}
}
cl
assConsumeri
mpl
ement
sRunnabl
e{
Pr
oducerpr
oducer
;
Consumer
(Pr
oducerpr
oducer
){
t
his.
producer
=pr
oducer
;
}
publ
i
cvoi
drun(
)
{
sy
nchr
oni
zed(
producer
)
{
whi
l
e(t
rue)
{
Syst
em.
out
.pr
int
ln(
"Consumi
ng"
+pr
oducer
.get
It
em(
).
get
Cont
ent
())
;
pr
oducer
.not
if
y()
;
t
ry{
i
f(
producer
.get
It
em(
).
get
Cont
ent
()==9)
r
etur
n;
pr
oducer
.wai
t(
);
}
cat
ch(
Int
err
upt
edExcept
ione)
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
{
/
/todoaut
o-gener
atedcat
chbl
ock
e.
pri
ntSt
ackTr
ace(
);
}
}
}
}
}
publ
i
ccl
assExecut
e{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
I
tem i
tem1=newI
tem(
);
Pr
oducerpr
oducer
=newPr
oducer
(i
tem1)
;
Consumerconsumer
=newConsumer
(pr
oducer
);
Thr
eadpr
oducer
Thr
ead=newThr
ead(
producer
);
Thr
eadconsumer
Thr
ead=newThr
ead(
consumer
);
pr
oducer
Thr
ead.
star
t()
;
consumer
Thr
ead.
star
t()
;
}
}
Out
put
:
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
act
ical
No.
:-
1 Dat
e:-//
2018
(i
ii
)Onef orm ofcommuni cati
oninaCl i
ent–ServerSystemsenv i
ronment
i
sRemot emet hodi nvocation(RMI).RMIisaJav afeaturesimi
larto
RPCs.RMIal lowsat hreadt oinv
okeamet hodonar emot eobj
ect.
Objectsareconsi deredremot eiftheyresi
dei nadiff
erentJavavirt
ual
machi ne(JVM) .Demonst r at
eRMIpr ogram for
adding/subtr
act i
ng/mul ti
plyi
ng/divi
dingtwonumber s.
PROCEDURE:
1.St
artt
hepr
ocess.
2.Toperf
ormtheRMIoperat
ioncr
eateI
nter
face
f
il
e,Implementati
onf
il
e,Ser
verf
il
eand
Cli
entf
il
e.
3.Per
for
mtheJAVAcompi
l
ati
onf
oral
lfi
l
es.
4.Per
formtheRMIcompil
ati
onf
orthe
i
mplementat
ionfi
l
etocr
eatest
ubandskel
etonbyusi
ng
r
miccommand.
5.St
artt
heRMIr
egi
str
y.
6.Runt
heser
verandcl
i
entf
il
esepar
atel
y.
7.Di
spl
ayt
her
esul
t.
8.Ter
minat
ethepr
ocess.
PROGRAM:
CLI
ENTPROGRAM:
i
mpor
tjav
a.i
o.*
;
i
mpor
tjav
a.r
mi.
*;
publ
i
ccl
asscl
i
ent
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
thr
owsExcept
ion
{
`
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
t
ry
{
St
ri
ngs="
rmi
:
//"
+ar
gs[
0]+"
/abc"
;
ser
ver
intf
=(ser
ver
int
)Nami
ng.
lookup(
s);
Dat
aInput
Str
eam m=newDat
aInput
Str
eam(
Syst
em.
in)
;
i
ntn1=I
nteger
.par
seI
nt(
m.r
eadLi
ne(
));
Sy
stem.
out
.pr
int
ln(
"t
hef
act
ori
ali
s"+f
.f
act
(n1)
);
}
cat
ch(
Except
ione)
{
Sy
stem.
out
.pr
int
ln(
e);
}
}
}
INTERFACEPROGRAM:
i
mpor
tjav
a.r
mi.
*;
publ
i
cint
erf
aceser
ver
intext
endsRemot
e
{
i
ntf
act
(i
ntn)
thr
owsExcept
ion;
}
I
MPLEMENTATI
ONPROGRAM:
i
mpor
tjav
a.r
mi.
*;
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
i
mpor
tj
ava.
rmi
.ser
ver
.*;
publ
iccl
assserv
eri
mpl
ext
endsUni
cast
Remot
eObj
ect
i
mpl
ementsser
ver
int
{
publ
i
cser
ver
impl
()
thr
owsEx
cept
ion
{
}
publ
i
cintf
act
(i
ntn)
{
i
nti
,
c=1;
f
or(
i=1;
i
<=n;
i
++)
{
c=i
*c;
}
r
etur
nc;
}
}
SERVERPROGRAM:
i
mpor
tjav
a.net
.*;
i
mpor
tjav
a.r
mi.
*;
publ
i
ccl
assser
ver
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
{
t
ry
{
ser
ver
impl
m=newser
ver
impl
()
;
Nami
ng.
rebi
nd(
"abc"
,m)
;
}
cat
ch(
Except
ione)
{
Sy
stem.
out
.pr
int
ln(
"Except
ion"
+e)
;
}
}
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Out
put
:
SERVERWI
NDOW:
C:\
vino20>j
avacser
ver
int
.j
ava
C:\
vino20>j
avacser
ver
impl
.j
ava
C:\
vino20>j
avacser
ver
.j
ava
C:\
vino20>r
micser
ver
impl
C:\
vino20>st
artr
mir
egi
str
y
C:\
vino20>j
avaser
ver
CLI
ENTWI
NDOW:
C:\
vino20>j
avaccl
i
ent
.j
ava
Not
e:cl
i
ent
.j
avausesorov
err
idesadepr
ecat
edAPI
.
Not
e:Recompi
l
ewi
th-
Xli
nt:
depr
ecat
ionf
ordet
ail
s.
C:\
vino20>j
avacl
i
entl
ocal
host
3
t
hef
act
ori
ali
s6
C:\
vino20>
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
act
ical
No:
-2Dat
e:-24/
07/
18
GENERATINGPRIMEANDFI
BONACCINUMBERSUSI
NG
MULTITHREADI
NG
i
mpor tjava.i
o.*;
i
mpor tj
ava.util
.*
;
cl
assMy Thread1ext endsThr ead
{
pri
vatePipedReader pr;
pri
vatePipedWr i
terpw;
My Thread1(PipedReader pr,pi
pedWr it
erpw)
{
this.
pr=pr ;
this.
pw=pw;
}
publi
cv oidrun( )
{
try
{
i
ntI ;
for(i
=1;i<10;i
++)
{
i
ntj;
for( j
=2; j
<i;j
++)
{
i
ntn=i %j ;
i
f( n==0)
{
break;
}
}
if(I==j)
{
pw. writ
e(i+”\
n”)
;
}
}
pw.close( )
;
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
cat ch( I
OExcept i
one)
{
}
}
}
classMy Thr ead2ext endsThr ead
{
privatePi pedReader pr;
Priv atePi pedWr i
terpw;
My Thr ead2( PipedReader pr,PipedWr i
terpw)
{
this.pr ;
this.pw=pw;
}
publ icv oidr un( )
{
try
{
i
ntf 1,
f 2=1, f3=1;
for(inti =1; i
<10; i++)
{
pw. wr ite( f
3+” \n” );
f1=f 2;
f2=f 3;
f3=f 1+f 2;
}
cat ch( IOExcept ione)
{
}
}
}
classMul tit
hr eadedPr ogram
{
publ icst ati
cv oidmai n(str
ing[]args)
throwsExcept
ion
{
Array Li stlist1=newAr ray
Listli
st(
);
Array Li stlistlist 2=newAr rayList
li
st(
);
PipedWr i
terpw1=newPi pedWr it
er()
;
PipedReaderpr 1=newPi pedReader (
pw1) ;
My Thr ead1mt 1=newMy Thread1( pr
1,pw1);
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
System. out.
pr i
nt l
n(“PrimeNumber s”);
mt 1.start()
;
i
nti t
em1;
whi l
e((item1=pr 1.r
ead( ))
!=-1)
{
charch1=( (char)i
tem1) ;
System. out.
pr i
nt (
Char acter.t
oString(ch1))
;
l
ist1.add( Char acter.toStri
ng(ch1) );
}
pr1.close( )
;
PipedWr it
erpw2=newpi pedWr iter()
;
PipedReaderpr 2=newPi pedReader (pw2);
My Thr ead2mt 2=newMy Thread2( pr2,
pw2);
System. out.
pr i
nt l
n(“Fibonacci Series”);
mt 2.satrt()
;
i
nti t
em2;
whi l
e( (i
tem2=pr 2.read())!=-
1)
{
charch2=( (char )i
tem2) ;
System. out.
pr i
nt (
Char acter.t
oString(ch2))
;
l
ist2.add( Char acter.toStri
ng(ch2) );
}
pr2.close( )
;
System. out.
pr i
nt l
n(“Element scommont obothl
i
stsar
e:”
);
l
ist1.retainAll(l
ist2);
for(i
nti =0;i
<list1.si
ze( );
i
++)
{
System. out.
pr i
nt (
li
st1.get(i)
);
}
}
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Out
put
:-
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
act
ical
No:
-3(
i) Dat
e:-04/
09/
18
Gi
veJav
asol
uti
ont
oBoundedbuf
ferpr
obl
em.
i
mpor
tjav
a.i
o.*
;
cl
assBuf
fer
{
pr
ivat
efi
nal
intMaxBuf
fSi
ze;
pr
ivat
echar
[]st
ore;
pr
ivat
eintBuf
fer
Star
t,Buf
fer
End,
Buf
fer
Size;
publ
i
cBuf
fer
(i
ntsi
ze)
{
MaxBuf
fSi
ze=si
ze;
Buf
fer
End=-
1;
Buf
fer
Star
t=0;
Buf
fer
Size=0;
st
ore=newchar
[MaxBuf
fSi
ze]
;
}
publ
i
csy
nchr
oni
zedv
oidi
nser
t(charch)
{
t
ry
{
whi
l
e(Buf
fer
Size==MaxBuf
fSi
ze)
{
wai
t(
);
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Buf
fer
End=(
Buf
fer
End+1)%MaxBuf
fSi
ze;
st
ore[
Buf
fer
End]=ch;
Buf
fer
Size++;
not
if
yAl
l
();
}
cat
ch(
Int
err
upt
edExcept
ione)
{
Thr
ead.
cur
rent
Thr
ead(
).
int
err
upt
();
}
}
publ
i
csy
nchr
oni
zedchardel
ete(
)
{
t
ry
{
whi
l
e(Buf
fer
Size==0)
{
wai
t(
);
}
charch=st
ore[
Buf
fer
Star
t]
;
Buf
fer
Star
t=(
Buf
fer
Star
t+1)%MaxBuf
fSi
ze;
Buf
fer
Size-
-;
not
if
yAl
l
();
r
etur
nch;
}
cat
ch(
Int
err
upt
edExcept
ione)
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
{
Thr
ead.
cur
rent
Thr
ead(
).
int
err
upt
();
r
etur
n'i
'
;
}
}
}
cl
assConsumerext
endsThr
ead
{
pr
ivat
efi
nal
Buf
ferbuf
fer
;
publ
i
cConsumer
(Buf
ferb)
{
buf
fer=b;
}
publ
i
cvoi
drun(
)
{
whi
l
e(!
Thr
ead.
cur
rent
Thr
ead(
).
isI
nter
rupt
ed(
))
{
charc=buf
fer
.del
ete(
);
Sy
stem.
out
.pr
int
(c)
;
}
}
}
cl
assPr
oducerext
endsThr
ead
{
pr
ivat
efi
nal
Buf
ferbuf
fer
;
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
pr
ivat
eInput
Str
eamReaderi
n=newI
nput
Str
eamReader
(Sy
stem.
in)
;
publ
i
cPr
oducer
(Buf
ferb)
{
buf
fer=b;
}
publ
i
cvoi
drun(
)
{
t
ry
{
whi
l
e(!
Thr
ead.
cur
rent
Thr
ead(
).
isI
nter
rupt
ed(
))
{
i
ntc=i
n.r
ead(
);
i
f(c==-
1)br
eak;
//-
1iseof
buf
fer
.i
nser
t((
char
)c)
;
}
}
cat
ch(
IOExcept
ione)
{}
}
}
cl
assBoundedBuf
fer
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs)
{
Sy
stem.
out
.pr
int
ln(
"pr
ogr
am st
art
ing"
);
Buf
ferbuf
fer=newBuf
fer
(5)
;//buf
ferhassi
ze5
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
oducerpr
od=newPr
oducer
(buf
fer
);
Consumercons=newConsumer
(buf
fer
);
pr
od.
star
t()
;
cons.
star
t()
;
t
ry
{
pr
od.
joi
n()
;
cons.
int
err
upt
();
}
cat
ch(
Int
err
upt
edExcept
ione){
}
Sy
stem.
out
.pr
int
ln(
"EndofPr
ogr
am"
);
}
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Out
put:
-
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
act
ical
:
-3 Dat
e:- \ \
2018
Sy
nchroni
zati
on:
(i
i
)Gi v
esolut
iontot
her
eader
s–writer
sprobl
em usi
ngJav
a
synchroni
zat
ion.
/
/Dat
abase
publ
i
ccl
assDat
abase
{
pr
ivat
eintr
eader
s;
publ
i
cDat
abase(
)
{
t
his.
reader
s=0;
}
publ
i
cvoi
dread(
intnumber
)
{
sy
nchr
oni
zed(
thi
s)
{
t
his.
reader
s++;
Sy
stem.
out
.pr
int
ln(
"Reader
"+number
+"St
atusr
eadi
ng:
");
}
f
inal
i
ntDELAY=5000;
t
ry
{
Thr
ead.
sleep(
(i
nt)
(Mat
h.r
andom(
)*DELAY)
);
}
cat
ch(
Int
err
upt
edExcept
ione)
{}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
sy
nchr
oni
zed(
thi
s)
{
Sy
stem.
out
.pr
int
ln(
"Reader
"+number
+"St
opsr
eadi
ng:
");
t
his.
reader
s--
;
i
f(
thi
s.r
eader
s==0)
{
t
his.
not
if
yAl
l
();
}
}
}
publ
i
csy
nchr
oni
zedv
oidwr
it
e(i
ntnumber
)
{
whi
l
e(t
his.
reader
s!=0)
{
t
ry
{
t
his.
wai
t(
);
}
cat
ch(
Int
err
upt
edExcept
ione)
{}
}
Sy
stem.
out
.pr
int
ln(
"Wr
it
er"
+number
+"St
art
swr
it
ing.
")
;
f
inal
i
ntDELAY=5000;
t
ry
{
Thr
ead.
sleep(
(i
nt)
(Mat
h.r
andom(
)*DELAY)
);
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
}
cat
ch(
Int
err
upt
edExcept
ione)
{}
Sy
stem.
out
.pr
int
ln(
"Wr
it
er"
+number
+"st
opswr
it
ing.
")
;
t
his.
not
if
yAl
l
();
}
}
/
/reader
publ
i
ccl
assReaderext
endsThr
ead
{
pr
ivat
est
ati
cintr
eader
s=0;
pr
ivat
eintnumber
;
pr
ivat
eDat
abasedat
abase;
publ
i
cReader
(Dat
abasedat
abase)
{
t
his.
dat
abase=dat
abase;
t
his.
number
=Reader
.r
eader
s++;
}
publ
i
cvoi
drun(
)
{
whi
l
e(t
rue)
{
f
inal
i
ntDELAY=5000;
t
ry{
Thr
ead.
sleep(
(i
nt)
(Mat
h.r
andom(
)*DELAY)
);
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
cat
ch(
Int
err
upt
edExcept
ione)
{}
t
his.
dat
abase.
read(
thi
s.number
);
}
}
}
/
/wr
it
er
publ
i
ccl
assWr
it
erext
endsThr
ead
{
pr
ivat
est
ati
cintwr
it
ers=0;
pr
ivat
eintnumber
;
pr
ivat
eDat
abasedat
abase;
publ
i
cWr
it
er(
Dat
abasedat
abase)
{
t
his.
dat
abase=dat
abase;
t
his.
number
=Wr
it
er.
wri
ter
s++;
}
publ
i
cvoi
drun(
)
{
whi
l
e(t
rue)
{
f
inal
i
ntDELAY=5000;
t
ry
{
Thr
ead.
sleep(
(i
nt)
(Mat
h.r
andom(
)*DELAY)
);
}
cat
ch(
Int
err
upt
edExcept
ione)
{}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
t
his.
dat
abase.
wri
te(
thi
s.number
);
}
}
}
publ
i
ccl
asssi
mul
ator
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs)
{
i
f(
args.
lengt
h<2)
{
Sy
stem.out.
pri
ntl
n("
Usagej
avasumul
ator
<numberofr
eader
s><numberof
wri
ter
s>")
;
}
el
se{
f
inal
i
ntREADERS=I
nteger
.par
seI
nt(
args[
0])
;
f
inal
i
ntWRI
TERS=I
nteger
.par
seI
nt(
args[
1])
;
Dat
abasedat
abase=newDat
abase(
);
f
or(
inti
=0;
i
<READERS;
i
++)
{
newReader
(dat
abase)
.st
art
();
}
f
or(
inti
=0;
i
<WRI
TERS;
i
++)
{
newWr
it
er(
dat
abase)
.st
art
();
}
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
}
}
Out
put
:-
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
act
ical
No:
- 3 Sy
nchr
oni
zat
ionDat
e:- - -
18
/
/Sy
nchr
oni
zed
cl
assr
oom
{
sy
nchr
oni
zedv
oidt
akesl
ect
ure(
Str
ingname)t
hrowsI
nter
rupt
edExcept
ion
{
Sy
stem.
out
.pr
int
ln(
name+"
ent
ers.
."
);
Thr
ead.
sleep(
250)
;
Sy
stem.
out
.pr
int
ln(
name+"
star
tlect
ure.
."
);
Thr
ead.
sleep(
500)
;
Sy
stem.
out
.pr
int
ln(
name+"
exi
ts"
);
Thr
ead.
sleep(
250)
;
}
}
cl
assl
ect
urei
mpl
ement
sRunnabl
e
{
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
St
ri
ngname;
r
oom r
;
Thr
eadt
;
l
ect
ure(
Str
ingn,
room r
)
{
name=n;
t
=newThr
ead(
thi
s,
n);
t
his.
r=r
;
}
publ
i
cvoi
dst
art
()
{
t
.st
art
();
}
publ
i
cvoi
drun(
)
{
t
ry
{
r
.t
akesl
ect
ure(
name)
;
}
cat
ch(
Int
err
upt
edExcept
ioni
ae)
{
Sy
stem.
out
.pr
int
ln(
name+"
oper
ator
int
err
upt
rd.
..
")
;
}
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
cl
assos2
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
r
oom r=newr
oom(
);
l
ect
urekaml
akar
=newl
ect
ure(
"kaml
akar
sir
",
r)
;
l
ect
ureswapni
l
=newl
ect
ure(
"swapni
l
sir
",
r)
;
l
ect
urer
am=newl
ect
ure(
"r
am si
r"
,r
);
kaml
akar
.st
art
();
swapni
l
.st
art
();
r
am.
star
t()
;
}
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Out
put
:-
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
act
ical
No.4 Dat
e:-
I
mpl
ementFCFSschedul
i
ngal
gor
it
hm i
nJav
a.
i
mpor
tj
ava.
uti
l
.Scanner
;
cl
asspr
ocess
{
i
ntwai
t;
i
ntsubmi
ssi
on;
i
ntbur
sts;
i
ntt
urnAr
ound;
i
ntCompl
eti
onTi
me=0;
pr
ocess(
intsub,
intbur
)
{
submi
ssi
on=sub;
bur
sts=bur
;
}
}
cl
asspr
ocessmai
n
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
i
ntwai
t=0,
x=1;
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Scanners=newScanner
(Sy
stem.
in)
;
Sy
stem.
out
.pr
int
ln(
"ent
ert
henumberofpr
ocess"
);
i
ntn=s.
next
Int
();
pr
ocess[
]My
process=newpr
ocess[
n];
f
or(
inti
=0;
i<n;
i++)
{
Sy
stem.
out
.pr
int
ln(
"ent
erar
ri
val
ti
meandbur
sts"
);
i
ntsub=s.
next
Int
();
i
ntbur
=s.
next
Int
();
My
process[
i]=newpr
ocess(
sub,
bur
);
f
or(
i=0;
i<My
process.
lengt
h;i
++)
{
x=x-
Mypr
ocess[
i]
.bur
sts;
My
process[
i]
.Compl
eti
onTi
me=x;
My
process[
i]
.tur
nAr
ound=My
process[
i]
.Compl
eti
onTi
me-
My
process[
i]
.submi
ssi
on;
My
process[
i]
.wai
t=My
process[
i]
.t
urnAr
ound-
Mypr
ocess[
i]
.bur
sts;
Sy
stem.
out
.pr
int
ln(
"pr
ocess"
+i+"
:"
);
Sy
stem.
out
.pr
int
ln(
"t
urnar
ound\
tcompl
eti
on\
twat
ini
ng"
);
Syst
em.
out.
pri
ntl
n(Mypr
ocess[
i]
.t
urnAr
ound+"
\t\
t\t
"+My
process[
i]
.Compl
eti
onTi
me+"
\t\
t\t
"+Mypr
ocess[
i]
.wai
t);
}
}
}
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Out
put
:-
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
act
ical
:-6 Dat
e:-/ /
2018
I
mpementRRschedul
i
ngal
gor
it
hm i
njav
a
Sy
ntax:
i
mpor
tjav
a.i
o.*
;
cl
assj
obi
mpl
ement
sRunnabl
e
{
i
ntpr
ocess_
id,
no_
of_
inst
r,t
ime_
quant
um;
Thr
eadt
;
j
ob(
intpi
d,i
nti
nst
r,
intt
q)
{
pr
ocess_
id=pi
d;
no_
of_
inst
r=i
nst
r;
t
ime_
quant
um=t
q;
t
=newThr
ead(
thi
s);
t
.st
art
();
}
publ
i
cvoi
drun(
)
{
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
t
ry
{
f
or(
inti
=1;
i
<=no_
of_
inst
r;
i++)
{
Sy st
em.out.
pri
ntl
n("
Execut
ingi
nst
rno."
+i+"of
pr
ocess"+pr
ocess_i
d);
Thr
ead.
sleep(
ti
me_
quant
um)
;
}
Sy
stem.
out
.pr
int
ln(
"j
oin"
+pr
ocess_
id+"i
sov
er"
);
}
cat
ch(
Int
err
upt
edExcept
ione)
{
Sy
stem.
out
.pr
int
ln(
"Thej
obhasbeeni
ntur
rept
ed.
..
")
;
}
}
}
cl
assos1
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
t
ry
{
i
ntpr
ocess_
id=100,
ti
me_
quant
um=100;
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Buf
feredReaderbr
=newBuff
eredReader
(new
I
nputStr
eamReader(Sy
stem.
in)
);
Sy
stem.
out
.pr
int
ln(
"Ent
erapr
ocessuserst
art
ingnumber
:"
);
pr
ocess_
id=I
nteger
.par
seI
nt(
br.
readLi
ne(
));
Sy
stem.
out
.pr
int
ln(
"Ent
erat
ime_
quant
um(
inmi
l
ls)
:"
);
t
ime_
quant
um=I
nteger
.par
seI
nt(
br.
readLi
ne(
));
j
obj
1=newj
ob(
++pr
ocess_
id,
10,
ti
me_
quant
um)
;
j
obj
2=newj
ob(
++pr
ocess_
id,
6,
ti
me_
quant
um)
;
j
obj
3=newj
ob(
++pr
ocess_
id,
8,
ti
me_
quant
um)
;
}
cat
ch(
Except
ione)
{
System.out
.pr
int
ln(
"Somepr
ocessf
ail
edt
o
complete.
..
")
;
Sy
stem.
out
.pr
int
ln(
"Pl
easecont
actsy
stem admi
n..
."
);
}
}
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Out
put
:
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
act
ical
No:
-7 Dat
e:- / /
2018
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Wr
it
eaJav
apr
ogr
am t
hati
mpl
ement
sthebanker
’sal
gor
it
hm
i
mpor
tjav
a.i
o.*
;
cl
assBanker
{
i
ntav
ail
[];
i
ntmax
[][]={{
3,2,
2,
1},
{8,
12,
0,
0},
{2,
1,
0,
0},
{4,
3,
0,
0},
{2,
0,
3,
1}}
;
i
ntal
l
oc[]
[]={{
1,1,
1,
0},
{2,
1,
0,
0},
{1,
0,
0,
0},
{2,
1,
0,
0},
{1,
0,
0,
0}}
;
i
ntneed[]
[];
i
ntm,
n;
Banker
()
{
m =4;
n=5;
av
ail
=newi
nt[
4];
av
ail
[0]=16;
//NumberofRegi
ster
s
av
ail
[1]=50;
//NumberofFi
l
es
av
ail
[2]=5;
//NumberofPor
ts
av
ail
[3]=2;
//NumberofPr
int
er
need=newi
nt[
5][
4];
f
or(
inti
=0;
i
<5;
i++)
f
or(
intj
=0;
j
<4;
j++)
need[
i]
[j
]=max[
i]
[j
]-
all
oc[
i]
[j
];
}
bool
eani
sSaf
e()
{
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
i
ntwor
k[]=newi
nt[
m];
bool
eanf
ini
sh[
]=newbool
ean[
n];
f
or(
inti
=0;
i
<m;
i++)
wor
k[i
]=av
ail
[i
];
f
or(
inti
=0;
i
<n;
i++)
f
ini
sh[
i]=f
alse;
f
or(
inti
=0;
i<n;
i++)
{
i
f(
fi
nish[
i]
==f
alse)
{
bool
eani
ncompl
ete=f
alse;
f
or(
intj
=0;
j
<m;
j++)
{
i
f(
need[
i]
[j
]>wor
k[j
])
{
i
ncompl
ete=t
rue;
/
/Sy
stem.
out
.pr
int
ln(
i+"
:"+j
+":
"+need[
i]
[j
]+"
:"+wor
k[j
])
;
}
i
f(
need[
i]
[j
]!
=0&&need[
i]
[j
]<=wor
k[j
])
{
wor
k[j
]=wor
k[j
]-need[
i]
[j
];
}
}
i
f(
!i
ncompl
ete)
{
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
f
ini
sh[
i]
=tr
ue;
}
i
f(
fi
nish[
i]
)
Sy
stem.
out
.pr
int
ln(
"Pr
ocess"+i
+"canbecompl
eted"
);
el
se
Sy
stem.
out
.pr
int
ln(
"Pr
ocess"+i
+"can'
tbecompl
eted"
);
}
}
f
or(
inti
=0;
i<n;
i++)
{
i
f(
!f
ini
sh[
i]
)
{
r
etur
nfal
se;
}
}
r
etur
ntr
ue;
}
v
oidr
esour
ceRequest
()
{
i
ntr
equest
[]
[]={{
1,1,
1,
0},
{2,
2,
0,
0},
{1,
1,
0,
1},
{1,
1,
0,
0},
{1,
0,
0,
0}}
;
bool
eansaf
e=t
rue;
f
or(
inti
=0;
i<n;
i++)
{
f
or(
intj
=0;
j
<m;
j++)
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
{
i
f(
request
[i
][
j]
<=need[
i]
[j
])
{
i
f(
request
[i
][
j]
<=av
ail
[j
])
{
av
ail
[j
]=av
ail
[j
]-
request
[i
][
j]
;
al
l
oc[
i]
[j
]=al
l
oc[
i]
[j
]+r
equest
[i
][
j]
;
need[
i]
[j
]=need[
i]
[j
]-
request
[i
][
j]
;
}
el
se
{
Syst
em.out.
pri
ntl
n("
Heyprocess"+i
+"!r
esour
ce"+j
+"i
snotav
ail
abl
e
now..
.t
ryagai
nlater
..
."
);
}
}
el
se
{
Sy
stem.out.
pri
ntl
n("
Process"+i
+"hasexceededr
equestf
orr
esour
ce"+j
+"henceisunsafe"
);
saf
e=f
alse;
}
}
}
i
f(
saf
e)
{
Sy
stem.
out
.pr
int
ln(
"t
hesy
stem i
ssaf
e..
."
);
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
}
el
se
{
Sy
stem.
out
.pr
int
ln(
"t
hesy
stem i
sunsaf
e..
."
);
}
}
}
cl
assos5
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])t
hrowsExcept
ion
{
Bankerb=newBanker
();
Sy
stem.
out
.pr
int
ln(
"Appl
yi
ngsaf
etyal
gor
it
hm.
..
")
;
i
f(
b.i
sSaf
e()
)
{
Sy
stem.
out
.pr
int
ln(
"Thesy
stem i
sinsaf
est
ate.
..
")
;
}
el
se
{
Sy
stem.
out
.pr
int
ln(
"Thesy
stem i
snoti
nsaf
est
ate.
..
."
);
}
Sy
stem.
out
.pr
int
ln(
"Appl
yi
ngr
esour
cer
equestal
go.
..
")
;
b.
resour
ceRequest
();
}
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Out
put
:-
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
act
ical
No:
-8 Dat
e:- - -
18
Wri
teaJavapr
ogr
am t
hati
mpl
ement
stheFI
FOpage-
repl
acement
al
gori
thm.
/
/page-
repl
acemental
gor
it
hm.
i
mpor
tjav
a.i
o.*
;
cl
assMemMgmt
{
St
ri
ngf
rame_
sequence;
i
ntmem_
block[
];
MemMgmt
(St
ri
ngf
s,i
ntn)
{
f
rame_
sequence=f
s;
mem_
block=newi
nt[
n];
f
or(
inti
=0;
i
<n;
i++)
mem_
block[
i]
=-1;
}
v
oiddi
spMemBl
ock(
)
{
Sy
stem.
out
.pr
int
("
|"
);
f
or(
inti
=0;
i
<mem_
block.
lengt
h;i
++)
{
Sy
stem.
out
.pr
int
(mem_
block[
i]
+"|
")
;
}
Sy
stem.
out
.pr
int
ln(
);
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
v
oidf
cfs(
)
{
Sy
stem.
out
.pr
int
ln(
"=======FI
FO==========="
);
St
ri
ngst
rpages[
]=f
rame_
sequence.
spl
i
t(""
);
i
ntpages[
]=newi
nt[
str
pages.
lengt
h];
i
nti
=0;
f
or(
i=0;
i<st
rpages.
lengt
h;i
++)
pages[
i]=I
nteger
.par
seI
nt(
str
pages[
i]
);
i
ntmem_
block_
num=0,
page_
faul
ts=0;
Sy
stem.
out
.pr
int
ln(
"I
nit
ial
Memor
ylay
out
..
."
);
di
spMemBl
ock(
);
f
or(
i=0;
i<mem_
block.
lengt
h;i
++)
{
bool
eanpr
esent
=fal
se;
f
or(
intj
=0;
j
<mem_
block.
lengt
h;j
++)
{
i
f(
mem_
block[
j]==pages[
i]
)
{
pr
esent
=tr
ue;
br
eak;
}
}
i
f(
!pr
esent
)
{
mem_
block[
mem_
block_
num]=pages[
i]
;
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
mem_
block_
num++;
page_
faul
ts++;
}
i
f(
mem_
block_
num==mem_
block.
lengt
h)
{
mem_
block_
num=0;
Sy
stem.
out
.pr
int
ln(
"Loadi
ngpageno.
"+(
i+1)+"
:"+pages[
i]
);
di
spMemBl
ock(
);
}
}
Sy
stem.
out
.pr
int
ln(
"Tot
alnumberofpagef
aul
ts:
"+page_
faul
ts)
;
}
}
cl
assos9a
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])t
hrowsExcept
ion
{
Buf
feredReader
br=newBuf
fer
edReader
(new
I
nputStr
eamReader(
Syst
em.
in)
);
Sy
stem.
out
.pr
int
ln(
"Ent
erpagef
ramesequence(
separ
atedbyspace)
:"
);
St
ri
ngf
rame_
sequence="
70120304230321201701"
;
MemMgmtm =newMemMgmt
(fr
ame_
sequence,
3);
m.
fcf
s()
;
}
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Out
put
:-
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
act
ical
No:
-9 Dat
e:- - -
18
Writ
eaJavapr
ogr
am t
hati
mpl
ement
stheLRUpage-
repl
acement
al
gori
thm.
i
mpor
tjav
a.i
o.*
;
cl
assMemMgmt
{
St
ri
ngf
rame_
sequence;
i
ntmem_
block[
];
MemMgmt
(St
ri
ngf
s,i
ntn)
{
f
rame_
sequence=f
s;
mem_
block=newi
nt[
n];
f
or(
inti
=0;
i
<n;
i++)
mem_
block[
i]
=-1;
}
v
oiddi
spMemBl
ock(
)
{
Sy
stem.
out
.pr
int
("
|"
);
f
or(
inti
=0;
i
<mem_
block.
lengt
h;i
++)
{
Sy
stem.
out
.pr
int
(mem_
block[
i]
+"|
")
;
}
Sy
stem.
out
.pr
int
ln(
);
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
v
oidoppr
()
{
Sy
stem.
out
.pr
int
ln(
"=======Opt
imal
PageRepl
acement==========="
);
St
ri
ngst
rpages[
]=f
rame_
sequence.
spl
i
t(""
);
i
ntpages[
]=newi
nt[
str
pages.
lengt
h];
i
nti
=0;
f
or(
i=0;
i<st
rpages.
lengt
h;i
++)
pages[
i]=I
nteger
.par
seI
nt(
str
pages[
i]
);
i
ntmem_
block_
num=0,
page_
faul
ts=0;
Sy
stem.
out
.pr
int
ln(
"I
nit
ial
Memor
ylay
out
..
."
);
di
spMemBl
ock(
);
f
or(
i=0;
i<mem_
block.
lengt
h;i
++)
{
bool
eanpr
esent
=fal
se;
f
or(
intj
=0;
j
<mem_
block.
lengt
h;j
++)
{
i
f(
mem_
block[
j]==pages[
i]
)
{
pr
esent
=tr
ue;
br
eak;
}
}
i
f(
!pr
esent
)
{
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
mem_
block[
mem_
block_
num]=pages[
i]
;
mem_
block_
num++;
page_
faul
ts++;
}
Sy
stem.
out
.pr
int
ln(
"Loadi
ngpageno.
"+(
i+1)+"
:"+pages[
i]
);
di
spMemBl
ock(
);
}
f
or(
;i<pages.
lengt
h;i
++)
{
bool
eanpr
esent
=fal
se;
f
or(
intj
=0;
j
<mem_
block.
lengt
h;j
++)
{
i
f(
mem_
block[
j]==pages[
i]
)
{
pr
esent
=tr
ue;
br
eak;
}
}
i
f(
!pr
esent
)
{
mem_
block_
num=-
1;
i
ntl
ongest
_page=-
1;
f
or(
intj
=0;
j
<mem_
block.
lengt
h;j
++)
{
i
ntk=0;
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
f
or(
k=i
+1;
k<pages.
lengt
h;k++)
{
i
f(
mem_
block[
j]==pages[
k])
{
i
f(
k>l
ongest
_page)
{
l
ongest
_page=k;
mem_
block_
num =j
;
}
br
eak;
}
}
i
f(
k==pages.
lengt
h)
{
l
ongest
_page=pages.
lengt
h;
mem_
block_
num =j
;
}
}
mem_
block[
mem_
block_
num]=pages[
i]
;
page_
faul
ts++;
}
Sy
stem.
out
.pr
int
ln(
"Loadi
ngpageno.
"+(
i+1)+"
:"+pages[
i]
);
di
spMemBl
ock(
);
}
Sy
stem.
out
.pr
int
ln(
"Tot
alNumberofpagef
aul
ts:
"+page_
faul
ts)
;
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
}
}
cl
assos8
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])t
hrowsExcept
ion
{
Buf
feredReader
br=newBuf
fer
edReader
(new
I
nputStr
eamReader(
Syst
em.
in)
);
Sy
stem.
out
.pr
int
ln(
"Ent
erpagef
ramesequence(
separ
atedbyspace)
:"
);
St
ri
ngf
rame_
sequence="
70120304230321201701"
;
MemMgmtm =newMemMgmt
(fr
ame_
sequence,
3);
m.
oppr
();
}
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Out
put
:-
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Pr
act
ical
No:
-10 Dat
e:- - -
18
Desi
gnaFi
l
eSy
stem i
nJav
a.
/
/ReadFi
l
e
i
mpor
tjav
a.i
o.*
;
i
mpor
tj
ava.
uti
l
.Scanner
;
publ
i
ccl
assReadf
il
e
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
St
ri
ngFi
l
eName;
Scannersc=newScanner
(Sy
stem.
in)
;
Sy
stem.
out
.pr
int
ln(
"Ent
ert
hef
il
ename"
);
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Fi
l
eName=sc.
next
Line(
);
t
ry
{
Fi
l
ef1=newFi
l
e(Fi
l
eName)
;
i
f(
f1.
exi
sts(
)==f
alse)
{
Sy
stem.
out
.pr
int
ln(
"fi
l
edoesnotnotexi
ts"
);
Sy
stem.
exi
t(
0);
}
St
ri
ngt
ext
;
i
ntv
al;
Fi
l
eInput
Str
eamf
il
eIn=newFi
l
eInput
Str
eam(
f1)
;
Sy
stem.
out
.pr
int
ln(
"cont
entoff
il
eis:
")
;
whi
l
e((
val
=fi
l
eIn.
read(
))!
=-1)
{
Sy
stem.
out
.pr
int
((char
)val
);
}
Sy
stem.
out
.pr
int
ln(
);
f
il
eIn.
close(
);
}
cat
ch(
Except
ione)
{
Sy
stem.
out
.pr
int
ln(
e);
}
}
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Out
put
:-
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
/
/Wr
it
eFi
l
e
i
mpor
tjav
a.i
o.*
;
i
mpor
tj
ava.
uti
l
.Scanner
;
publ
i
ccl
assWr
it
eFi
l
e
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs)
{
St
ri
ngFi
l
eName;
Scannersc=newScanner
(Sy
stem.
in)
;
Sy
stem.
out
.pr
int
ln(
"Ent
ert
henameoft
hef
il
eyouwantt
oread"
);
Fi
l
eName=sc.
next
Line(
);
t
ry
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
{
Fi
l
ef1=newFi
l
e(Fi
l
eName)
;
i
f(
f1.
exi
sts(
)==f
alse)
{
i
f(
f1.
creat
eNewFi
l
e()
)
{
Sy
stem.
out
.pr
int
ln(
"Fi
l
eCr
eat
edSuccessf
ull
y")
;
}
el
se
{
Sy
stem.
out
.pr
int
ln(
"Fi
l
ecr
eat
ionf
ail
ed"
);
Sy
stem.
exi
t(
0);
}
}
St
ri
ngt
ext
;
Fi
l
eOut
put
Str
eamf
il
eout
=newFi
l
eOut
put
Str
eam(
f1)
;
i
ntch;
whi
l
e(t
rue)
{
Sy
stem.
out
.pr
int
ln(
"Ent
ert
extt
owr
it
etot
hef
il
e&pr
ess0t
ost
opwr
it
ing"
);
t
ext
=sc.
next
Line(
);
i
f(
tex
t.equal
s("
0")
)
br
eak;
el
se
{
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
f
il
eout
.wr
it
e(t
ext
.get
Byt
es(
));
f
il
eout
.f
lush(
);
}
}
f
il
eout
.cl
ose(
);
Sy
stem.
out
.pr
int
ln(
"Fi
l
eSav
ed"
);
}
cat
ch(
Except
ionex)
{
Sy
stem.
out
.pr
int
ln(
"Except
ion"
+ex.
toSt
ri
ng(
));
}
}
}
Out
put
:-
Sub:
-Oper
ati
ngSy
stem
SYB.
Sc[
Comput
erSci
ence] Rol
lNo:
-1852009
Sub:
-Oper
ati
ngSy
stem