OS Lab2012

download OS Lab2012

of 122

Transcript of OS Lab2012

  • 7/21/2019 OS Lab2012

    1/122

    UNIPROCESSOR OPERATING

    SYSTEMS

    1

  • 7/21/2019 OS Lab2012

    2/122

    Ex. No.: 1 CPU SCHEDULING

    DATE: 9 2 2012

    Aim:To write a java program to implement various C! s"#e$uling algorit#ms% su"# as &C&'%

    '(& )reemptive * non+preemptive,% -oun$-oin% an$ riorit/ )reemptive * non+preemptive,

    Algorithm:

    Step 1: et t#e numer o pro"esses rom user.

    Step 2:n main un"tion get t#e "#oi"e

    i. t#e "#oi"e or &C&'%

    i. et t#e C! urst time or ea"# pro"ess.ii. '"#e$ule t#e pro"esses ase$ on &&3 manner.

    iii. Cal"ulate t#e waiting time o all t#e pro"esses an$ t#e average

    waiting time.

    iv. Cal"ulate t#e turn aroun$ time o all t#e pro"esses an$ t#e average

    turn aroun$ time.

    v. Displa/ t#e antt "#art.

    ii. t#e "#oi"e or '(& )Non+reemptive,

    i. et t#e C! urst time or ea"# pro"ess.

    ii. '"#e$ule t#e pro"esses ase$ on s#ortest urst time.

    iii. Cal"ulate t#e waiting time o all t#e pro"esses an$ t#e average

    waiting time.

    iv. Cal"ulate t#e turn aroun$ time o all t#e pro"esses an$ t#e average

    turn aroun$ time.

    v. Displa/ t#e antt C#art.

    iii. t#e "#oi"e or '(& )reemptive,

    i. et t#e C! urst time an$ arrival time or ea"# pro"ess.

    ii. '"#e$ule t#e pro"esses ase$ on s#ortest urst time an$ arrival time.

    iii. Cal"ulate t#e waiting time o all t#e pro"esses an$ t#e average

    waiting time.

    2

  • 7/21/2019 OS Lab2012

    3/122

    iv. Cal"ulate t#e turn aroun$ time o all t#e pro"esses an$ t#e average

    turn aroun$ time.

    v. Displa/ t#e antt C#art.

    iv. t#e "#oi"e or -oun$ -oin

    i. et t#e C! urst time or ea"# pro"ess an$ time 4uantum.

    ii. '"#e$ule t#e pro"esses ase$ on t#e time 4uantum in &&3 manner.

    a. reempt a pro"ess i $oes not inis# exe"ution wit#in t#e time

    4uantum

    . -es"#e$ule t#e preempte$ pro"esses again ater s"#e$uling

    all t#e pro"esses on"e.

    ". -epeat t#e aove two steps until all t#e pro"esses inis# t#eir

    exe"ution.

    iii. Cal"ulate t#e waiting time o all t#e pro"esses an$ t#e average

    waiting time.

    iv. Cal"ulate t#e turn aroun$ time o all t#e pro"esses an$ t#e average

    turn aroun$ time.

    v. Displa/ t#e antt "#art.

    )e, t#e "#oi"e orriorit/ )Non+reemptive,

    i. et t#e C! urst time an$ riorit/ or ea"# pro"ess

    ii. '"#e$ule t#e pro"esses ase$ on t#eir riorit/ )Non+reemptive,

    lower priorit/ value in$i"ates #ig#er priorit/ or exe"ution.

    iii. Cal"ulate t#e waiting time o all t#e pro"esses an$ t#e average

    waiting time.

    iv. Cal"ulate t#e turn aroun$ time o all t#e pro"esses an$ t#e average

    turn aroun$ time.

    v. Displa/ t#e antt C#art.

    ), t#e "#oi"e orriorit/ )reemptive,

    i. et t#e C! urst time% arrival time an$ riorit/ or ea"#

    pro"ess.

    5

  • 7/21/2019 OS Lab2012

    4/122

    ii. '"#e$ule t#e pro"esses ase$ on t#eir arrival time an$ riorit/

    )reemptive, lower priorit/ value in$i"ates #ig#er priorit/

    or exe"ution.

    iii. Cal"ulate t#e waiting time o all t#e pro"esses an$ t#e average

    waiting time.

    iv. Cal"ulate t#e turn aroun$ time o all t#e pro"esses an$ t#e

    average turn aroun$ time.

    v. Displa/ t#e antt "#art.

    Step !: Exe"ute t#e program an$ $ispla/ t#e result.

    Step ":En$ t#e rogram

    PROGRAM:

    67 re"tangle.java 76

    import java.applet.78

    import java.awt.78

    import java.io.78

    import java.util.78

    import java.lang.78

    67applet "o$e;re"tangle; #eig#t

    6applet>

    76

    puli" "lass re"tangle?ol$ exten$s Applet

    @

    int apw1B8

    int pB8

    int i0%n%n18

    rap#i"s g getrap#i"s),8

    puli" voi$ init),

    @

    tr/

  • 7/21/2019 OS Lab2012

    5/122

    @

    uere$-ea$er ojnew uere$-ea$er)new nput'tream-ea$er

    )'/stem.in,,8

    '/stem.out.println);ENTE- no. o pro"esses:;,8

    nnteger.parsent)oj.rea$ine),,8

    int "#8

    int tBnew intnB8

    $o @

    '/stem.out.println);FEN! &3- C! 'CGED!N;,8

    '/stem.out.println);1.&C&' Hn 2.'G3-TE'T (3 &-'T )non+

    preemptive, Hn 5. 'G3-TE'T (3 &-'T )preemptive, Hn

    . -3!ND -3N Hn

  • 7/21/2019 OS Lab2012

    6/122

  • 7/21/2019 OS Lab2012

    7/122

  • 7/21/2019 OS Lab2012

    8/122

    apw1iL1BsjiBLapw1iB8

    or) i08in8iLL,

    @

    '/stem.out.println);in$ivi$ual waiting time or pro"ess p;LpiB

    L;is;Lapw1iBL; ;,8

    M

    or) i08in8iLL,

    tLapw1iB8

    loat avgt6n8

    '/stem.out.println);average waiting time is:;Lavg,8

    or) i08in8iLL,

    @

    int piB8

    turniBt+1BLapw1iB8

    '/stem.out.println);turnaroun$ time or pro"ess p;LpiB

    L;is;LturniBL; ;,8

    M

    or) i08in8iLL,

    tuLturniB8

    loat avg1tu6n8

    '/stem.out.println);average turn+aroun$ time is:;Lavg1,8

    paint)g,8

    M

    rea8

    "ase 5:

    @

    int total0%0%"o0%small999%sp0%sp10%x0%"ount08

    int proBBnew intnB5B8

    int wtBnew intnB8

    int wt1Bnew intnB8

    int t1Bnew intnB8

    O

  • 7/21/2019 OS Lab2012

    9/122

    int ttBnew intnB8

    apw1new int 20B8

    pnew int20B8

    or)int i08in8iLL,

    @

    proiB0Bi8

    '/stem.out.print);urst Time or ;LiL; :;,8

    proiB1Bnteger.parsent)oj.rea$ine),,8

    totalLproiB1B8

    '/stem.out.print);Arrival Time or ;LiL; :;,8

    proiB2Bnteger.parsent)oj.rea$ine),,8

    M

    or)int i08in8iLL,

    wt1iBproiB2B8

    or)int i08in8iLL,

    t1iBproiB1B8

    or)int i08in8iLL,

    or)int jiL18jn8jLL,

    i)proiB2B>projB2B,

    @

    int tempBproiB8

    proiBprojB8

    projBtemp8

    M

    or)int i18itotal8iLL,

    @

    small9998

    or)int j"o8jn8jLL,

    i)>projB2B,

    "oLL8

    or)int j08j"o8jLL,

    9

  • 7/21/2019 OS Lab2012

    10/122

    @

    i)small>projB1B**projB1BP0,

    @

    smallprojB1B8

    spprojB0B8

    sp1j8

    M

    M

    i)pxBsp,

    @

    apw1xL1BLL8

    M

    else

    @

    xLL8

    pxBsp8

    apw1xL1Bapw1xB8

    apw1xL1BLL8

    "ountLL8

    M

    prosp1B1B+18

    i)prosp1B1B0,

    ttsp1Bi8

    or)int j08jn8jLL,

    @

    i)projB1BP0**jPsp,

    wtjBL18

    M

    LL8

    M

    10

  • 7/21/2019 OS Lab2012

    11/122

    or)int i08in8iLL,

    or)int jiL18jn8jLL,

    i)proiB0B>projB0B,

    @

    int tempBproiB8

    '/stem.out.println),8

    or)int m08m"ount8mLL,

    pmBL18

    $oule awt0.0%att0.08

    '/stem.out.println),8

    or )int i 08 in8iLL,

    @

    '/stem.out.println);waiting time or pro"ess p

    ;LiL; ;L)wtiB+wt1iB,,8

    awtL)wtiB+wt1iB,8

    attL)wtiB+wt1iBLt1iB,8

    M

    or )int i 08 in8iLL,

    @

    '/stem.out.println);turnaroun$ time or

    pro"ess p;LiL; ;L)wtiB+wt1iBLt1iB,,8

    M

    '/stem.out.println);Average waiting time : ;L

    )awt6n,,8

    '/stem.out.println);Average turnaroun$ time : ;L

    )att6n,,8

    n1"ountL18

    paint)g,8

    M

    rea8

    "ase :

    11

  • 7/21/2019 OS Lab2012

    12/122

    @

    or) i08in8iLL,

    @

    '/stem.out.println);ENTE- urst time or ea"#

    pro"ess: p;L)iL1,,8

    tiBnteger.parsent)oj.rea$ine),,8

    M

    int roBnew intnB8

    apw1new int20B8

    apw10B08

    int s4Bnew int20B8

    int temp8

    loat t08

    loat tu08

    int apwBnew intnB8

    int ttime08

    int turnBnew intnB8

    int 08

    or)i08in8iLL,

    @

    roiBtiB8

    M

    pnew int50B8

    '/stem.out.println);enter time 4uantum;,8

    int tsnteger.parsent)oj.rea$ine),,8

    int 08

    s4B08

    i08

    tempts8

    int "ount08

    $o

    12

  • 7/21/2019 OS Lab2012

    13/122

    @

    i)roiB0,

    @

    iLL8

    s4Bs4+1BLtemp8

    M

    else

    i)roiB>ts,

    @

    roiBroiB+ts8

    pBiL18

    LL8

    iLL8

    tempts8

    s4Bs4+1BLtemp8

    turni+1Bs4B8

    08

    or)int m08mn8mLL,

    LromB8

    "ountLL8

    M

    else

    i)roiB>0,

    @

    temproiB8

    roiB08

    pBiL18

    iLL8

    LL8

    s4Bs4+1BLtemp8

    turni+1Bs4B8

    15

  • 7/21/2019 OS Lab2012

    14/122

    08

    or)int m08mn8mLL,

    LromB8

    "ountLL8

    M

    i)in,

    i08

    Mw#ile)P0,8

    '/stem.out.println),8

    or)i08i"ount8iLL,

    @

    apw1iBs4iB8

    M

    '/stem.out.println),8

    or)i08in8iLL,

    @

    apwiBturniB+tiB8

    tLapwiB8

    tuLturniB8

    M

    or)i08in8iLL,

    '/stem.out.println); waiting time or p;L)iL1,L;is:;LapwiB,8

    loat avgt6n8

    '/stem.out.println);average waiting time is:;Lavg,8

    or)i08in8iLL,

    '/stem.out.println);turn aroun$ time is:;LturniB,8

    loat avg1tu6n8

    '/stem.out.println);average turn+aroun$ time is:;Lavg1,8

    n1"ount8

    paint)g,8

    M

    1

  • 7/21/2019 OS Lab2012

    15/122

    rea8

    "ase pr1jB,

    1

  • 7/21/2019 OS Lab2012

    16/122

    @

    temppr1iB8

    pr1iBpr1jB8

    pr1jBtemp8

    M

    or) i08in8iLL,

    or)int j08jn8jLL,

    i)pr1iBprjB,

    piBjL18

    or)i08in8iLL,

    @

    int piB8

    apw1iL1Bt+1BLapw1iB8

    M

    or) i08in8iLL,

    @

    '/stem.out.println);in$ivi$ual waiting time or

    pro"ess p;LpiBL;is;Lapw1iBL; ;,8

    M

    or) i08in8iLL,

    tLapw1iB8

    loat avgt6n8

    '/stem.out.println);average waiting time is:;Lavg,8

    or) i08in8iLL,

    @

    int piB8

    turniBt+1BLapw1iB8

    '/stem.out.println);turnaroun$ time or pro"ess

    p;LpiBL;is;LturniBL; ;,8

    M

    or) i08in8iLL,

    1J

  • 7/21/2019 OS Lab2012

    17/122

    tuLturniB8

    loat avg1tu6n8

    '/stem.out.println);average turn+aroun$ time is:;Lavg1,8

    paint)g,8

    M

    rea8

    "ase J:

    @

    int total0%0%"o0%small999%sp0%sp10%x0%"ount08

    int proBBnew intnBB8

    int wtBnew intnB8

    int wt1Bnew intnB8

    int t1Bnew intnB8

    int ttBnew intnB8

    apw1new int 20B8

    pnew int20B8

    or)int i08in8iLL,

    @

    proiB0Bi8

    '/stem.out.print);urst Time or ;LiL; :;,8

    proiB5Bnteger.parsent)oj.rea$ine),,8

    totalLproiB5B8

    '/stem.out.print);Arrival Time or ;LiL; :;,8

    proiB2Bnteger.parsent)oj.rea$ine),,8

    '/stem.out.print);riorit/ or ;LiL; :;,8

    proiB1Bnteger.parsent)oj.rea$ine),,8

    M

    or)int i08in8iLL,

    wt1iBproiB2B8

    or)int i08in8iLL,

    t1iBproiB5B8

    1=

  • 7/21/2019 OS Lab2012

    18/122

    or)int i08in8iLL,

    or)int jiL18jn8jLL,

    i)proiB2B>projB2B,

    @

    int tempBproiB8

    proiBprojB8

    projBtemp8

    M

    or)int i18itotal8iLL,

    @

    small9998

    or)int j"o8jn8jLL,

    i)>projB2B,

    "oLL8

    or)int j08j"o8jLL,

    @

    i)small>projB1B**projB5BP0,

    @

    smallprojB1B8

    spprojB0B8

    sp1j8

    M

    M

    i)pxBsp,

    @

    apw1xL1BLL8

    M

    else

    @

    xLL8

    pxBsp8

    1O

  • 7/21/2019 OS Lab2012

    19/122

    apw1xL1Bapw1xB8

    apw1xL1BLL8

    "ountLL8

    M

    prosp1B5B+18

    i)prosp1B5B0,

    ttsp1Bi8

    or)int j08jn8jLL,

    @

    i)projB5BP0**jPsp,

    wtjBL18

    M

    LL8

    M

    or)int i08in8iLL,

    or)int jiL18jn8jLL,

    i)proiB0B>projB0B,

    @

    int tempBproiB8

    proiBprojB8

    projBtemp8

    int temwtiB%tem1ttiB8

    wtiBwtjB8

    wtjBtem8

    ttiBttjB8

    ttjBtem18

    M

    '/stem.out.println),8

    or)int m08m"ount8mLL,

    pmBL18

    $oule awt0.0%att0.08

    19

  • 7/21/2019 OS Lab2012

    20/122

    '/stem.out.println),8

    or )int i 08 in8 iLL,

    @

    '/stem.out.println);waiting time or pro"ess p ;LiL; ;L

    )wtiB+wt1iB,,8

    awtL)wtiB+wt1iB,8

    attL)wtiB+wt1iBLt1iB,8

    M

    or )int i 08 in8 iLL,

    @

    '/stem.out.println);turnaroun$ time or pro"ess p;LiL; ;L

    )wtiB+wt1iBLt1iB,,8

    M

    '/stem.out.println);Average waiting time : ;L)awt6n,,8

    '/stem.out.println);Average turnaroun$ time : ;L)att6n,,8

    n1"ountL18

    paint)g,8

    M M

    M w#ile)"#P=,8

    M

    "at"# )Ex"eption e,

    @M

    M

    puli" voi$ paint)rap#i"s g,

    @

    or)int j18jn18jLL,

    @

    g.$raw-e"t)

  • 7/21/2019 OS Lab2012

    21/122

    g.$raw'tring);;Lapw1n1B%appletviewer re"tangle?ol$.#tml

    ENTE- no. o pro"esses:

    java &itting

    Enter numer o lo"s

    0,

    @

    nee$iBjBmaxiBjB+allo"iBjB8

    M

    M

    M

    '/stem.out.println); T#e $etails o max. nee$ o pro"esses;,8

    or)i08im8iLL,

    @

    '/stem.out.print);Enter t#e max nee$ resour"e or t#e pro"ess:; Li,8

    or)j08jn8jLL,

    @

    '/stem.out.print)nee$iBjB,8

    '/stem.out.print);Ht;,8

    M

    '/stem.out.print);Hn;,8

    M

    66'/stem.out.println); T#e sae se4uen"e is;,8

    sae)p%allo"%nee$%avl%m%n,8

    M

    puli" stati" voi$ sae)int pB%int allo"BB%int nee$BB%int avlB%int m%int n,

    @

    int i%j%"0%0%0%108

    intB npnew int=B8

    intB ssnew int1

  • 7/21/2019 OS Lab2012

    44/122

    or)j08jn8jLL,

    @

    i)nee$iBjBavljB,

    @

    LL8

    M

    M

    '/stem.out.println);test ;L ,8

    i)n,

    @

    '/stem.out.println);Allo"ate$ ; LpiB,8

    ssiB18

    '/stem.out.println); eore Allo"ation ;,8

    or)j08jn8jLL,

    @

    '/stem.out.print)avljB,8

    '/stem.out.println);Ht;,8

    M

    '/stem.out.println);Fo$iie$ Avl;,8

    or)j08jn8jLL,

    @

    avljBallo"iBjBLavljB8

    '/stem.out.print)avljB,8

    '/stem.out.println);Ht;,8

    M

    M

    else

    @

    ssiB9998

    M

    M

  • 7/21/2019 OS Lab2012

    45/122

    or)i08im8iLL,

    @

    i)ssiB999,

    @

    1LL8

    or)j08jn8jLL,

    @

    npBpiB8

    '/stem.out.println);unallo"ate$ pro"ess ;L npB,8

    M

    LL8

    M

    else

    @

    '/stem.out.print);Ht;,8

    M

    M

    i)1P0,

    @

    sae1)np%allo"%nee$%avl%m%n,8

    M

    M

    puli" stati" voi$ sae1)intB np%intBB allo"%int nee$BB%int avlB%int m%int n,

    @

    int i1%j1%"10%10%10%20%x08

    intB np1new int=B8

    intB ssnew int1

  • 7/21/2019 OS Lab2012

    46/122

    108

    or)j108j1n8j1LL,

    @

    i)nee$i1Bj1Bavlj1B,

    @

    1LL8

    66'/stem.out.println); re test ;L 1,8

    M

    M

    i)1n,

    @

    '/stem.out.println);Allo"ate$ pro"ess ; Li1,8

    '/stem.out.println);eore Fo$ii"ation;,8

    or)j108j1n8j1LL,

    @

    '/stem.out.print)avlj1B,8

    '/stem.out.println);Ht;,8

    M

    ssi1B18

    '/stem.out.println);Fo$iie$ Avl;,8

    or)j108j1n8j1LL,

    @

    avlj1Ballo"i1Bj1BLavlj1B8

    '/stem.out.print)avlj1B,8

    '/stem.out.println);Ht;,8

    M

    M

    else

    @

    ssi1B9998

    J

  • 7/21/2019 OS Lab2012

    47/122

    '/stem.out.println);!nallo"ate$ pro"esses; Li1,8

    M

    xxL18

    M

    M

    or)i108i1m8i1LL,

    @

    i)ssi1B999,

    @

    2LL8

    or)j108j1n8j1LL,

    @

    np1Bnpi1B8

    M

    1LL8

    M

    M

    i)20,

    @

    '/stem.out.println); T#e s/stem is in sae state;,8

    M

    else

    @

    '/stem.out.println); T#e s/stem is in unsae state;,8

    M M M

    OUTPUT:

    Q:H3'>java" aner.java

    Q:H3'>java aner

    Enter t#e no. o pro"esses java" aging.java

    Q:H3'>java aging

    Enter numer o pages java "igarette

    Agent: putting paper mat"#es

    'moer 5: etting paper mat"#es + IesP

    'moer 5: got w#at nee$e$P

    'moer 5: -olling.

    'moer 5: 'moing.

    'moer 5: Done smoing.

    Agent: putting paper mat"#es

    'moer 5: etting paper mat"#es + IesP

    'moer 5: got w#at nee$e$P

    'moer 5: -olling.

    'moer 5: 'moing.

    'moer 5: Done smoing.

    Agent: putting paper mat"#es

    'moer 5: etting paper mat"#es + IesP

    'moer 5: got w#at nee$e$P

    'moer 5: -olling.

    'moer 5: 'moing.

    'moer 5: Done smoing.

    Agent: putting toa""o paper

    'moer 2: etting toa""o paper + IesP

    'moer 2: got w#at nee$e$P 'moer 2: -olling.

    'moer 2: 'moing.

    'moer 2: Done smoing.

    Agent: putting paper mat"#es

    'moer 5: etting paper mat"#es + IesP

    =1

  • 7/21/2019 OS Lab2012

    72/122

    'moer 5: got w#at nee$e$P

    'moer 5: -olling.

    'moer 5: 'moing.

    'moer 5: Done smoing.

    Agent: putting toa""o paper

    'moer 2: etting toa""o paper + IesP

    'moer 2: got w#at nee$e$P

    'moer 2: -olling.

    'moer 2: 'moing.

    'moer 2: Done smoing.

    RESULT:

    =2

  • 7/21/2019 OS Lab2012

    73/122

    T#us t#e program or implementing "igarette smoers prolem using multit#rea$ing was

    exe"ute$ an$ output was veriie$.

    Ex.No: = MULTIPLE SLEEPING )AR)ERS

    DATE: 22 5 2012. PRO)LEM

    AIM:

    To write a java program to implement t#e multiple sleeping arers prolem.

    PRO)LEM STATEMENT:

    To simulate multiple sleeping arers% all in one arers#op t#at #as a inite numer o

    "#airs in t#e waiting room. Ea"# "ustomer is instantiate$ rom a single "ustomer "lass an$ ea"#

    arer is instantiate$ rom a single arer "lass(

    ALGORITHM:

    Step 1:mport all t#e java iles.

    Step 2: Create a "lass "alle$ em1 to implement all t#e "ustomers in t#e s/stem an$ "reate

    a separate "lass or arers or servi"ing t#e "ustomer.

    Step !: n t#e main "lass initialiSe all t#e variales an$ get t#e numer o "ustomers

    entering in t#e s/stem.

    Step ": t#e numer o "ustomer is less t#an t#e no o "#airs "#airs t#en all t#e "ustomer

    will e serve$ or else t#e "ustomer #ave to wait until an$ unless t#e arer ree.

    PROGRAM:

    import java.io.78

    import java.lang.78"lass "ust

    @

    puli" int $isp)int "n,

    @

    return)"n,8

    =5

  • 7/21/2019 OS Lab2012

    74/122

    M

    M

    "lass em1 exten$s T#rea$

    @

    arer mnew arer),8

    "ust "new "ust),8

    puli" s/n"#roniSe$ voi$ run),

    @

    tr/

    @

    w#ile)m."numm.n,

    @

    int t".$isp)m."numLL,8

    '/stem.out.println);arer2 serves Customer ;Lt,8

    T#rea$.sleep)1

  • 7/21/2019 OS Lab2012

    75/122

    "ust "new "ust),8

    int j8

    '/stem.out.println);Enter no o C#airs in"lu$ing two arer C#airs: ;,8

    "#nteger.parsent)r.rea$ine),,8

    '/stem.out.println);Enter no o Customers : ;,8

    nnteger.parsent)r.rea$ine),,8

    e.start),8

    i)"#n,

    @

    n1n+"#8

    '/stem.out.println)n1L; Customers et t#e '#op;,8

    nn+n18

    '/stem.out.println)"#L; Numer o "ustomers in t#e s#op;,8

    wt"#+28

    '/stem.out.println)wtL;Numer o "ustomers waiting in t#e s#op;,8

    M

    i)"#>n,

    @

    "#+n8

    '/stem.out.println)L; No o ree "#airs;,8

    M

    66e.start),8

    w#ile)"numn,

    @

    int t".$isp)"numLL,8

    '/stem.out.println);arer1 serves ; L; Customer ; L t,8

    T#rea$.sleep)1000,8

    M

    '/stem.out.println);arer1 sleeps ;,8

    M

    "at"#)Ex"eption e,@M

    =

  • 7/21/2019 OS Lab2012

    76/122

    M

    M

    OUTPUT:

    Q:H>java" arer.java

    Q:H>java arer

    Enter no o C#airs in"lu$ing two arer C#airs:

    java" et'/stem.java

    Q:H3'>java et'/stem

    ntera"e Name : F' TC oopa" intera"e

    Car$Name: lo

    o"al #ost Name : lo"al#ost

    Gost A$$ress : 12=.0.0.1

    #ar$ware a$ress:

    00+01+JC+

  • 7/21/2019 OS Lab2012

    81/122

    T#us t#e java program or implementation o retrieval o networ operating s/stem

    inormation was written an$ exe"ute$.

    REAL 8 TIME OPERATING

    SYSTEMS

    O1

  • 7/21/2019 OS Lab2012

    82/122

    Ex.No.:9 ALARM CLOC+

    Date: 12 2012

    AIM:

    To write a C program or implementing Alarm Clo".

    PRO)LEM STATEMENT:

    Clo" wit# alarm un"tionalit/ s#all e implemente$% t s#all e possile to set t#e time%

    an$ to set t#e alarm time% t#e alarm s#all e enabledw#en t#e alarm time is set% t#e alarm s#all e

    activatedw#en t#e alarm is enale$% an$ w#en t#e "urrent time is e4ual to t#e alarm time% an

    a"tivate$ alarm must e a"nowle$ge$. A"nowle$gement o an alarm s#all lea$ to t#e alarm

    eing disabled, thealarm is enale$ again w#en a new alarm time is set% an alarm w#i"# is not

    a"nowle$ge$ s#all e repeate$ ever/ 10 se"on$s. T#e program s#all "ommuni"ate wit# a

    grap#i"al user intera"e% w#ere t#e "urrent time s#all e $ispla/e$% an$ w#ere t#e alarm time s#all

    e $ispla/e$ w#en t#e alarm is enale$. t s#all e possile to terminate t#e program% using a

    "omman$ w#i"# is sent rom t#e grap#i"al user intera"e.

    ALGORITHM:

    Step 1:mport all t#e " iles.

    Step 2: et t#e input rom t#e user set t#e time an$ alarm time(

    Step !: "urrent time is e4ual to alarm time t#en alarm must a"tivate$ an$ a"nowle$ge$. An$

    t#ere is no a"nowle$gement

    Step ":T#e alarm is enale$ again on"e a new alarm time is set an$ a"tivates an

    a"nowle$gement.

    Step 4:'top t#e program w#en "omman$ rom !.

    PROGRAM:

    ]in"lu$est$io.#>

    O2

  • 7/21/2019 OS Lab2012

    83/122

    ]in"lu$e"onio.#>

    ]in"lu$e$os.#>

    ]in"lu$est$li.#>

    ]in"lu$egrap#i"s.#>

    ]$eine ATG ;":Ht"HgiH;

    voi$ main),

    @

    int i%j%%#our%min%g%/18

    "#ar msgO0B8

    int g$DETECT%gm8

    initgrap#)*g$%*gm%ATG,8

    stru"t $ostime?t t8

    ?$os?gettime)*t,8

    set"olor)1,8

    sprint)msg%;T#e "urrent time is: [$ [02$ [02$;% t.#our%

    t.minute%t.se"on$,8

    outtextx/)1 #our , goto 8

    else i)#our t.#our ** mint.minute , goto 8

    else

    @

    O5

  • 7/21/2019 OS Lab2012

    84/122

    or)it.#our8i#our8iLL,

    @

    or)jt.minute8jJ08jLL,

    @

    08

    w#ile)J0,

    @

    "lear$evi"e),8

    LL8

    setillst/le)1%1,8

    ar)

  • 7/21/2019 OS Lab2012

    85/122

    goto a8

    MMMMM

    a:

    get"#),8

    exit)0,8

    :

    "lear$evi"e),8

    outtextx/)2

  • 7/21/2019 OS Lab2012

    86/122

    T#us t#e program or implementing -eal+time 3perating '/stem !sing Alarm Clo" was

    written exe"ute$ an$ output was veriie$.

    DATA)ASE OPERATING

    SYSTEMS

    OJ

  • 7/21/2019 OS Lab2012

    87/122

    Ex. No.: 10 TRANSACTION AND CONCURRENCY CONTROL

    DATE: 19 2012

    AIM:

    To write a (ava program to implement aning appli"ation or #an$ling t#e "on"urren"/

    "onli"t t#at o""urs etween multiple "lient appli"ations.

    PRO)LEM STATEMENT:

    Assume an/ appli"ation )e.g. aning, on /our own an$ $o t#e ollowing exer"ises.1. nvestigate an$ implement t#e 3je"t 'toreUs "on"urren"/ options.

    2. mplement t#e "on"urren"/ "onli"t t#at o""urs etween multiple "lient appli"ations.

    5. 3serve an$ implement t#e impli"ation o neste$ transa"tions.

    ALGORITHM:

    CLIENT:1. Create a menu to #ave a new a""ount% "#e" alan"e% wit#$raw% $eposit an$ transer.

    2. et t#e "#oi"e rom t#e user an$ write it to t#e server.

    5. t#e "#oi"e is 1% get t#e a""ount name an$ "reate t#e a""ount numer.

    . t#e "#oi"e is 2% get t#e a""ount numer an$ $ispla/ t#e alan"e.

  • 7/21/2019 OS Lab2012

    88/122

    1. et t#e "#oi"e rom t#e "lient.

    2. t#e "#oi"e is 1% estalis# a $ataase "onne"tion an$ insert t#e new a""ount $etails

    into t#e $ataase.

    5. t#e "#oi"e is 2% sele"t t#e ne"essar/ re"or$ an$ "#e" t#e alan"e.

    . t#e "#oi"e is 5% %

  • 7/21/2019 OS Lab2012

    89/122

    @

    '/stem.out.println);;,8'/stem.out.println);1.Create a new A""ountHn

    2.C#e" alan"eHn5.Rit#$rawHn .DepositHn

  • 7/21/2019 OS Lab2012

    90/122

    out.writent)a"no1,8

    M

    rea8"ase 2:

    '/stem.out.print);Enter t#e A""ount Numer:;,8

    int a"no08oolean pa"#alse8

    tr/

    @a"nos".nextnt),8

    M

    "at"#)Ex"eption e,

    @'/stem.out.println);Enter t#e \ali$ A""ount Numer;,8

    pa"#true8

    M

    int al08out.writent)a"no,8

    i)in.rea$oolean),,@

    alin.rea$nt),8

    '/stem.out.println);t#e alan"e is:;Lal,8M

    else

    @

    i)pa"#alse,@

    '/stem.out.println);A""ount not availale;,8

    MM

    rea8

    "ase 5:oolean "#e"1alse8

    int a"no508

    int amt508

    tr/@

    '/stem.out.print);Enter t#e A""ount Numer:;,8

    a"no5s".nextnt),8'/stem.out.print);Enter t#e Ammount:;,8

    amt5s".nextnt),8

    M"at"#)Ex"eption e,

    @

    '/stem.out.println);nvali$ nput;,8

    "#e"1true8

    90

  • 7/21/2019 OS Lab2012

    91/122

    M

    i)"#e"1alse,

    @out.writent)a"no5,8

    out.writent)amt5,8

    i)in.rea$oolean),,@

    i)in.rea$oolean),,

    @'/stem.out.println);Fone/ is Rit#$rawn;,8

    M

    else

    @'/stem.out.println);Fone/ is not Enoug# to Rit#$raw;,8

    M

    M

    else@

    '/stem.out.println);A""ount is not availale;,8rea8

    M

    Mrea8

    "ase :

    '/stem.out.print);Enter t#e A""ount Numer:;,8

    int a"nos".nextnt),8out.writent)a"no,8

    '/stem.out.print);Enter t#e Ammount:;,8

    int amts".nextnt),8out.writent)amt,8

    i)in.rea$oolean),,

    @'/stem.out.println);Ammount is Deposite$;,8

    M

    else

    @'/stem.out.println);C#e" t#e A""ount Numer;,8

    M

    rea8"ase

  • 7/21/2019 OS Lab2012

    92/122

    '/stem.out.print);Enter t#e Amount to transer:;,8

    int tamts".nextnt),8

    out.writent)tamt,8rea8

    "ase J:

    '/stem.exit)0,8rea8

    $eault:

    '/stem.out.println);nvali$ nput;,8rea8

    M

    M

    Minall/

    @

    s."lose),8

    MM"at"#)3Ex"eption e,

    @e.print'ta"Tra"e),8

    M MM

    Ser-er

    import java.io.78

    import java.net.78import java.util.78

    import java.lang.78

    puli" "lass "lient@

    puli" stati" voi$ main)'tring argsB,

    @tr/

    @

    'o"et snew 'o"et);12=.0.0.1;%O0O0,8

    tr/@

    Data3utput'tream outnew

    Data3utput'tream)s.get3utput'tream),,8Datanput'tream innew

    Datanput'tream)s.getnput'tream),,8out.writent)2

  • 7/21/2019 OS Lab2012

    93/122

    '/stem.out.println);1.Create a new A""ountHn

    2.C#e" alan"eHn 5.Rit#$rawHn .DepositHn

  • 7/21/2019 OS Lab2012

    94/122

    @

    a"nos".nextnt),8

    M"at"#)Ex"eption e,

    @

    '/stem.out.println);Enter t#e \ali$ A""ount Numer;,8pa"#true8

    M

    int al08out.writent)a"no,8

    i)in.rea$oolean),,

    @

    alin.rea$nt),8'/stem.out.println);t#e alan"e is:;Lal,8

    M

    else

    @ i)pa"#alse,

    @'/stem.out.println);A""ount not availale;,8

    M

    Mrea8

    "ase 5:

    oolean "#e"1alse8

    int a"no508int amt508

    tr/

    @'/stem.out.print);Enter t#e A""ount Numer:;,8

    a"no5s".nextnt),8

    '/stem.out.print);Enter t#e Ammount:;,8amt5s".nextnt),8

    M

    "at"#)Ex"eption e,

    @'/stem.out.println);nvali$ nput;,8

    "#e"1true8

    Mi)"#e"1alse,

    @

    out.writent)a"no5,8out.writent)amt5,8

    i)in.rea$oolean),,

    @

    i)in.rea$oolean),,

    9

  • 7/21/2019 OS Lab2012

    95/122

    @

    '/stem.out.println);Fone/ is Rit#$rawn;,8

    Melse

    @

    '/stem.out.println);Fone/ is not Enoug# toRit#$raw;,8

    M

    Melse

    @

    '/stem.out.println);A""ount is not availale;,8rea8

    M

    Mrea8

    "ase :'/stem.out.print);Enter t#e A""ount Numer:;,8

    int a"nos".nextnt),8out.writent)a"no,8

    '/stem.out.print);Enter t#e Ammount:;,8

    int amts".nextnt),8out.writent)amt,8

    i)in.rea$oolean),,

    @'/stem.out.println);Ammount is Deposite$;,8

    M

    else@

    '/stem.out.println);C#e" t#e A""ount Numer;,8

    M

    rea8"ase

  • 7/21/2019 OS Lab2012

    96/122

    $eault:

    '/stem.out.println);nvali$ nput;,8

    rea8M

    M

    Minall/

    @

    s."lose),8M

    M"at"#)3Ex"eption e,

    @

    e.print'ta"Tra"e),8MMM

    OUTPUT:Ser-er

    Q:H>java" server.java

    Q:H >java server

    Clie't

    Q:H >java" "lient.java

    Q:H >java "lient

    1.Create a new A""ount

    2.C#e" alan"e

    5.Rit#$raw

    .Deposit

  • 7/21/2019 OS Lab2012

    97/122

    2. C#e" alan"e

    5. Rit#$raw

    . Deposit

  • 7/21/2019 OS Lab2012

    98/122

    Fone/ is Rit#$rawn

    1.Create a new A""ount

    2.C#e" alan"e

    5.Rit#$raw

    .Deposit

  • 7/21/2019 OS Lab2012

    99/122

    1.Create a new A""ount

    2.C#e" alan"e

    5.Rit#$raw

    .Deposit

  • 7/21/2019 OS Lab2012

    100/122

    2

    Enter t#e A""ount Numer: 1001

    t#e alan"e is:2000

    1.Create a new A""ount

    2.C#e" alan"e

    5.Rit#$raw

    .Deposit

  • 7/21/2019 OS Lab2012

    101/122

    RESULT:

    T#us t#e (ava program to implement aning appli"ation or #an$ling t#e "on"urren"/

    "onli"t t#at o""urs etween multiple "lient appli"ations. Ras written an$ exe"ute$ su""essull/.

    101

  • 7/21/2019 OS Lab2012

    102/122

    DISTRI)UTED OPERATING

    SYSTEMS

    Ex.No: 11 GENERATION O* ', SET O* NUM)ERS

    DATE: 2J 2012 USING RMI

    AIM:

    To write a java program or implementing -F to generate XnU set o numers.

    PRO)LEM STATEMENT:

    During an -F appli"ation% ea"# time /ou run t#e "lient program t#r -F "lient n

    t#e server program `-F server will generate XnU sets o -an$om numer XnU is a positive integer.

    ALGORITHM:

    Step 1: mport all java iles in -F server an$ -F "lient iles.

    Step 2: Create a -F server "lass t#at will exten$ t#e remote oje"t an$ implement re"eive

    102

  • 7/21/2019 OS Lab2012

    103/122

    message)w#i"# is a "lient intera"e etween server an$ "lient,

    Step !: "reate a -F server "lass in w#i"# t#e re"eive message #as een $e"lare$(

    Step ": w#ile exe"uting t#e server no$e an$ port numer t#at server an$ it will as

    t#e numer o items #as to e sent an$ $ispla/ t#ose items.

    Step : n -F "lient t#e port numer generate$ in t#e server will e given or

    re"eiving all t#e items.

    PROGRAM:

    RMI CLIENT:

    import java.io.78

    import java.rmi.78

    import java.rmi.registr/.78

    import java.net.78

    puli" "lass -miClient

    @

    stati" puli" voi$ main)'tring argsB,t#rows 3Ex"eption

    @

    -e"eiveFessagentera"e rmi'erver8

    -egistr/ registr/8

    'tring serverA$$ressargs0B8

    'tring serverortargs1B8

    66'tring textargs2B8

    int no8

    nonteger.parsent)args2B,8

    int rvalBnew intnoB8

    66'/stem.out.println);Enter t#e numers;,8

    66uere$-ea$er rnew uere$-ea$er)new nput'tream-ea$er)'/stem.in,,8

    '/stem.out.println);sen$ing ;LnoL; to ;LserverA$$ressL;:;Lserverort,8

    tr/@

    66 get t#e `registr/

    105

  • 7/21/2019 OS Lab2012

    104/122

    registr/o"ate-egistr/.get-egistr/)

    serverA$$ress%

    )new nteger)serverort,,.int\alue),

    ,8

    66 loo up t#e remote oje"t

    rmi'erver

    )-e"eiveFessagentera"e,)registr/.looup);rmi'erver;,,8

    66 "all t#e remote met#o$

    rvalrmi'erver.re"eiveFessage)no,8

    or)int i08ino8iLL,

    '/stem.out.println)rvaliB,8

    M

    "at"#)-emoteEx"eption e,@

    e.print'ta"Tra"e),8

    M

    "at"#)Notoun$Ex"eption e,@

    e.print'ta"Tra"e),8

    M

    M

    M

    RMI SER#ER:

    import java.util.78

    import java.rmi.78

    import java.rmi.registr/.78

    import java.rmi.server.78

    import java.net.78

    puli" "lass -mi'erver exten$s java.rmi.server.!ni"ast-emote3je"t

    implements -e"eiveFessagentera"e

    @

    nt t#isort8

    10

  • 7/21/2019 OS Lab2012

    105/122

    'tring t#isA$$ress8

    -egistr/ registr/8 66 rmi registr/ or looup t#e remote oje"ts.

    66 T#is met#o$ is "alle$ rom t#e remote "lient / t#e -F.

    66 T#is is t#e implementation o t#e `-e"eiveFessagentera"e.

    puli" intB re"eiveFessage)int x, t#rows -emoteEx"eption

    @

    '/stem.out.println)x,8

    -an$om rnew -an$om),8

    int valB new intxB8

    or)int i08ix8iLL,

    @

    valiBr.nextnt)2

  • 7/21/2019 OS Lab2012

    106/122

    registr/.rein$);rmi'erver;% t#is,8

    M

    "at"#)-emoteEx"eption e,@

    t#row e8

    M

    M

    stati" puli" voi$ main)'tring argsB,

    @

    tr/@

    -mi'erver snew -mi'erver),8

    M

    "at"# )Ex"eption e, @

    e.print'ta"Tra"e),8

    '/stem.exit)1,8

    M

    M

    M

    RECEI#E MESSAGE INTER*ACE:

    import java.rmi.78

    puli" intera"e -e"eiveFessagentera"e exten$s -emote

    @

    puli" int B re"eiveFessage)int x, t#rows -emoteEx"eption8

    M

    OUTPUT:

    Q:HiisemHosla>java -mi'erver

    t#is a$$ressno$e+956192.1JO.2.105%port5252

    J

    Q:HiisemHosla>java -miClient 192.1JO.2.105 5252 J

    10J

  • 7/21/2019 OS Lab2012

    107/122

    sen$ing J to 192.1JO.2.105:5252

    10

    2

    12

    5

    19

    tsiB,

    @

    temptsiB8

    M

    M

    '/stem.out.println);ENTE-N C-TCA 'ECT3N.......;,8

    '/stem.out.println);Hn;,8

    10O

  • 7/21/2019 OS Lab2012

    109/122

    '/stem.out.println);'TEV' TFE'TAF';,8

    '/stem.out.println);Hn;,8

    or)i08in8iLL,

    @

    j108

    '/stem.out.print)i,8

    '/stem.out.print);Ht;,8

    w#ile)re44iBj1BP0,

    @

    or)j08jn8jLL,

    @

    i)re44iBj1BtsjB,

    @

    '/stem.out.print););L re44iBj1B L;%; Lj L;,;,8

    '/stem.out.print);Ht;,8

    M

    M

    j1LL8

    M

    '/stem.out.println);Hn;,8

    M

    or)i08in8iLL,

    @

    i))temptsiB, ** )tsiBP0,,

    @

    '/stem.out.println);T#e site ; L i L; is in "s;,8

    '/stem.out.println);Hn;,8

    tsiB08

    '/stem.out.println);-EEA'N.......;,8

    '/stem.out.println);Hn;,8

    or)j08jn8jLL,

    109

  • 7/21/2019 OS Lab2012

    110/122

    @

    i)iPj,

    @

    '/stem.out.println); -elease message to :; Lj L ;

    rom ; LiLHn ,8

    M

    M

    M

    M

    '/stem.out.println);-e4uest bueue !p$ations ;,8

    '/stem.out.println);+++++++++++++++++++++++++++;,8

    '/stem.out.println);'TE'V -Eb!'T b!E!E';,8

    or)i08in8iLL,

    @

    '/stem.out.print)i,8

    '/stem.out.print);Ht;,8

    sen$release)i%re44%n%%ts,8

    M

    M

    puli" stati" voi$ sen$release)int i%intBB re44%int n%int %intB ts,

    @

    int j08

    w#ile)re44iBjBP0,

    @

    re44iBjBre44iBLLjB8

    M

    int t0%j108

    w#ile)re44iBtBP0,

    @

    110

  • 7/21/2019 OS Lab2012

    111/122

    or)j108j1n8j1LL,

    @

    i)re44iBtBtsj1B,

    @

    '/stem.out.print););L re44iBtBL;%; Lj1 L;,;,8

    '/stem.out.print);Ht;,8

    rea8

    M

    M

    tLL8

    M

    '/stem.out.println);Hn;,8

    M

    puli" stati" voi$ sen$re4uest)int s%int r%int st%int rt%intB ts%int re44BB%int n,

    @

    int i0%j0%t08

    '/stem.out.println); -e4uest 4ueue elements eore getting re4uest o :; Lr,8

    w#ile)re44rBiBP0,

    @

    or)t08tn8tLL,

    @

    i)re44rBiBtstB,

    @

    '/stem.out.print); ) ; L re44rBiB L; %; L t L;,;,8

    '/stem.out.print);Ht;,8

    rea8

    M

    111

  • 7/21/2019 OS Lab2012

    112/122

    M

    iiL188

    M

    re44rBiBst8

    Arra/s.sort)re44rB%0%)iL1,,8

    '/stem.out.println);Hn;,8

    '/stem.out.println); ater getting re4uest; ,8

    w#ile)re44rBjBP0,

    @

    or)i08in8iLL,

    @

    i)re44rBjBtsiB,

    @

    '/stem.out.print); ) ; L re44rBjB L; %; L i L;,;,8

    '/stem.out.print);Ht;,8

    rea8

    M

    M

    jLL8

    M

    '/stem.out.println);Hn;,8

    M

    puli" stati" voi$ main)'tring argsB,t#rows 3Ex"eption

    @

    int i%j%n%repl/%0%temp%108

    intB snew int10B8

    intB tsnew int10B8

    intBB re44new int10B10B8

    uere$-ea$er rnew uere$-ea$er)new nput'tream-ea$er)'/stem.in,,8

    '/stem.out.println);Enter t#e numer o sites availale;,8

    nnteger.parsent)r.rea$ine),,8

    112

  • 7/21/2019 OS Lab2012

    113/122

    or)i08in8iLL,

    @

    siBi8

    '/stem.out.println);Enter t#e timestamp value or :;L i,8

    tsiBnteger.parsent)r.rea$ine),,8

    re44iB0BtsiB8

    M

    '/stem.out.println);Hn;,8

    '/stem.out.println);'TEsV -Eb!E'T b!E!E';,8

    or)i08in8iLL,

    @

    '/stem.out.println)iL;Ht;L;);LtsiBL;%;LiL;,;,8

    M

    '/stem.out.println);Hn;,8

    '/stem.out.println);-Eb!E'TN.......;,8

    '/stem.out.println);Hn;,8

    or)i08in8iLL,

    @

    '/stem.out.println);T#e pro"ess ; L i L; 'en$ing t#e re4uest to t#e sites ;,8

    '/stem.out.println);++++++++++++++++++++++++++++++++++++++++++++++;,8

    or)j08jn8jLL,

    @

    i))iPj, ** )tsiBP0,,

    @

    '/stem.out.println); T3 :; L j,8

    '/stem.out.println);Hn;,8

    sen$re4uest)i%j%tsiB%tsjB%ts%re44%n,8

    '/stem.out.println);Hn;,8

    M

    M

    115

  • 7/21/2019 OS Lab2012

    114/122

    M

    '/stem.out.println);-EI............;,8

    '/stem.out.println);Hn;,8

    or)i08in8iLL,

    @

    '/stem.out.println);'TE ; L iL ; getting repl/ ;,8

    '/stem.out.println);+++++++++++++++++++++++++;,8

    or)j08jn8jLL,

    @

    i))iPj, ** )tsiBP0,,

    @

    '/stem.out.println);etting repl/ rom ; Lj L;:;L ; ) ; L

    tsjB L; %;LjL ;, Hn;,8

    M

    M

    M

    or)j18jn8jLL,

    @

    i)tsiB>tsjB,

    @

    temptsjB8

    tsjBtsiB8

    tsiBtemp8

    M

    M

    exe"ute"s)i%ts%n%%re44,8

    M

    M

    OUTPUT:

    Q:H>java" nontoen.java

    11

  • 7/21/2019 OS Lab2012

    115/122

    Q:H>java nontoen

    Enter t#e numer o sites availale

    2

    Enter t#e timestamp value or: 0

    5

    Enter t#e timestamp value or: 1

    2

    'TEs -Eb!E'T b!E!E'

    0 )5%0,1 )2%1,

    -Eb!E'TNccccc

    T#e pro"ess 0 sen$ing t#e re4uest to t#e sites T3: 1

    -e4uest 4ueue elements eore getting re4uest o :1

    )2%1,

    Ater getting re4uest

    )2%1, )5%0,

    T#e pro"ess 1 sen$ing t#e re4uest to t#e sites

    T3: 0

    -e4uest 4ueue elements eore getting re4uest o :0

    ) 5%0,

    Ater getting re4uest

    )2%1, )5%0,

    -EIcccc..

    'TE 0 getting repl/

    etting repl/ rom 1:)2%1,

    'TE 1 getting repl/

    etting repl/ rom 0:)5%0,

    ENTE-N C-TCA 'ECT3Nccc.

    'TEUs TFE'TAF'

    0 )2%1, )5%0,

    11

  • 7/21/2019 OS Lab2012

    116/122

    1 )2%1, )5%0,

    T#e site 1 is in "s

    -EEA'Nccc..

    -elease message to :0 rom 1

    -e4uest bueue !p$ations

    'TEUs -Eb!E'T b!E!E'

    0 )5.0,

    1 )5%0,

    RESULT:

    T#us t#e java program to implement t#e Non+Toenase$ algorit#m )amportUs

    algorit#m, or $istriute$ mutual ex"lusion was written an$ exe"ute$ su""essull/.

    Ex No.:15 TO+EN )ASED ALGORITHM

    Date: 10 < 2012

    AIM:

    To write a java program to implement t#e Toen ase$ alogorit#m )'uSui ^asamiUs

    algorit#m, or $istriute$ mutual ex"lusion.

    ALGORITHM:

    Step 1: etting t#e numer o sites rom t#e user

    1.1. enerate t#e se4uen"e numer or t#e sites ran$oml/.

    1.2. 'ort t#e se4uen"e numer o t#e site in as"en$ing or$er.

    1.5. T#e sites #aving outstan$ing se4uen"e numer is allowe$ to enter t#e C' irst.

    Step2: -Eb!E'TN:

    11J

  • 7/21/2019 OS Lab2012

    117/122

    2.1. Ea"# site wants to enter C' sen$ re4uest to ot#er site in t#e orm o site i$ se4uen"e

    numer

    2.2. Ater re"eving t#e re4uest ot#er sites up$ates t#eir re4uest 4ueue an$ sites giving t#e

    re4uest an$ t#e site i$ is pla"e$ in toen 4ueue.

    Step!: ENTE-N C-TCA 'ECT3N:

    5.1. 'ite wit# t#e most outstan$ing re4uest value will enter t#e C' irst.

    5.2. t#e site #aving toen is eing in "riti"al se"tion is sen$s t#e toen to t#e outstan$ing

    se4uen"e numer ater inis#ing t#e C'.

    Step": EKTN TGE C-TCA 'ECT3N:

    T#e siteUs i$ w#i"# is exiting t#e C' t#e "orrespon$ing siteUs i$ is remare$ rom t#e toen

    4ueue.

    PROGRAM:

    import java.util.-an$om8

    import java.io.78

    import java.lang.78

    import java.util.Arra/s8

    puli" "lass Toenase$

    @

    puli" stati" voi$ sen$re4)int s%int r%int snumer%intB se4no%intBB rn%int n%int toB%intB t4,

    @

    int i08

    rnsB0Bsnumer8

    '/stem.out.println); -e4uest rom ; Ls L ; to ; L rL ;:; L;);LsL;%;L )snumerL1,

    L;, Hn;,8

    '/stem.out.print);Toen bueue ;L ;Ht;,8

    or)i08i)n+1,8iLL,

    @

    '/stem.out.print) t4iB L ;Ht;,8

    M

    '/stem.out.println);Hn;,8

    11=

  • 7/21/2019 OS Lab2012

    118/122

    '/stem.out.println);T#e -e4uest bueue o ; Ls L ; is : ; L;Ht;L )LLrnsB0B,,8

    M

    puli" stati" voi$ exe"ute"s)intB to%int s%intB t4%int n%intB se4no,

    @

    int i0%j%temp08

    or)i08in8iLL,

    @

    i)toiB1,

    @

    or)j08jn8jLL,

    @

    i))s)se4nojB,, ** )iPj,,

    @

    tempj8

    rea8

    M

    M

    '/stem.out.println);T#e site ;L i L ; sen$s t#e toen to ;L )j,,8

    '/stem.out.println);Hn;,8

    '/stem.out.println);????????????????????????????????????;,8

    '/stem.out.println);T#en site ;Lj L ; enters Criti"al 'e"tion ;,8

    '/stem.out.println);????????????????????????????????????;,8

    '/stem.out.println);Hn;,8

    '/stem.out.println);T#e toen 4ueue e"omes :;,8

    or)i08i)n+1,8iLL,

    @

    i)t4iBPj,

    @

    '/stem.out.println)t4iBL ;Ht;,8

    M

    11O

  • 7/21/2019 OS Lab2012

    119/122

    M

    rea8

    M

    M

    M

    puli" stati" voi$ main)'tring argsB,t#rows 3Ex"eption

    @

    int i%j%n%repl/%0%temp%t108

    intB tonew int10B8

    -an$om rnew -an$om),8

    intBB rnnew int10B10B8

    uere$-ea$er rnew uere$-ea$er)new nput'tream-ea$er)'/stem.in,,8

    '/stem.out.println);Enter t#e numer o sites availale;,8

    nnteger.parsent)r.rea$ine),,8

    intB t4new intnL1B8

    intB se4nonew intnL1B8

    intB nsnnew intnL1B8

    intB snnew intnL1B8

    '/stem.out.println);Hn;,8

    or)i08in8iLL,

    @

    t4iB9998

    sniBr.nextnt)10,8

    se4noiBsniB866 a"up t#e se4numer arra/

    '/stem.out.println); T#e 'e4uen"e numer or; L iL ; is : ;LsniB,8

    M

    '/stem.out.println); Hn;,8

    Arra/s.sort)sn%0%)nL1,,8

    or)i18in8iLL,

    @

    '/stem.out.print)sniBL ;Ht;,8

    119

  • 7/21/2019 OS Lab2012

    120/122

    M

    '/stem.out.println);Hn;,8

    or)in%j08i>08jLL%i++,

    @

    nsnjBsniB8

    66'/stem.out.println); T#e new 'e4uen"e numer or ; L jL ; is : ;LnsnjB,8

    M

    tempnsn1B866 to "#e" or in$ing w#i"# site is in C' in ex"ute"s

    or)i08in8iLL,

    @

    i)nsn0Bse4noiB,

    @

    toiB1866 it# site is #aving t#e toen

    rniB0Bnsn0B8

    '/stem.out.println); Toen is in ; L i L ; t# site .so T#e site ; L i L ;

    is eing in Criti"al 'e"tion;,8

    rea8

    M

    M

    or)i08in8iLL,

    @

    i)toiBP1,

    @

    '/stem.out.println); Hn;,8

    '/stem.out.println);T#e pro"ess ; L i L ;'en$ing t#e re4uest to t#e

    sites ;,8

    '/stem.out.println);++++++++++++++++++++++++++++++++++++++++++++++;,8

    t4t1Bi8

    t1t1L18

    or)j08jn8jLL,

    @

    120

  • 7/21/2019 OS Lab2012

    121/122

    i)iPj,

    @

    '/stem.out.println); T3 :; L j,8

    '/stem.out.println);Hn;,8

    sen$re4)i%j%se4noiB%se4no%rn%n%to%t4,8

    '/stem.out.println);Hn;,8

    M

    M

    M

    M

    exe"ute"s)to%temp%t4%n%se4no,8

    M

    M

    OUTPUT:

    Q:H3'>java Toenase$

    Enter t#e numer o sites availale 5

    T#e 'e4uen"e numer or0 is : J

    T#e 'e4uen"e numer or1 is : 2

    T#e 'e4uen"e numer or2 is : J

    2 J J

    Toen is in 0 t# site .so T#e site 0 is eing in Criti"al 'e"tion

    T#e pro"ess 1 sen$ing t#e re4uest to t#e sites

    T3 :0

    -e4uest rom 1 to 0:)1%5,

    Toen bueue 1 999

    T#e -e4uest bueue o 1 is : 5

    T3 :2

    -e4uest rom 1 to 2:)1%5,

    Toen bueue 1 99

    T#e -e4uest bueue o 1 is : 5

    121

  • 7/21/2019 OS Lab2012

    122/122

    T#e pro"ess 2 'en$ing t#e re4uest to t#e sites

    T3 :0

    -e4uest rom 2 to 0:)2%=,

    Toen bueue 1 2

    T#e -e4uest bueue o 2 is : =

    T3 :1

    -e4uest rom 2 to 1:)2%=,

    Toen bueue 1 2

    T#e -e4uest bueue o 2 is : =

    T#e site 0 sen$s t#e toen to 2

    T#en site 2 enters Criti"al 'e"tion

    T#e toen 4ueue e"omes:

    1

    RESULT:

    T#us t#e java program to implement t#e toenase$ algorit#m )'uSui ^asamiUs algorit#m,

    or mutual ex"lusion was written an$ exe"ute$ su""essull/.