Top Banner

of 174

BE-IT-V SEM-95808-OS and Java Programming Lab Manual

Nov 02, 2015

Download

Documents

jayaprabas

free
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 1DEPT OF COMPUTER SCIENCE AND ENGGB.E[INFORMATION TECHNOLOGY]

    V-SEMESTER

    95808- OPERATING SYSTEM AND JAVAPROGRAMMING LAB MANUAL

    Lab InchargeDr. P. ArunaProfessorDept of CSE

  • 2Annamalai UniversityCYCLE-I

    JAVA PROGRAMMING

    Ex.no Name of the Exercises

    1. Classes and Objects

    2. Command Line Argument

    3. Bitwise Operators

    4. Method Overloading

    5. Packages

    6.Interface

    7. Inheritance

  • 38. Exception Handling

    9. User Defined Exception Handling

    10. Multithreading:join() and isAlive()

    11. Multithreading:Inter threadcommunications

    12. Addition of Two Numbers using Applet

  • 4CYCLE-IIUNIX PROGRAMMING

    Ex.no Name of the Exercises

    1. Job scheduling techniques

    2.Disk scheduling techniques

    3.Memory allocation techniques

    4.Memory management techniques

    5.Page replacement techniques

    6. Producer consumer problem

    7.Bankers algorithm

    8.Dining Philosophers problem

  • 5Ex.No: 1CLASSES AND OBJECTS

    Aim:To write a java program to work with the creation of objects for the class

    with overloaded constructor and user defined methods returning a value.

    Algorithm: Start Create an object called room. Create an object with overloaded constructor and get length and breadth. Create a function room where only length is passed and breadth remains

    constant. Create a function getdata() to return the area. Create another class ex1 that includes the main function. Declare variable int l=0,b=0,float area. Get the dynamic input using exceptional try and catch. Pass the input values to the constructor. Calculate the input values to the getdata(). Display the result.

    Source code:import java.io.*;import java.io.DataInputStream;class Room{float length,breadth;Room(int a,int b){

    length=a;breadth =b;

    }Room(int a){

  • 6length =a;breadth=100;}float getdata(){Return(length*breadth);}

    }class ex1{public static void main(String args[]){float area;int l=0,b=0;DataInputStream in=new DataInputStream(System.in);try{System.out.println(\n enter the value for Room length:);l=Integer.parseInt(in.readLine());System.out.println(\n enter the value for Room breadth:);b=Integer.parseInt(in.readLine());

    }Catch(Exception e){}Room room1=new Room(l,b);area=room1.getdata();System.out.println(\n length=+room1.length);System.out.println(\n breadth=+room1.breadth);System.out.println(\n Area=+area);

  • 7System.out.println(\n passing only length of the room with breadth=100);Room room2=new Room(l);area =room2,getdata();System.out.println(\n length=+room2.length);System.out.println(\n breadth=+room2.breadth);System.out.println(\n Area=+area);}}Sample input and output:Enter the value for Room length:100Enter the value for Room breadth:200Length =100.0Breadth=200.0Area=20000.0Passing only length of the room with breadth=100Length=100.0Breadth=100.0Area=10000.0

    Result:Thus the java program was executed successfully and the output was

    verified.

  • 8ExNo: 2COMMAND LINE ARGUMENT

    Aim:To write a java program to get and sort names by command line argument.

    Algorithm: Create a class name ex2 and declare the variables int n and string s[] Allocate the memory of names for variable s. Get total number of names to be entered in the variable n. Using compareTo function sort the names in alphabetical order. Use forloop to sort the name. Print the sorted name list.

    Source code:import java.io.*;import java.lang.*;public static void main(String args[])throws IOException{String t;int n=args.length;int i,j;System.out.println(\n you have entered+n+names to sort it in ascendingorder);

    for(i=0;i

  • 9}}

    }System.out.println(\n sorted name list is\n);for (i=0;ijava ex2 one two three four five six seven eight nine ten eleven twelveYou have entered 12 names to sort it in ascending orderSorted name list isEightElevenFiveFourNineOneSevenSixTenThreeTwelvetwoResult:

    Thus the java program was executed successfully and the output wasverified

  • 10

    ExNo: 3BITWISE OPERATOR

    Aim:To write a java program to understand the concept of functionalities of different

    Bitwise operators.Algorithm:

    Create a class called ex3. Use DataInputStream to get stream of data. Display the menu and get choice from the user. Now,call the appropriate care. Display the output.

    Source code:import java.io.*;public class ex3{public static void main(String args[])throws IOException{DataInputStream d=new DataInputStream(System.in);System.out.println(\n enter value for x:);int x=Integer.parseInt(d.readLine());System.out.println(\n enter value for y:);int y= Integer.parseInt(d.readLine());int c;do{

    System.out.println(\n BITWISE OPERATOR\n);System.out.println(1.AND);System.out.println(2.OR);System.out.println(3.XOR);

  • 11

    System.out.println( 4.LEFT SHIFT);System.out.println( 5.RIGHT SHIFT);System.out.println( 6.NOT);System.out.println( 7.EXIT\n);System.out.println( \n enter your choice:);c=Integer.parseInt(d.readLine());switch(c){case 1:System.out.println(AND OPERATION:+(x+y));break;case 2:System.out.println(OR OPERATION:+(x|y));break;case 3:System.out.println(XOR OPERATION:+(x^y));break;case 4:

    System.out.println(LEFT SHIFT:+(xy));break;

    case 6:System.out.println(NOT of x :+(~x));System.out.println(NOT of y :+(~y));break;

    case 7:System.exit(1);

  • 12

    break;default:System.out.println(\n enter only 1 to 7);break;}}while(c!=7);}}Sample input and output:Enter value for x:10Enter value for y:6BITWISE OPERATOR1.AND2.OR3.XOR4.LEFT SHIFT5.RIGHT SHIFT6.NOT7.EXITEnter your choice:1AND OPERATION:2BITWISE OPERATOR1.AND2.OR3.XOR4.LEFT SHIFT5.RIGHT SHIFT6.NOT7.EXIT

  • 13

    Enter your choice:2OR OPERATION:14BITWISE OPERATOR1.AND2.OR3.XOR4.LEFT SHIFT5.RIGHT SHIFT6.NOT7.EXITEnter your choice:3XOR OPERATION:12BITWISE OPERATOR1.AND2.OR3.XOR4.LEFT SHIFT5.RIGHT SHIFT6.NOT7.EXITEnter your choice:4LEFT SHIFT OPERATION:640BITWISE OPERATOR1.AND2.OR3.XOR4.LEFT SHIFT5.RIGHT SHIFT6.NOT

  • 14

    7.EXITEnter your choice:5RIGHT SHIFT OPERATION:0

    BITWISE OPERATOR1.AND2.OR3.XOR4.LEFT SHIFT5.RIGHT SHIFT6.NOT7.EXIT

    Enter your choice:6NOT of x :-11NOT of y :-7

    BITWISE OPERATOR1.AND2.OR3.XOR4.LEFT SHIFT5.RIGHT SHIFT6.NOT7.EXIT

    Enter your choice:7

    Result:Thus,the java program has been written to understand the concept of functionalities

    of different bitwise operators and output were verified.

  • 15

    ExNo: 4METHOD OVERRIDING

    Aim:To write a java program to understand the concept of Method Overriding.

    Algorithm: Create a base class vehicle and declare the variable reg_no as string

    and model as integer. Create a function read()inside the class to read data for the variables. Create a derived classtwo-wheeler and declare the variable

    no_gear,power as integer. Create another deriver classscooter and declare variable

    manufacturer,owner as string. Create a function read()to get the data for no_gear. Create another function print that prints the values of the

    reg_no,model,power,no_gearSource code:import java.io.*;class vehicle{String reg_no;int model;void read()throws IOException{DataInputStream d=new DataInputStream(System.in);System.out.println(\n enter reg_no and model :);reg_no=d.readLine();model=Integer.parseInt(d.readLine());}

    }class two-wheeler extends vehicle

  • 16

    {int no_gear,power;void read()throws IOException{super.read();DataInputStream d=new DataInputStream(System.in);System.out.println(\n enter no_gear and power :);no_gear=Integer.parseInt(d.readLine());power= Integer.parseInt(d.readLine());}

    }class scooter extends two-wheeler{String manufacturer,owner;void read()throws IOException{super.read();DataInputStream d=new DataInputStream(System.in);System.out.println(\n enter manufacturer and owner :);manufacturer=d.readLine();owner= d.readLine();}

    void print(){System.out.println(\n);System.out.println(\n Reg_no :+reg_no);System.out.println(\n Model :+model);System.out.println(\n No_gear :+no_gear);System.out.println(\n Power :+power);

  • 17

    System.out.println(\n Manufacturer :+manufacturer);System.out.println(\n Owner :+owner);}}class overriding{public static void main(String args[])throws IOException{scooter s=new scooter();s.read();s.print();}

    }Sample input and output:Enter reg_no and model:TN31AD1224 2010Enter no_gear and power:5 798Enter manufacturer and owner : Maruthi Sauresh.AReg_no : TN31AD1224Model : 2010No_gear : 5Power : 798Manufacturer : MaruthiOwner : Sauresh.A

    Result:Thus, the java program has been written to understand the concept of Method

    Overriding and output was verified.

  • 18

    ExNo: 5PACKAGES

    Aim:To write a java program to understand the steps in the creation of packages.

    Algorithm: Include the package complex. Create a class arith and declare rp,ip as integer. Define the function arith(),set rp and ip to 0 Another function arith(int rp,int ip) set this.rp=rp and this.ip=ip. Create a function add() pass arguments with arith a1 and arith a2. Add a1.rp and a2.rp store in rp. Similarly add a1.ip and a2.ip and store in ip. Create a function sub(arith a1,arith a2) Subtract a1.rp and a2.rp store it in rp Subtract a1.ip and a2.ip store it in ip

    Source code:Package creation code:package pkg;public class arith{public int rp,ip;public arith(){rp=0;ip=0;}

    public arith(int rp,in rip){this.rp=rp;this.ip=ip;

  • 19

    }public void add(arith a1,arith a2){rp=a1.rp + a2.rp;ip=a1.ip +a2.ip;

    }public void sub (arith a1,arith a2){rp=a1.rp - a2.rp;ip=a1.ip -a2.ip;

    }}

    Main code:import pkg.arith;import java.io.*;class package1{public static void main(String args[])throws IOException{int rp,ip;DataInputStream d=new DataInputStream(System.in);System.out.println(\n enter real part and imaginary part of 1st complex no :);rp=Integer.parseInt(d.readLine());ip= Integer.parseInt(d.readLine());arith a1=new arith(rp,ip);System.out.println(\n enter real part and imaginary part of 2nd complex no :);rp=Integer.parseInt(d.readLine());ip= Integer.parseInt(d.readLine());arith a2=new arith(rp,ip);

  • 20

    arith a3=new arith();System.out.println(\n a1=+a1.rp+ + +a1.ip+ i);System.out.println(\n a2=+a2.rp+ + +a2.ip+ i);a3.add(a1,a2);System.out.println(\n Added value: +a3.rp+ + +a3.ip+ i);a3.sub(a1,a2);System.out.println(\n Subtacted value : +a3.rp+ - a3.ip + i);}}Sample input and output:Enter real part and imaginary part of 1st complex no : 10 5Enter real part and imaginary part of 2nd complex no : 3 6

    a1= 10 + 5ia2= 3 +6i

    Added value : 13 +11iSubtracted value : 7 1i

    Result:Thus, the java program has been written to create a package and output were

    verified by importing it in another program

  • 21

    Ex.No : 6INTERFACE

    Aim:To write java program to implement the concept of interface.

    Algorithm: Create a class income,declare iamount as integer. Create a function get() to get the values for the variable. Create a class expenditure and declare the function get1(). Create a derived class net_income that extends income and implements

    expenditure. Declare variables eamount,namount as float. Create a function print() to print the values. Create a main class and create an object for net_income. Now,get the values using n.print(). End.

    Source code:import java.io.*;class income{float iamount;void get()throws IOException{DataInputStream d=new DataInputStream(System.in);System.out.println(\n enter the income :);iamount=Float.pareseFloat(d.readLine());}

    }interface expenditure{

  • 22

    public void get1();}class net_income extends income implements expenditure{float eamount,namount;public void get1(){try{DataInputStream d=new DataInputStream(System.in);System.out.println(\n enter expenditure:);eamount=Float.parseFloat(d.readLine());}

    Catch(IOException e){}

    }

    void print(){System.out.println(\n income : +iamount);System.out.println(\n expenditure : +eamount);System.out.println(\n net income : +(iamount-eamount));}}

    class interface6{public static void main(String args[])throws IOException{

  • 23

    net_income n=new net_income();n.get();n.get1();n.print();

    }}

    Sample input and output:Enter income : 1000Enter expenditure : 200

    Income : 1000.0Expenditure : 200.0Net income : 800.0

    Result:Thus, the java program was created and executed successfully and the output is

    verified.

  • 24

    ExNo : 7INHERITANCE

    Aim:To write a java program to handle the situation of exception multi inheritance.

    Algorithm: Create a class student and declare variable rollno. Create function getnumber() and display(). Create another class test which inherit student. Create function display(). Create another interface sports with variable sport and function putsp(). Create variable total and function putsp(),display(). Create another class result which extends the test and sport. Create main class inhetitance7 and which has the student as object of class

    result.Source code:import java.io.*;class student{int roll_no;void getnumber(int n){roll_no=n;}

    void display()

  • 25

    {System.out.println(\n Rollno: +roll_no);}}class test extends student{int m1,m2;void getmarks(int p,int q){m1=p;m2=q;}

    void display(){System.out.println(\n Rollno: +roll_no);System.out.println(\n Marks Achieved);System.out.println(\n Mark1 : +m1);System.out.println(\n Mark2 : +m2);}}interface sports{float sport=6.0f;

  • 26

    void putsp();}class results extends test implements sports{float total;public void putsp(){System.out.println(\n sports result =+sport);}

    void display(){total=m1+m2+sport;display();putsp();System.out.println(\n total score = +total);}}class inheritance7{public static void main(String args[]){result student1 = new results();student1.getnumber(1256);

  • 27

    student1.getmarks(30,40);student1.display();

    }}Sample input and output:Rollno :1256Marks AchievedMarks1 = 30Marks2 =40Sports result =6.0Total score = 76.0

    Result:Thus, the java program was created and executed successfully and output is verified.

  • 28

    ExNo : 8EXCEPTION HANDLING

    Aim:To write a java program to implement the concept of exception handling.

    Algorithm: Create a class exception8 Declare the integer a,b,c inside the class. Store length of the argument in a and get the variable for b. Calculate c=b/a. Print the value of a,b,c. Declare variable d[] = new int [a-2]. End.

    Source code:import java.io.*;class exception8{public static void main(String args[])throws IOException{DataInputStream din= new DataInputStream(System.in);int a,b,c;try{a=args.length;

    System.out.println(\n enter b value :);b= Integer.pareseInt(din.readLine());

  • 29

    c=b/a;System.out.println(\n a=+a);System.out.println(\n b= +b);System.out.println(\n b/a = +c);int d[] = new int[a-2];d[42]=99;}catch(ArithmeticException e3){System.out.println(e3);

    }catch(NegativeArraySizeException e2){System.out.println(e2);}catch(ArrayIndexOutOfBoundsException e1){System.out.println(e1);}catch(NumberFormatException e){System.out.println(e);}

  • 30

    Sample input and output:C:\>java exception8Enter b value:1java.lang. ArithmeticException:/by zeroC:\>java exception8 3Enter b value: 4a = 1b = 4b/a = 4java.lang. NegativeArraySizeExceptionC:\>java exception8 3 5Enter b value: 5a = 2b = 5b/a =2java.lang. ArrayIndexOutOfBoundsException: 42C:\>java exception8 3 5Enter b value: tjava.lang. NumberFormatException: for input string : t.Result:

    Thus, the java program was created and executed successfully and output is verified.

  • 31

    ExNo : 9USER DEFINED EXCEPTION HANDLING

    Aim:To write a java program to implement the concept of user defined exception.

    Algorithm: Create a class myexception that extends exception ,create variable and function

    to read and return a message. Create another class exception , create variable eno ,ename ,job ,count ,as static

    int and string. Create a function display() to display the employee details. The main function call the get() and display() function to red and print the

    values.Source code:import java.io.*;import java.lang.*;class MyException extends Exception{String msg;public MyException(String message){msg = message;}

    public StringtoString(){return msg;

  • 32

    }}class exception9{static int eno[] = new int[];static String ename[] = new String[3];static String job[] = new String[3];static int sal[] =new int[3];static int count = 0;static void get()throws IOException{int i;DataInputStream d = new DataInputStream(System.in);try{for(i=count;i

  • 33

    System.out.println(\n enter job and salary :);job[i] = d.readLine();sal[i] = Integer.parseInt(d.readLine());if(sal[i]

  • 34

    get();System.out.println(\n ENO\t\t ENAME\t\t JOB\t\t SALARY\n);disp();}}Sample input and output:Enter 1 employee details:Enter employee no and name : -5554Exception caught : enter valid numberEnter 1 employee detail :Enter employee no and name : 3001R.HaricharanEnter job and salary : lecturer 25000Enter 2 employee detail :Enter employee no and name : 5001S.KalaiselvanEnter job and salary : reader

    20000Enter 3 employee detail :Enter employee no and name : 7801S.RajaduraiEnter job and salary : director23000

  • 35

    ENO ENAME JOB SALARY3001 R.Haricharan lecturer 250005001 S.Kaliselvan reader 200007801 S.Rajadurai director 23000

    Result:Thus, the java program was created and executed successfully and output is verified.

  • 36

    ExNo : 10MULTI THREADING join() and isAlive()

    Aim:To create a java program in a multithread environment and implement join() and

    isAlive() functions.Algorithm:

    Create a class table that is derived from Thread. Declare name as string. Create a constructor table , pass thread name. Call super(threadname) , start() , start the thread. Create a function run() , print the multiples of 5. Create another class fseries that extends thread. Declare name as string and t1,t2,t3 as int and set f1=-1,f2=1. Create a constructor fseries pass thread name call super thread name() In the run() function display the Fibonacci series. Create a class multi in the main function , create object f for table. f.join() waits for thread to find create object f for fseries and do f.join().

    Source code:import java.io.*;class table extends Thread{String name;table(String threadname){super(threadname);

  • 37

    name = threadname;System.out.println(\n New thread : +this);start();}

    public void run(){System.out.println(\n Five Table);for(int i=0;i

  • 38

    }public void run(){System.out.println(\n Fibonacci Series);for(int i= 1;i

  • 39

    fseries f = new fseries(Fibonacci Series);System.out.println(\n Second thread is alive : +f.isAlive());}catch(InterruptedException e){System.out.println(\n Main thread is interrupted);}System.out.println(\n Main thread is exiting);}

    }Sample input and output:New thread : Thread[Five Table , 5, main]First thread is alive : trueFive Table1*5=52*5=103*5=154*5=205*5=256*5=307*5=358*5=409*5=45

  • 40

    10*5=50First thread is alive : false

    New thread [Fibonacci Series , 5, main]Fibonacci seriesSecond thread is alive : true01123Second thread is alive : falseMain thread is exiting.

    Result:Thus, the java program was created and executed successfully and output is verified.

  • 41

    ExNo : 11MULTI THREADING : INTER-THREAD COMMUNICATON

    Aim:To write a java program to implement the concept of inter-thread communication.

    Algorithm: creating a class q and declare variable n as int and declare value set(Boolean) as

    false. Create synchronized method in get(). Create another synchronized method void put(int n). If (value set) then go to try and catch . inside catch call notify() this tells

    produces that it away put more about in the queue. Create a class producer , that implements runnable. Create object q. create a constructor producer where object is passed. Create a function run() , initialize and declare i= 0. Create a class consumer that implements runnable. Create constructor consumer , pass the object and create a function run(). Create a class multi2 inside the main function and create object q.

    Source code:import java.io.*;class consumer implements Runnable{Stock c;Thread t;Consumer(Stock c){this.c=c;

  • 42

    t = new Thread(this , producer thread);t.start();}public void run(){while(true){try{t.sleep(750);}catch(InterrupedException e){}c.getstock((int) (Math.random() * 100);

    }}void stop(){t.stop();

    }}class producer implements Runnable{

  • 43

    Stock s;Thread t;producer(Stock s){this.s=s;t= new Thread(this , producer thread);t.start();}public void run(){while(true){try{t.sleep(750);}

    catch(InterrupedException e){}c.getstock((int) (Math.random() * 100);}

    }void stop(){

  • 44

    t.stop();}}class stock{int goods = 0;public synchronized void addstock(int i){goods = goods +i ;System.out.println(\n stock added : +i);System.out.println(\n present stock : +goods);notify();

    }public synchronized int getstock(int j){while(true){if(goods>=j){goods = goods-j;System.out.println(\n stock taken away : +j);System.out.println(\n present stock : +goods);break;

  • 45

    }else{System.out.println(\n stock not enough);System.out.println(\n waiting for stock to come..);try{wait();}catch(InterrupedException e){}

    }}return goods;}public static void main(String args[]){stock j = new stock();producer p = new producer (j);consumer c =new consumer (j);try{Thread.sleep(20000);

  • 46

    p.stop(); c.stop();p.t.join();c.t.join();System.out.println(\n Thread stopped);}catch(InterrupedException e){}System.exit(0);}}Sample input and output:Stock added : 58Present stock : 58Stock taken away : 32Present stock : 26Stock not enough..Waiting for stock to come..Stock added : 15Present stock : 41Stock not enough Waiting for stock to come ..Thread stopped.Result:Thus, the java program was created and executed successfully and output is verified.

  • 47

    Ex.No:12ADDITION OF TWO NUMBERS USING APPLET

    Aim:To write a java program to implement applet concept.

    Algorithm:1. Start2. Create a object for input stream class3. Compile the program in java4. Run the program using applet viewer5. Enter the values6. End.

    Source Code:import java.awt.*;import java.applet.*;public class userin extends applet{TextField text1,text2;public void init(){text1=new TextField(8);text2=new TextField(8);add(text1);add(text2);

  • 48

    text1.setText("0");text2.setText("0");}public void paint(Graphics g){int x=0,y=0,z=0;String s1,s2,s;g.drawString("input a number in each box",10,50);try{s1=text1.getText();x=Integer.parsseInt(s1);s2=text2.getText();y=Integer.parseInt(s2);}catch(Exception ex){}z=x+y;s=String.valueOf(z);g.drawString("the sum is:",10,75);g.drawString(s,100,75);}public boolean action(Event event,Object object)

  • 49

    {repaint();return true;}/**/}Execution Method:C:\j2sdk1.4.1_06\bin>javac userin.javaNote:userin.java uses or overrides a deprecated APINote:recpmpile with-deprecation for detailsC:\j2sdk1.4.1_06\bin>appletviewer userin.java

    Result:Thus the java program to add two numbers using applet was created and executed

    successfully and the output was verified.

  • 50

    Ex.No:13JOB SCHEDULING TECHNIQUES

    Aim:To write a java program to implement the concept of different job scheduling

    techniques like,a) First Come First Served[FCFS]b) Shortest Job First[SJF]c) Round Robind) Priority Scheduling

    Algorithm:a) First Come First Served:

    1. Get the number by jobs to be scheduled2. Get the arrival time and service time of each and every job3. Get the job according time as zero for each jobs4. Initialize the waiting time as zero for each jobs5. Compute each job and find the average waiting by dividing

    total compute each job and find number of jobsb) Shortest Job First:

    1. Get the number of jobs to be scheduled2. Get the arrival time and service time of each job3. sort the job according to its service time4. Intialize the waiting time as zero for each job

    c) Round Robin:

  • 51

    1. Get the number of jobs to be scheduled2. Get the arrival time ,service time and timeslice of each job3. Sort the job according to its time of the arrival4. Repeat the above steps intill all the jobs are completely

    servicedd) Priority scheduling:

    1. Get the number of jobs to be scheduled2.Get the arrival time, service time and priority of every jobs3. Initialize the waiting time as zero for each jobs4. While processing the jobs according to the started time andthe service time of the jobs being processed currently to thewaiting time for each job5. Compute the total waiting time by adding individual waiting

    time of the each job and find the arrange time of jobSource Code for FCFS:import java.io.*;class schedule{int hr,min,sec,ser,wait;String name;DataInputStream d=new DataInputStream(System.in);void get()throws IOException{

  • 52

    System.out.println("ENTER PROCESS NAME:");name=d.readLine();System.out.println("ENTER THE HOUR:");hr=Integer.parseInt(d.readLine());if(hr

  • 53

    else{System.out.println("ENTER THE MINUTES

  • 54

    schedule s[],t;DataInputStream d=new DataInputStream(System.in);System.out.println("ENTER THE NUMBER OF PROCESSES:");int n=Integer.parseInt(d.readLine();s=new schedule[10];t=new schedule();for(i=0;is[j].sec){

  • 55

    t=s[i];s[i]=s[j];s[j]=t;}else if(s[i].hr==s[j].hr&&s[i].min>s[j].min&&s[i].sec>s[j].sec){t=s[i];s[i]=s[j];s[j]=t;}}}System.out.println("\nPROCESS\tSTARTING TIME\t SERVICE TIME\tWAITINGTIME\n");for(i=0;i

  • 56

    System.out.println("\nTOTAL WAITING TIME""+total);System.out.println("\nAVERAGE WAITING TIME:"+(total/n));}}Source Code for SJF:import java.io.*;class schedule{int hr,min,sec,ser,wait;int tot;String name;DataInputStream d=new DataInputStream(System.in);void get()throws IOException{System.out.println("ENTER PROCESS NAME:");name=d.readLine();System.out.println("ENTER THE HOUR:");hr=Integer.parseInt(d.readLine());if(hr

  • 57

    {System.out.println("ENTER THE SECONDS:");sec=Integer.parseInt(d.readLine());if(sec

  • 58

    hr=Integer.parseInt(d.readLine());}tot=hr+min+sec;}void put(){System.out.println(name+"\t"+hr+":"+min+":"+sec+"\t\t\t"+ser+"\t\t\t"+wait);}}class sjf{public static void main(String args[])throws IOException{int i,j;float total=0.0f;schedule s[],t;DataInputStream d=new DataInputStream(System.in);System.out.println("ENTER THE NUMBER OF PROCESSES:"):int n=Integer.parseInt(d.readLine());s=new schedule[10];t=new schedule();for(i=0;i

  • 59

    s[i]=new schedule();s[i].get();}for(i=0;i

  • 60

    total+=s[i].wait;s[i].put();}System.out.println("\nTOTAL WAITING TIME:"+total);System.out.println("\nAVERAGE WAITING TIME:"+(total/n));}}Source Code for Round Robin:import java.io.*;class schedule{int hr,min,sec,ser,wait;String name;DataInputStream d=new DataInputStream(System.in);void get()throws IOException{System.out.println("ENTER PROCESS NAME:");name=d.readLine();System.out.println("ENTER THE HOUR:");hr=Integer.parseInt(d.readLine());if(hr

  • 61

    min=Integer.parseInt(d.readLine());if(min

  • 62

    {System.out.println("ENTER THE HOURS

  • 63

    t=new schedule();for(i=0;is[j].min&&s[i].sec>s[j].sec)

  • 64

    {t=s[i];s[i]=s[j];s[j]=t;}}}System.out.println("enter the time slice:");ts=Integer.parseInt(d.readLine());max=0;for(i=0;i

  • 65

    m1=tser[j]-ts;if(m1>0){mod[i][j]=ts;tser[j]=tser[j]-ts;}else{mod[i][j]=tser[j];tser[j]=0;}}elsemod[i][j]=0;}}for(k=0;k

  • 66

    {for(j=0;j

  • 67

    }}Source Code for priority scheduling:import java.io.*;class schedule{int hr,min,sec,ser,wait,pr;int tot;String name;DataInputStream d=new DataInputStream(System.in);void get()throws IOException{System.out.println("ENTER PROCESS NAME:");name=d.readLine();System.out.println("ENTER THE HOUR:");hr=Integer.parseInt(d.readLine());if(hr

  • 68

    sec=Integer.parseInt(d.readLine());if(sec

  • 69

    hr=Integer.parseInt(d.readLine());}tot=hr+min+sec;}void put(){System.out.println(name+"\t"+hr+":"+min+":"+sec+"\t\t\t"+ser+"\t\t\t"+wait);}}class pri{public static void main(String args[])throws IOException{int i,j;float total=0.0f;schedule s[],t;DataInputStream d=new DataInputStream(System.in);System.out.println("ENTER THE NUMBER OF PROCESSES:"):int n=Integer.parseInt(d.readLine());s=new schedule[10];t=new schedule();for(i=0;i

  • 70

    s[i]=new schedule();s[i].get();}for(i=0;i

  • 71

    total+=s[i].wait;s[i].put();}System.out.println("\nTOTAL WAITING TIME:"+total);System.out.println("\nAVERAGE WAITING TIME:"+(total/n));}}Sample Input and Output:FCFS:ENTER THE NUMBER OF PROCESSES:4ENTER PROCESS NAME:P1ENTER THE HOUR:2ENTER THE MINUTES:5ENTER THE SECONDS:3ENTER THE SERVICE TIME:1ENTER THE PROCESS NAME:P2

  • 72

    ENTER THE HOUR:4ENTER THE MINUTES:6ENTER THE SECONDS:3ENTER THE SERVICE TIME:4ENTER PROCESS NAME:P3ENTER THE HOUR:8ENTER THE MINUTES:3ENTER THE SECONDS:2ENTER THE SERVICE TIME:5ENTER PROCESS NAME:P4ENTER THE HOUR:6ENTER THE MINUTES:

  • 73

    3ENTER THE SECONDS:1ENTER THE SERVICE TIME:6PROCESS STARTING TIME SERVICE TIME WAITING TIME

    P1 2:5:3 1 0P2 4:6:3 4 1P4 6:3:1 6 5P3 8:3:2 5 11

    TOTAL WAITING TIME:17.0AVERAGE WAITING TIME:4.25SJF:ENTER THE NUMBER OF PROCESSES:4ENTER PROCESS NAME:P1ENTER THE HOUR:4ENTER THE MINUTES:5ENTER THE SECONDS:3

  • 74

    ENTER THE SERVICE TIME:2ENTER THE PROCESS NAME:P2ENTER THE HOUR:2ENTER THE MINUTES:1ENTER THE SECONDS:0ENTER THE SERVICE TIME:4ENTER PROCESS NAME:P3ENTER THE HOUR:3ENTER THE MINUTES:5ENTER THE SECONDS:2ENTER THE SERVICE TIME:7ENTER PROCESS NAME:

  • 75

    P4ENTER THE HOUR:5ENTER THE MINUTES:2ENTER THE SECONDS:1ENTER THE SERVICE TIME:4PROCESS STARTING TIME SERVICE TIME WAITING TIME

    P1 4:5:3 2 0P2 2:1:0 4 2P4 5:2:1 4 6P3 3:5:2 7 10

    TOTAL WAITING TIME:18.0AVERAGE WAITING TIME:4.5Round Robin:ENTER THE NUMBER OF PROCESSES:4ENTER PROCESS NAME:P1ENTER THE HOUR:1

  • 76

    ENTER THE MINUTES:1ENTER THE SECONDS:1ENTER THE SERVICE TIME:1ENTER THE PROCESS NAME:P2ENTER THE HOUR:2ENTER THE MINUTES:2ENTER THE SECONDS:2ENTER THE SERVICE TIME:2ENTER PROCESS NAME:P3ENTER THE HOUR:3ENTER THE MINUTES:3ENTER THE SECONDS:

  • 77

    3ENTER THE SERVICE TIME:3ENTER PROCESS NAME:P4ENTER THE HOUR:4ENTER THE MINUTES:4ENTER THE SECONDS:4ENTER THE SERVICE TIME:4Enter the time slice:3PROCESS STARTING TIME SERVICE TIME WAITING TIME

    P1 1:1:1 1 0P2 2:2:2 2 1P3 3:3:3 3 6P4 4:4:4 4 6

    TOTAL WAITING TIME:13.0AVERAGE WAITING TIME:3.25

  • 78

    Priority Scheduling:ENTER THE NUMBER OF PROCESSES:4ENTER PROCESS NAME:P1ENTER THE HOUR:1ENTER THE MINUTES:2ENTER THE SECONDS:3ENTER THE SERVICE TIME:4ENTER THE PRIORITY:3ENTER THE PROCESS NAME:P2ENTER THE HOUR:2ENTER THE MINUTES:5ENTER THE SECONDS:6

  • 79

    ENTER THE SERVICE TIME:3ENTER THE PRIORITY:2ENTER PROCESS NAME:P3ENTER THE HOUR:6ENTER THE MINUTES:2ENTER THE SECONDS:1ENTER THE SERVICE TIME:6ENTER THE PRIORITY:1ENTER PROCESS NAME:P4ENTER THE HOUR:7ENTER THE MINUTES:3ENTER THE SECONDS:

  • 80

    1ENTER THE SERVICE TIME:5ENTER THE PRIORITY:4PROCESS STARTINGTIME SERVICETIME WAITINGTIME PRIORITY

    P3 6:2:1 6 0 1P2 2:5:6 3 6 2P1 1:2:3 4 9 3P4 7:3:1 5 13 4

    TOTAL WAITING TIME:28.0AVERAGE WAITING TIME:7.0

    Result:Thus the java program to implement the job scheduling techniques was

    created,executed successfully and the output was verified.

  • 81

    Ex.No:14DISK SCHEDULING TECHNIQUES

    Aim:To write a java program to implement the concept of different disk scheduling

    techniques like,a)First Come First Server[FCFS]b)Shortest Seek Time First[SSTF]c)Scand)C-Scane)Lookf)C-Look

    Algorithm:a) First Come First Serverd:

    Move the head according to the requestb) Shortest Seek Time First:

    Move the disk arm to the request position from the current position inwardor outward that maintain minimizes arm movements

    c) Scan:1.Move the disk arm to the requested position in the path2.Change the direction when there are no more request to service in

    the count directiond) C-Scan:

    Move the disk arm to the requested position which moving towardsinward and direction

  • 82

    e) Look:The arm goes only as far as the final request in each direction then it reverse it

    directionf) C-Look:

    1. Move head from current position2. Process the request untill the condition is satisfied

    Source Code for FCFS:import java.io.*;import java.lang.*;class disk_fcfs{public static void main(String args[])throws IOException{int i,sum=0,n,st;int b[],dd[],a[];a=new int[20];b=new int[20];dd=new int[20];DataInputStream d=new DataInputStream(System.in);do{System.out.println("Enter the block number between 0 and 200:");st=Integer.parseInt(d.readLine());

  • 83

    }while((st>=200)||(st

  • 84

    {System.out.println("\t"+a[i]);System.out.println("\n\nACCESS ORDER:");for(i=0;i

  • 85

    dd=new int[20];DataInputStream d=new DataInputStream(System.in);do{System.out.println("Enter the block number between 0 and 200:");st=Integer.parseInt(d.readLine());}while((st>=200)||(st

  • 86

    }for(i=0;i0){j=1;min=Math.abs(dd[0]=dd[1]);for(i=2;i

  • 87

    for(z=j;z

  • 88

    public static void main(String args[])throws IOException{int i,j,c=0,sum=0,n,st,temp,t;int b[],dd[],a[];a=new int[20];b=new int[20];dd=new int[20];DataInputStream d=new DataInputStream(System.in);do{System.out.println("Enter the block number between 0 and 200:");st=Integer.parseInt(d.readLine());}while((st>=200)||(st

  • 89

    a[i]=Integer.parseInt(d.readLine());if((a[i]>200)||(a[i]200)||(a[i]

  • 90

    for(j=i-1;j>=0;j--)b[++c]=dd[j];b[++c]=0;for(j=t;j

  • 91

    Source Code for C-Scan:import java.io.*;import java.lang.*;class disk_c_scan{public static void main(String args[])throws IOException{int i,j,sum=0,n,st,temp,t,c=0;int b[],dd[],a[];a=new int[20];b=new int[20];dd=new int[20];DataInputStream d=new DataInputStream(System.in);do{System.out.println("Enter the block number between 0 and 200:");st=Integer.parseInt(d.readLine());}while((st>=200)||(st

  • 92

    for(i=1;i200)||(a[i]200)||(a[i]

  • 93

    {if(st==dd[i]){t=i;b[c]=st;for(j=t+1;j>=n;j++)b[++c]=dd[j];b[++c]=199;b[++c]=0;for(j=0;j

  • 94

    }System.out.println("\n\n Total no.of head movements:"+sum);}}Source Code for Look:import java.io.*;import java.lang.*;class disk_look{public static void main(String args[])throws IOException{int i,j,c=0,sum=0,n,st,temp,t,s;int b[],dd[],a[];a=new int[20];b=new int[20];dd=new int[20];DataInputStream d=new DataInputStream(System.in);do{System.out.println("Enter the block number between 0 and 200:");st=Integer.parseInt(d.readLine());}while((st>=200)||(st

  • 95

    a[0]=st;System.out.println("\nEnter the no.of request:");n=Integer.parseInt(d.readLine());System.out.println("\nEnter request:\n");for(i=1;i200)||(a[i]200)||(a[i]

  • 96

    temp=dd[i];dd[i]=dd[j];dd[j]=temp;}for(i=0;i=0;j--)b[++c]=dd[j];b[++c]=200;for(j=n;j>i;j--)b[++c]=dd[j];}}System.out.println("\n\n\t\tLOOK TECHNIQUE:");System.out.println("\nDISK QUEUE:");for(i=0;i

  • 97

    System.out.println("\t"+b[i]);if(i!=c)sum+=Math.abs(b[i]-b[i+1]);}System.out.println("\n\n Total no.of head movements:"+sum);}}Source Code for C-Look:import java.io.*;import java.lang.*;class disk_c_look{public static void main(String args[])throws IOException{int i,j,c=0,sum=0,n,st,temp,t,s;int b[],dd[],a[];a=new int[20];b=new int[20];dd=new int[20];DataInputStream d=new DataInputStream(System.in);do{System.out.println("Enter the block number between 0 and 200:");

  • 98

    st=Integer.parseInt(d.readLine());}while((st>=200)||(st

  • 99

    for(j=i+1;jdd[j]){temp=dd[i];dd[i]=dd[j];dd[j]=temp;}for(i=0;i

  • 100

    System.out.println("\n\nACCESS ORDER:");for(i=0;i

  • 101

    ACCESS ORDER : 53 123 45 20 100 199Total no.of head movements : 352SSTF:Enter the block number between 0 and 200:53Our disk head is on the 53th blockEnter the no.of request:5Enter Request:Enter 1 Request:123Enter 2 Request:45Enter 3 Request:20Enter 4 Request:100Enter 5 Request:199

    SHORTEST SEEK TIME FIRSTDISK QUEUE : 53 123 45 20 100 199ACCESS ORDER : 53 45 20 100 123 199Total no.of head movements : 212SCAN:Enter the block number between 0 and 200:53Our disk head is on the 53th blockEnter the no.of request:5Enter Request:Enter 1 Request:45Enter 2 Request:20

  • 102

    Enter 3 Request:123Enter 4 Request:100Enter 5 Request:199

    SCAN TECHNIQUE:DISK QUEUE : 53 45 20 123 100 199ACCESS ORDER : 53 45 20 0 100 123 199Total no.of head movements : 252C-SCAN:Enter the block number between 0 and 200:53Our disk head is on the 53th blockEnter the no.of request:5Enter Request:Enter 1 Request:123Enter 2 Request:45Enter 3 Request:20Enter 4 Request:100Enter 5 Request:199

    C-SCAN TECHNIQUE:DISK QUEUE : 53 123 45 20 100 199ACCESS ORDER : 53 100 123 199 199 0 20 45Total no.of head movements : 390LOOK:Enter the block number between 0 and 200:53

  • 103

    Our disk head is on the 53th blockEnter the no.of request:5Enter Request:Enter 1 Request:123Enter 2 Request:45Enter 3 Request:20Enter 4 Request:100Enter 5 Request:199

    LOOK TECHNIQUE:DISK QUEUE : 53 123 45 20 100 199ACCESS ORDER : 53 45 20 200 100 123 100Total no.of head movements : 313C-LOOK:Enter the block number between 0 and 200:53Our disk head is on the 53th blockEnter the no.of request:5Enter Request:Enter 1 Request:123Enter 2 Request:45Enter 3 Request:20Enter 4 Request:100Enter 5 Request:199

  • 104

    C-LOOK TECHNIQUE:DISK QUEUE : 53 123 45 20 100 199ACCESS ORDER : 53 100 123 199 20 45Total no.of head movements : 350

    Results:Thus the java program to implement the disk scheduling techniques was created,

    executed successfully and the output was verified.

  • 105

    Ex.No:15MEMORY ALLOCATION TECHNIQUES

    Aim:To write a java program to implement the concept of different memory allocation

    techniques like,a) First Fitb) Best Fitc) Worst Fitd) Next Fit

    Algorithm:a) First Fit

    1. Get thenumber of process to be allocated2. Get the number of free blocks available and its size3. Find the first free block from the starting, so that the new

    process can allocated fully in that blockb) Best Fit

    1. Get the number of process to be allocated2. Get the number of free blocks available and its size3. Repeat the above steps, so that all the process can be allocated

    c) Worst Fit1. Get the number of process to be allocated2. Get the number of free blocks available and size3. Get the memory size of the process to be allocated

  • 106

    4. Repeat the above steps, so that all the process can be allocatedd) Next Fit

    1. Get the number of process to be allocated2. Get the memory of free blocks available and its size3. Get the memory size of the process to be allocated4. Find the first free block from the starting, so that the new

    process can be allocated in that block.Source Code:import java.lang.*;import java.io.*;class MALLOCATION{static int f1,p,next,c,l;static int bsize[]=new int[30];static int fsize[]=new int[30];static int f1size[]=new int[30];static int asize[]=new int[30];static void firstFit(int n){int k=0,i=1;for(i=0;i=n)

  • 107

    {asize[i]=asize[i]+n;next=i+1;System.out.println("\nMEMORY ALLOCATION IN BLOCK:"+i);int s1=50;for(l=0;l

  • 108

    if((fsize[i]-n)>=0){s=fsize[i]-n;if(s

  • 109

    }}if(k==0)System.out.println("\n No matching block for:"+n);System.out.println();}public static void main(String args[]){try{BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));char ch;int var=1;int b1size,i;next=0;System.out.println("\n\t\tMEMORY ALLOCATION TECHNIQUES");System.out.println("\n\t\t*************************************");System.out.println("Output for memory allocation techniques");System.out.println("\nEnter the number of free block:");f1=Integer.parseInt(ob.readLine());int sum=50;System.out.println("Enter within the width 480");System.out.println(Enter width within the limit");

  • 110

    for(i-0;i481){System.out.println("\nExecuting the limit, re-enter the value!");System.out.println();continue;}}}if(k!=0){next=k;fsize[k-1]=min1;asize[k-1]+=n;int s1=50;for(l=0;l

  • 111

    elseSystem.out.println("\n No matching block for:"+n);}static void worstFit(int n){int l,s,k-0;int max1=0;for(int i=0;i0){s=fsize[i]-n;if(s>=max1){max1=s;k=i+1;}}}if(k!=0){next=k;fsize[k-1]=max1;

  • 112

    fsize[k-1]=asize[k-1]+n;int s1=50;for(l=0;l

  • 113

    while(var!=0){System.out.println("\n\t\tMEMORY ALLOCATION TECHNIQUES");System.out.println("\n\t\t*************************************");System.out.println("\n1.FIRST FIT\n2.BEST FIT\n3.WORST FIT\n4.NEXTFIT\n5.EXIT"):System.out.println("\nEnter your choice:");c=Integer.parseInt(ob.readLine());switch(c){case 1:for(i=0;i

  • 114

    for(i=0;i

  • 115

    catch(IOException e){System.out.println("\n Exception Raised");}catch(Exception e){}}}Sample Input and Output:

    MEMORY ALLOCATION TECHNIQUES*************************************

    Output for memory allocation techniquesEnter the number of free block:3Enter within the width 480Enter width within the limitEnter the size of the block 0:100Enter the size of the block 1:50Enter the size of the block 2:200How many no.of process:3Enter the size of allocated memory process 0:200Enter the size of allocated memory process 1:250

  • 116

    Enter the size of allocated memory process 2:100MEMORY ALLOCATION TECHNIQUES*************************************

    1.FIRST FIT2.BEST FIT3.WORST FIT4.NEXT FIT5.EXITEnter your choice:1MEMORY ALLOCATION IN BLOCK : 2Memory allocated for process : 200No matching block for : 250MEMORY ALLOCATION IN BLOCK : 0Memory allocated for process : 100Do you want to continue : Y

    MEMORY ALLOCATION TECHNIQUES*************************************

    1.FIRST FIT2.BEST FIT3.WORST FIT4.NEXT FIT5.EXITEnter your choice : 2

  • 117

    Memory allocated for process : 200Memory allocated in block : 2No matching block for : 250Memory allocated for process : 100Memory allocated in block : 0Do you want to continue : Y

    MEMORY ALLOCATION TECHNIQUES*************************************

    1.FIRST FIT2.BEST FIT3.WORST FIT4.NEXT FIT5.EXITEnter your choice : 3No matching block for : 200No matching block for : 250Memory allocated for process : 0Memory allocated in block : 2Do you want to continue : Y

    MEMORY ALLOCATION TECHNIQUES*************************************

    1.FIRST FIT2.BEST FIT

  • 118

    3.WORST FIT4.NEXT FIT5.EXITEnter your choice : 4No matching block for : 200No matching block for : 250No matching block for : 100Do you want to continue : Y

    MEMORY ALLOCATION TECHNIQUES*************************************

    1.FIRST FIT2.BEST FIT3.WORST FIT4.NEXT FIT5.EXITEnter your choice : 5Result:

    Thus the java program to implement the memory allocation techniques was created,executed successfully and the output was verified.

  • 119

    Ex.No:16MEMORY MANAGEMENT TECHNIQUES

    Aim:To write a java program to implement the concept of different memory

    management techniques like,1.Paging2.Demand paging

    Algorithm:1.Paging

    i)Display optiona)storeb)Retrievec)Exit

    ii)Read chiii)if ch=a then get page size and process sizeiv)allocate process in pagesv)if ch=2 then get logical address to the retrieve

    2.Page demandi)Display option

    a)Storeb)Retrievec)Exit

    ii)Read chiii)allocate the content to the pageiv)Enter the page number

    Source code for paging:import java.io.*;class pagemanagementsub{

  • 120

    String name;class pagemanagementsub{String content;}pagemanagementsub page[]=new pagemanagementsub[10];public pagemanagement(){for(int i=0;i

  • 121

    int i=0,j=0,e=0,t;do{System.out.println(\nEnter the logical memory size(power of 2):);pagememsize=Integer.parseInt(d.readLine());}while(ispow2(pagememsize)!=0);do{System.out.println(\nEnter the process size:);pageprocesssize=Integer.parseInt(d.readLine());if(pageprocesssize>pagememsize)System.out.println(\nError:process size should not exceed logical memory size!);}while(pageprocesssize>pagememsize);do{System.out.println(\Enter the page size(power of 2):);ppage=Integer.parseInt(d.readLine());}while(ispow2(ppage)!=0);ptotalpage=pageprocesssize/ppage;if((pageprocesssize%ppage)!=0)ptotalpage++;for(i=0;i

  • 122

    p[i].page[j].content=d.readLine();e++;if(e==pageprocessize)break;}}System.out.println(Page No\t Frame No);for(i=0;i

  • 123

    }int ispow(int no){float n=(float)no;for(;n>2;)n/=2;if(n==2.0)return 0;elsereturn 1;}void pageretreive()throws IOException{DataInputStream d=new DataInputStream(System.in);int offset=0,physical_addr=0,logical_addr;int pageno,frameno;System.out.println (\nEnter logical address to retrieve:);logical_addr=Integer.parseInt(d.readLine());if(logical_addr>=pageprocesssize||logical_addr

  • 124

    System.out.println(\nOffset value:+offset);System.out.println(\nPhysical address:+physical_addr);System.out.println(\nContent:+p[pageno].page[offset].content);}}class mem_paging{public static void main(String args[])throws IOException{mem_management m=new mem_management();int ch;DataInputStream d=new DataInputStream(System.in);System.out.println(\nMEMORY MANAGEMENT TECHNIQUE:);System.out.println(PAGING:);do{System.out.println(\n1.Store);System.out.println(\n2.Retreive);System.out.println(\n3.Exit);System.out.println(\nEnter your choice:);ch=Integr.parseInt(d.readLine());switch(ch){case 1:

    m.pagestore();break;

    case 2:m.pageretreive();break;

  • 125

    case 3:System.exit(0);break;

    default:continue;

    }}while(true);}}Source code for Demand Paging:import java.io.*;class demand_paging{int prs,t;int pmt[]=new int[20];int limit[]=new int[20];int base[]=new int[20];String frame[]=new String[50];String val[]=new String[50];String cont=y;void demandstore()throws IOException{int i,j,k=0;DataInputStream d=new DataInputStream(System.in);System.out.println(\nDemand Paging Storing);System.out.println(\nEnter the logical memory size:);prs=Integer.parseInt(d.readLine());System.out.println(\nLogical memory contents:);

  • 126

    for(i=0;i

  • 127

    }elsej++;}}else{limit[i]=-1;base[i]=-1;}}System.out.println(\nPage map table:);System.out.println(PAGE NO\tFRAME NO\tCONTENT);for(i=0;i

  • 128

    {System.out.println(\nThe page is loaded to frame+limit[i]+and contentval[i]);break;}else{k=0;while(true){if(val[i].compareTo(frame[k])==0){limit[i]=pmt[i];base[i]=1;break;}elsek++;}}}System.out.println(\nPage map table:);System.out.println(PAGE NO\tFRAME NO\tCONTENT);for(i=0;i

  • 129

    class mem_demand{public static void main(String args[])throws IOException{demand_paging dp=new demand_paging();int c;DataInputStream d=new DataInputStream(System.in);System.out.println(\nMEMORY MANAGEMENT TECHNIQUE);System.out.println(DEMAND Paging:);do{System.out.println(\n1.Store);System.out.println(2.Retreive);System.out.println(3.Exit);System.out.println(\nEnter your choice:);c=Integer.parseInt(d.readLine());switch (c){case 1:

    dp.demandstore();break;

    case 2:dp.demandretreive();break;

    case 3:System.exit(0);break;

    default:continue;

  • 130

    }}while(true);}}Sample Input and Output:PagingMEMORY MANAGEMENT TECHNIQUE:PAGING:1.Store2.Retreive3.ExitEnter your choice:1Enter the logical memory size(power of 2):16Enter the process size:10Enter the page size(power of 2):4Enter the content of page[0]:Enter the content[0]:00Enter the content[1]:01Enter the content[2]:02Enter the content[3]:03Enter the content of page[1]:Enter the content[0]:10Enter the content[1]:11Enter the content[2]:12Enter the content[3]:13Enter the content of page[2]:Enter the content[0]:20Enter the content[1]:21

  • 131

    Page No Frame No0 11 32 0

    Physical memoryContent of page[0]:Content[4]:00Content[5]:01Content[6]:02Content[7]:03Content of page[1]:Content[12]:10Content[13]:11Content[14]:12Content[15]:13Content of page[2]:Content[0]:20Content[1]:21The page has internal fragmentation1.Store2.Retreive3.ExitEnter your choice:2Enter logical address to retrieve:2Page no:0Frame no:1Offset value:2Physical address:6Content:02

  • 132

    1.Store2.Retreive3.ExitEnter your choice:3Demand PagingMEMORY MANAGEMENT TECHNIQUE:DEMAND PAGING:1.Store2.Retreive3.ExitEnter your choice:1Demand Paging StoringEnter the logical memory size:10Logical memory contents:Enter the value[0]:oneEnter the value[1]:TwoEnter the value[2]:ThreeEnter the value[3]:FourEnter the value[4]:FiveEnter the value[5]:SixEnter the value[6]:SevenEnter the value[7]:EightEnter the value[8]:NineEnter the value[9]:Ten

    Frame Details:Enter the frame no to store One:11Enter the frame no to store Two:22Enter the frame no to store Three:33

  • 133

    Enter the frame no to store Four:44Enter the frame no to store Five:55Enter the frame no to store Six:66Enter the frame no to store Seven:77Enter the frame no to store Eight:88Enter the frame no to store Nine:99Enter the frame no to store Ten:100Store Onein frame 11[Y/N]:YStore Twoin frame 22[Y/N]:YStore Threein frame 33[Y/N]:YStore Fourin frame 44[Y/N]:YStore Fivein frame 55[Y/N]:YStore Sixin frame 66[Y/N]:NStore Sevenin frame 77[Y/N]:YStore Eightin frame 88[Y/N]:YStore Ninein frame 99[Y/N]:YStore Tenin frame 100[Y/N]:NPage map table:PAGE NO FRAME NO CONTENT0 11 One1 22 Two2 33 Three3 44 Four4 55 Five5 -1 Six6 77 Seven7 88 Eight8 99 Nine9 -1 Ten

  • 134

    (-1) represents frame number which is not loaded1.Store2.Retreive3.ExitEnter your choice:2Demand page retrieval:Enter the page no to retrieve[0-9]:1The page is loaded to frame 22 and content TwoPage map table:PAGE NO FRAME NO CONTENT0 11 One1 22 Two2 33 Three3 44 Four4 55 Five5 -1 Six6 77 Seven7 88 Eight8 99 Nine9 -1 Ten(-1) represents frame number which is not loaded1.Store2.Retreive3.ExitEnter your choice:3Result:

    Thus the java program to implement the memory management techniques wascreated, executed successfully and the output was verified

  • 135

    Ex.No:17PAGE REPLACEMENT TECHNIQUES

    Aim:To write a java program to implement the concept of different page replacement

    techniques.Algorithm:1.Start the program execution2.enter the number of pages and frames3.enter the value into pages4.display menu5.if ch=1,the FIFO6.if ch=2,then LRU7.if ch=3,then exit8.if any page fault then print page fault occurs9.Stop the executionSource code:import java.io.*;class page_r{int page[][]=new int[50][50];int p[]=new int[50];int ht[]=new int[10];int n,fr;void get()throws IOException{int i;DataInputStream d=new DataInputStream(System.in);System.out.print(\nEnter the no.of page:);n=Integer.parseInt(d.readLine());

  • 136

    System.out.print(\nEnter the no.of frame:);fr=Integer.parseInt(d.readLine());System.out.println(\nEnter the content:);for(i=0;i

  • 137

    for(k=0;k

  • 138

    System.out.println();}System.out.println(\nTotal no.of page fault:+pf);}void lru{int page1[][]=new int[fr][n];int frame[]=new int[fr];int count[]=new int[fr];int I,j,k,ts,flg,pf=0,min,c=0;ts=0;for(i=0;i

  • 139

    c++;count[j]=ts;flg=1;continue;}}if((frame[j]!=-1&&flg==0)||(frame[j]==-1&&flg==1)){page1[j][c]=page1[j][c-1];}}if(flg==0){for(j=0;j

  • 140

    min=100;for(j=0;j

  • 141

    {for(i=0;i

  • 142

    System.out.println(\n\n1.FIFO);System.out.println(2.LRU);System.out.println(3.Exit);System.out.print(\nEnter your choice:);ch=Integer.parseInt(d.readLine());switch(ch){case 1:

    p.fifo();break;

    case 2:p.lru();break;

    case 3:System.out.println(you are exiting);

    default:System.out.println(Invalid choice!!);

    }}while(ch!=3);}}Sample Input and Output:Enter the no.of page:5Enter the no.frame:5Enter the content:234

  • 143

    561.FIFO2.LRU3.ExitEnter your choice:1

    FIFOpage number:2 3 4 5 6Page table:2 2 2 2 2- 3 3 3 3- - 4 4 4- - - 5 5- - - - 6Total no. of page fault:5

    1.FIFO2.LRU3.ExitEnter your choice:2

    LRUPage number:2 3 4 5 6Page table:2 2 2 2 2- 3 3 3 3- - 4 4 4

  • 144

    - - - 5 5- - - - 6Total no. of page fault:51.FIFO2.LRU3.ExitEnter your choice:3you are exiting

    Result:Thus the java program to implement the different page replacement techniques

    was created, executed successfully and the output was verified.

  • 145

    Ex.No:18PRODUCER CONSUMER PROBLEM

    Aim:To write a java program to implement the concept of the producer consumer

    problem.Algorithm:a)Display the menu Read the choice

    1.Produce a problem2.consume a problem3.display4.exit

    b)if choice=1 then do1.get the process to be proceed2.check whether process already existing if(yes) display3.check whether process already existing if(yes)then consume the process4.display all the process to be consumed

    i)get the process to be consumedii)check the process is already produced, if yes then consume.

    Source code:import java.io.*;class semaphore{int s=0,z=0;String wait[]=new String[50];String produce[]=new String[50];void producer()throws IOException{int i,j=0,check=0;String ch=y;

  • 146

    do{DataInputStream d=new DataInputStream(System.in);System.out.println(\nenter the process name to produce:);++s;produce[s]=d.readLine();for(i=1;i

  • 147

    for(i=j;i

  • 148

    System.out.println(\nEnter the consume process);consume=d.readLine();for(i=1;i

  • 149

    {System.out.println(\nprocess+consume+has to be produced);System.out.println(\nIt has to wait);wait[++z]=consume;}if(z==0)System.out.println(\nNo waiting process);else{System.out.println(\nWaiting process);for(i=1;i

  • 150

    class prod_cons{public static void main(String args[])throws IOException{DataInputStream d=new DataInputStream(System.in);semaphore se=new semaphore();int c;do{System.out.println(\n\nProducer--> Consumer problem);System.out.println(\n1.produce a process);System.out.println(2.Consume a process);System.out.println(3.Display);System.out.println(4.Exit);System.out.println(\nEnter your choice:);c=Integer.parseInt(d.readLine());switch(c){case 1:

    se.producer();break;

    case 2:se.consumer();break;

    case 3:se.display();break;

    case 4:System.exit(0);

  • 151

    default:System.out.println(Option not valid);

    }}while(true);}}Sample Input and Output:Producer- ->Consumer problem1.Produce a process2.Consume a process3.Display4.ExitEnter your choice:1Enter the process name to produce:P1Produced processP1Do you want to continue?YEnter the process name to produce:P2Produced processP1P2Do you want to continue?NProducer- ->Consumer problem1.Produce a process2.Consume a process3.Display4.ExitEnter your choice:2

  • 152

    Consume a processEnter the consume processP3Process P3 has to be producedIt has to waitWaiting processP3Do you want to continue?NProducer- ->Consumer problem1.Produce a process2.Consume a process3.Display4.ExitEnter your choice:3PRODUCED PROCESSP1P2WAITING PROCESSP3Producer- ->Consumer problem1.Produce a process2.Consume a process3.Display4.ExitEnter your choice:4Result:

    Thus the java program to implement the producer consumer problem was created,executed successfully and the output was verified.

  • 153

    Ex.No:19BANKERS ALGORITHM

    Aim:To write a java program to implement Bankers Algorithm

    Algorithm:1.If request be the array of process p2.If request [i]=k then process p wants k instance of resource type R3.when a request for resources is made by process p then following action are taken

    i)if request

  • 154

    class BankSub{int alloc[][],max[],need[][],ne[][],al[][],interavail[][];int finish[],avail[],requ[],work[],order[];int pre,av[];int n,j,i,f,ch,inc,a,r,flag;char c;public BankSub(){alloc=new int[10][10];max=new int[10][10];need=new int[10][10];ne=new int[10][10];al=new int[10][10];interavail=new int[10][10];finish=new int[10];avail=new int[10];requ=new int[10];work=new int[10];order=new int[10];pre=0;av=new int[10];inc=0;}public void mainFun()throws IOException{try{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

  • 155

    System.out.print(\n\nHow many process to be entered:);n=Integer.parseInt(br.readLine());System.out.print(\nEnter Number of resources:);r=Integer.parseInt(br.readLine());System.out.println(\nEnter the allocated value for each process:);for(i=0;i);for(j=0;j

  • 156

    for(i=0;i

  • 157

    catch(IOException e){System.out.println(Number format invalid);}}void safety(){int con[]=new int[10];a=0;inc=0;for(i=0;i

  • 158

    {interavail[i][j]=work[j]}*/}}inc++;}while(inc!=2);f=0;for(i=0;i

  • 159

    void summerize(){System.out.println(\n\nSummerizing..);System.out.println(\nMax value\tAllocated value\tNeeded value\tRemainingavailable);System.out.println(\n\t);for(i=0;i

  • 160

    {if(need[i][j]+n+:);pno=Integr.parseInt(b.readLine());if(pno

  • 161

    flag=1;}else{flag=0;}//flag=flag*f;}System.out.println();for(i=0;i

  • 162

    }else{System.out.println(\nERROR);f=0;System.out.println(\nSince proceaa requires more than what it had already asked);break;}}if(f!=0){summerize();if(pre!=0){alloc[pno][i]=al[pno][i];need[pno][i]=ne[pno][i];avail[i]=av[i];pre=0;}}var=0;}else{continue;}}}catch(IOException e)

  • 163

    {}}}Sample Input and Output:How many process to be entered:5Enter number of resources:4Enter the allocated value for each process:P1-->0012P2-->1000P3-->1354P4-->0014P5-->0

  • 164

    632Enter maximum value for each process:P1-->0012P2-->1750P3-->2356P4-->0656P5-->0652

  • 165

    Enter available value:Resource1-->1Resource2-->5Resource3-->2Resource4-->01.Safety Algorithm2.Resource request algorithm3.ExitEnter your choice:1

    The system in safe state.The Safe sequence:1 3 4 5 2Summerizing..Max value Allocated value Needed value Remaining Available0 0 1 2 0 0 1 2 0 0 0 0 1 5 3 21 7 5 0 1 0 0 0 0 7 5 0 3 14 12 122 3 5 6 1 3 5 4 1 0 0 2 2 8 8 60 6 5 6 0 0 1 4 0 6 4 2 2 8 9 100 6 5 2 0 6 3 2 0 0 2 0 2 14 12 12The available resource:1 5 2 01.Safety Algorithm2.Resource request algorithm3.ExitEnter your choice:2Enter the process no 1)-->5:2Enter the current request for this process:0420

  • 166

    Summerizing..Max value Allocated value Needed value Remaining Available0 0 1 2 0 0 1 2 0 0 0 0 1 1 1 21 7 5 0 1 4 2 0 0 3 3 0 3 14 12 122 3 5 6 1 3 5 4 1 0 0 2 2 4 6 60 6 5 6 0 0 1 4 0 6 4 2 2 10 10 100 6 5 2 0 6 3 2 0 0 2 0 2 10 9 8The available resource:1 1 0 01.Safety Algorithm2.Resource request algorithm3.ExitEnter your choice:3

    Result:Thus the java program to implement the Bankers Algorithm was created, executed

    successfully and the output was verified.

  • 167

    Ex.No:20DINING PHILOSOPHER PROBLEM

    Aim:To write a java program to implement the concept of the Dining Philosopher

    problem.Algorithm:1.Get the total number of philosopher2.get the number of philosopher who are hungry3.if all are hungry then deadlock occurs4.get the philosopher position5.display the menu6.Two can eat at a time7.one can eat at a time8.if choice is one,then

    a)get the philosopher states of eating positionb)display the means of those who are waiting.

    Source code:import java.io.*;import.java.math.*;public class DinPhilo{public static void main(String args[]){DinPhilosopher OB;OB=new DinPhilosopher();try{OB.mainFun();}

  • 168

    catch(IOException e){System.out.println(Exception caught from main.);}}}class DinPhilosopher{String[] philname;char op,conf=Y;int status[],hu[],pos,cho,x,i,j,r,t,tph,howhung,s;public DinPhilosopher(){philname=new String[20];status=new int[20];hu=new int[20];s=0;}void one(){pos=0;System.out.println(\nALLOW ONE PHILOSOPHER TO EAT AT ANY TIME\n);for(i=0;i

  • 169

    }void two(){System.out.println(\nTWO PHILOSOPHER TO EAT AT SAME TIME\n);for(i=0;i

  • 170

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));do(System.out.println(\n\nDINING PHILOSOPHER PROBLEM);System.out.print(\n1.One can eat at a time\n2.Two can eat at a time\n3.Exit\n\nEnteryour choice:);cho=Integer.parseInt(br.readLine());switch(cho){case 1:one();

    break;case 2:two();

    break;case 3:System.exit(0);default:System.out.println(\nInvalid option..);}System.out.print(\nContinue with main menu(Y/N):);conf=(char)br.read();br.readLine();}while(conf==Y||conf==y);}catch(IOException e){System.out.println(IOException handled);}}public void mainFun()throws IOException{try{

  • 171

    BufferReader br=new BufferedReader(new InputStreamReader(System.in));System.out.println(\n\nDINING PHILOSOPHER PROBLEM);System.out.println(\nENTER THE TOTAL PHILOSOPHER:);tph=Integer.parseInt(br.readLine());System.out.print(\n);for(i=0;i

  • 172

    hu[i]=Integer.parseInt(br.readLine());status[hu[i]]=2;}menu();}}catch(IOException ex){System.out.println(\nException caught from mainfun method);}}}Sample Input and Output:

    DINING PHILOSOPHER PROBLEMENTER THE TOTAL PHILOSOPHER:5PHILOSOPHER 1:aPHILOSOPHER 2:bPHILOSOPHER 3:cPHILOSOPHER 4:dPHILOSOPHER 5:eHow many are hungry:3ENTER PHILOSOPHER 1 POSITION:2ENTER PHILOSOPHER 2 POSITION:4ENTER PHILOSOPHER 3 POSITION:5DINING PHILOSOPHER PROBLEM1.One can eat at a time2.Two can eat at a time3.ExitEnter your choice:1

  • 173

    ALLOW ONE PHILOSOPHER TO EAT AT ANY TIMEPHILOSOPHERIS GRANTED TO EATPHILOSOPHERIS WAITINGPHILOSOPHERIS WAITINGPHILOSOPHERIS GRANTED TO EATPHILOSOPHERIS WAITINGPHILOSOPHERIS GRANTED TO EATContinue with main menu(Y/N):YDINING PHILOSOPHER PROBLEM1.One can eat at a time2.Two can eat at a time3.ExitEnter your choice:2TWO PHILOSOPHER TO EAT AT SAME TIMECOMBINATION 1ANDARE GRANTED TO EATPHILOSOPHERIS WAITINGCOMBINATION 2ANDARE GRANTED TO EATPHILOSOPHERIS WAITINGCOMBINATION 3ANDARE GRANTED TO EATPHILOSOPHERIS WAITING.Continue with main menu(Y/N):YDINING PHILOSOPHER PROBLEM1.One can eat at a time2.Two can eat at a time3.ExitEnter your choice:3

  • 174

    Result:Thus the java program to implement the Dining philosopher problem was created,

    executed successfully and the output was verified