You are on page 1of 24

 

                                                            PRACTICAL NO.4

Q.   Implement FCFS scheduling algorithm in Java.

 CODE:

           import java.util.Scanner;

          public class FCFS {

             public static void main(String[] args) {

                 System.out.println("Enter the number of Process -> ");

                 Scanner in = new Scanner(System.in);

                 int numberOfProcess = in.nextInt();

                 int pid[] = new int[numberOfProcess];

                 int bt[] = new int[numberOfProcess];

                 int ar[] = new int[numberOfProcess];

                int ct[] = new int[numberOfProcess];

                int ta[] = new int[numberOfProcess];

                int wt[] = new int[numberOfProcess];

                float avgWait = 0, avg_TA = 0;

                for(int k = 0; k < numberOfProcess; k++) {

                       System.out.println("Enter Process " + (k + 1) + " arrival time -> ");

                       ar[k] = in.nextInt();

                      System.out.println("Enter Process " + (k + 1) + " burst time -> ");

                      bt[k] = in.nextInt();

                      pid[k] = k + 1;
 

                }

               int temp;

               for (int i = 0; i < numberOfProcess; i++) {

                     for (int j = i + 1; j < numberOfProcess; j++) {

                           if (ar[i] > ar[j]) {

                                temp = ar[i];

                               ar[i] = ar[j];

                               ar[j] = temp;

                               temp = pid[i];

                               pid[i] = pid[j];

                               pid[j] = temp;

                              temp = bt[i];

                              bt[i] = bt[j];

                              bt[j] = temp;

                        }

                 }

             }

            System.out.println();

            ct[0] = bt[0] + ar[0];

 
            for(int i = 1; i<numberOfProcess; i++) {

                 ct[i] = ct[i-1] + bt[i];

            }

            for(int i = 0; i<numberOfProcess; i++) {

                ta[i] = ct[i] - ar[i];

                avg_TA += ta[i];

                wt[i] = ta[i] - bt[i];

                avgWait += wt[i];

            }

            avgWait /= numberOfProcess;

            avg_TA /= numberOfProcess;

            System.out.println("******************************************");

            System.out.println("Process\t\tAT\t\tBT\t\tCT\t\tTAT\t\tWT");

            System.out.println("*******************************************");

            for(int i = 0; i<numberOfProcess; i++) {

                System.out.println(pid[i] + "\t\t" + ar[i] + "\t\t" + bt[i] + "\t\t" + ct[i] + "\t\t" + ta[i] + "\t\t" +    
wt[i]);

            }

        System.out.println("\nAverage Waiting Time -> " + avgWait);

        System.out.println("\nAverage Turn Around Time -> " + avg_TA + "\n");

    }
 

/*

Enter the number of Process ->

Enter Process 1 arrival time ->

Enter Process 1 burst time ->

Enter Process 2 arrival time ->

Enter Process 2 burst time ->

Enter Process 3 arrival time ->

Enter Process 3 burst time ->

Enter Process 4 arrival time ->

Enter Process 4 burst time ->

Enter Process 5 arrival time ->

Enter Process 5 burst time ->

4
 

******************************************

Process         AT              BT              CT              TAT             WT

*******************************************

4               0               3               3               3               0

3               1               8               11              10              2

1               2               6               17              15              9

5               4               4               21              17              13

2               5               3               24              19              16

Average Waiting Time -> 8.0

Average Turn Around Time -> 12.8

*/

 PRACTICAL NO. 5

Q.Implement SJF (with no preemption) scheduling algorithm in Java

CODE:

            import java.util.*;

            public class SJF {

               public static void main(String args[])

              {
                 Scanner sc = new Scanner(System.in);

                 System.out.println ("enter no of process:");

                 int n = sc.nextInt();

                 int pid[] = new int[n];

                 int at[] = new int[n];

                 int bt[] = new int[n];

                 int ct[] = new int[n];

                int ta[] = new int[n];

                int wt[] = new int[n];

                int f[] = new int[n]; 

                int st=0, tot=0;

                float avgwt=0, avgta=0;

                for(int i=0;i<n;i++)

                    {

                        System.out.println ("enter process " + (i+1) + " arrival time:");

                        at[i] = sc.nextInt();

                        System.out.println ("enter process " + (i+1) + " brust time:");

                        bt[i] = sc.nextInt();

                        pid[i] = i+1;

                        f[i] = 0;

                 }

                  boolean a = true;

                  while(true)

                    {

                      int c=n, min=999;

                      if (tot == n)

                     break;

                     for (int i=0; i<n; i++)


                         {

                            if ((at[i] <= st) && (f[i] == 0) && (bt[i]<min))

                                {

                                     min=bt[i];

                                      c=i;

                                }

                        }

                       if (c==n)

                           st++;

                      else

                     {

                         ct[c]=st+bt[c];

                         st+=bt[c];

                         ta[c]=ct[c]-at[c];

                        wt[c]=ta[c]-bt[c];

                        f[c]=1;

                        tot++;

                     }

               }

              System.out.println("\npid  arrival brust  complete turn waiting");

              for(int i=0;i<n;i++)

                  {

                     avgwt+= wt[i];

                     avgta+= ta[i];

                     System.out.println(pid[i]+"\t"+at[i]+"\t"+bt[i]+"\t"+ct[i]+"\t"+ta[i]+"\t"+wt[i]);

                 }

               System.out.println ("\naverage tat is "+ (float)(avgta/n));


               System.out.println ("average wt is "+ (float)(avgwt/n));

               sc.close();

           }

    }

/*

enter no of process:

enter process 1 arrival time:

enter process 1 brust time:

enter process 2 arrival time:

enter process 2 brust time:

enter process 3 arrival time:

enter process 3 brust time:

enter process 4 arrival time:

enter process 4 brust time:

enter process 5 arrival time:

enter process 5 brust time:

4
 

pid  arrival brust  complete turn waiting

1       2       6       9       7       1

2       5       2       11      6       4

3       1       8       23      22      14

4       0       3       3       3       0

5       4       4       15      11      7

average tat is 9.8

average wt is 5.2

*/PRACTICAL NO.6

Q. Implement RR scheduling algorithm in Java . 

CODE:

           import java.util.Scanner;

public class RoundRobin {

    private static Scanner inp = new Scanner(System.in);

   

    public static void main(String[] args) {       

        int n, tq, timer = 0, maxProcessIndex = 0;

        float avgWait = 0, avgTT = 0;

        System.out.println("\nEnter the time quantum -> ");

        tq = inp.nextInt();

        System.out.println("\nEnter the number of processes -> ");


        n = inp.nextInt();

        int arrival[] = new int[n];

        int burst[] = new int[n];

        int wait[] = new int[n];

        int turn[] = new int[n];

        int queue[] = new int[n];

        int temp_burst[] = new int[n];

        boolean complete[] = new boolean[n];

        System.out.println("\nEnter the arrival time of the processes -> ");

        for(int i = 0; i < n; i++) {

            arrival[i] = inp.nextInt();

        }

        System.out.println("\nEnter the burst time of the processes -> ");

        for(int i = 0; i < n; i++) {

            burst[i] = inp.nextInt();

            temp_burst[i] = burst[i];

        }

       

        for(int i = 0; i < n; i++) {

            complete[i] = false;

            queue[i] = 0;

        }

       

        while(timer < arrival[0])

            timer++;

        queue[0] = 1;
 

        while(true) {

            boolean flag = true;

            for(int i = 0; i < n; i++) {

                if(temp_burst[i] != 0) {

                    flag = false;

                    break;

                }

            }

            if(flag)

                break;

            for(int i = 0; (i < n) && (queue[i] != 0); i++) {

                int ctr = 0;

                while((ctr < tq) && (temp_burst[queue[0]-1] > 0)) {

                    temp_burst[queue[0]-1] -= 1;

                    timer += 1;

                    ctr++;

                   

                    checkNewArrival(timer, arrival, n, maxProcessIndex, queue);

                }

                if((temp_burst[queue[0]-1] == 0) && (complete[queue[0]-1] == false)) {

                    turn[queue[0]-1] = timer;

                    complete[queue[0]-1] = true;

                }

                boolean idle = true;

                if(queue[n-1] == 0) {

                    for(int k = 0; k < n && queue[k] != 0; k++) {


                        if(complete[queue[k]-1] == false) {

                            idle = false;

                        }

                    }

                }

                else

                    idle = false;

               

                if(idle) {

                    timer++;

                    checkNewArrival(timer, arrival, n, maxProcessIndex, queue);

                }

                queueMaintainence(queue, n);

            }

        }

        for(int i = 0; i < n; i++) {

            turn[i] = turn[i] - arrival[i];

            wait[i] = turn[i] - burst[i];

        }

        System.out.println("\nPN\t\tAT\t\tBT\t\tTAT\t\tWT" + "\n");

        for(int i = 0; i < n; i++) {

            System.out.println(i+1 + "\t\t" + arrival[i] + "\t\t" + burst[i] + "\t\t" + turn[i] + "\t\t" + wait[i] + "\
n");

        }

 
        for(int i = 0; i < n; i++) {

            avgWait += wait[i];

            avgTT += turn[i];

        }

        System.out.println("\nAverage wait time : " + (avgWait/n) + "\naverage Turn Around Time : " +
(avgTT/n));

    }

   

    public static void queueUpdation(int queue[], int timer, int arrival[], int n, int maxProcessIndex) {

        int zeroIndex = -1;

        for(int i = 0; i < n; i++) {

            if(queue[i] == 0) {

                zeroIndex = i;

                break;

            }               

        }

       

        if(zeroIndex == -1)

            return;

        queue[zeroIndex] = maxProcessIndex + 1;

    }

    public static void checkNewArrival(int timer, int arrival[], int n, int maxProcessIndex, int queue[]) {

        if(timer <= arrival[n-1]) {

            boolean newArrival = false;

            for(int j = (maxProcessIndex+1); j < n; j++) {

                if(arrival[j] <= timer) {

                    if(maxProcessIndex < j) {


                        maxProcessIndex = j;

                        newArrival = true;

                    }

                }

            }

            if(newArrival)

                queueUpdation(queue, timer, arrival, n, maxProcessIndex);

        }

    }

        public static void queueMaintainence(int queue[], int n) {

            for(int i = 0; (i < n-1) && (queue[i+1] != 0); i++) {

                int temp = queue[i];

                queue[i] = queue[i+1];

                queue[i+1] = temp;

            }

        }

    } 

PRACTICAL NO.7

Q.Write a Java program that implements the banker’s algorithm

import java.util.Scanner;

public class BankersAlgo {

    int max[][];

    int need[][];

    int available[][];

    int allocation[][];

    int np, nr;


 

    public void input() {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter no. of processes and no. of resources -> ");

        np = input.nextInt();

        nr = input.nextInt();

        max = new int[np][nr];

        need = new int[np][nr];

        available = new int[1][nr];

        allocation = new int[np][nr];

        System.out.println("Enter the allocation matrix");

        for(int i = 0; i < nr; i++) {

            char c = (char) ((char)65+i);

            System.out.print(c + "\t");

        }

        System.out.println();

        for(int i = 0; i < np; i++) {

            for(int j = 0; j < nr; j++) {

                allocation[i][j] = input.nextInt();

            }

        }

        System.out.println("Enter max matrix");

        for(int i = 0; i < nr; i++) {

            char c = (char) ((char)65+i);


            System.out.print(c + "\t");

        }

        System.out.println();

        for(int i = 0; i < np; i++) {

            for(int j = 0; j < nr; j++) {

                max[i][j] = input.nextInt();

            }

        }

        System.out.println("Enter the Available matrix");

        for(int i = 0; i < nr; i++) {

            char c = (char) ((char)65+i);

            System.out.print(c + "\t");

        }

        System.out.println();

        for(int i = 0; i < nr; i++) {

                available[0][i] = input.nextInt();

        }

        input.close();

    }

    public void cal_need() {

        for(int i = 0; i < np; i++) {

            for(int j = 0; j < nr; j++) {

                need[i][j] = max[i][j] - allocation[i][j];

            }
        }

    }

   

    public boolean check(int p) {

        for(int i = 0; i < nr; i++) {

            if(available[0][i] < need[p][i]);

                return false;

        }

        return true;

    }

    public void algorithm() {

        cal_need();

        int c = 0;

        boolean status[] = new boolean[np];

        while(c < np) {

            boolean allocated = false;

            for(int i = 0; i < np; i++) {

                if(!status[i] && check(i)) {

                    status[i] = true;

                    allocated = true;

                    c++;

                    System.out.println("Allocated process -> " + i);

                    for(int j = 0; j < nr; j++) {

                        available[0][j] = available[0][j] + allocation[i][j];

                    }
                }

            }

            if(!allocated)  break;

        }

        if(c == np)

            System.out.println("\nSafely allocated");

        else

            System.out.println("\nAll processes cannot be allocated safely");

    }

    public static void main(String[] args) {

       

        BankersAlgo obj = new BankersAlgo();

        obj.input();

        obj.algorithm();

       

    }

PRACTICAL NO.8

Q. Write a Java program that implements the FIFO page-replacement algorithm

CODE:  

              import java.io.BufferedReader;

 
             import java.io.*;

           public class FIFO {

   

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

           

                      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                      int frames, pointer = 0, hit = 0, fault = 0, ref_len;

                      int buffer[];

                      int reference[];

                     int mem_layout[][];

                    System.out.println("Enter the number of Frames -> ");

                    frames = Integer.parseInt(br.readLine());

                    System.out.println("Enter the length of the Reference String -> ");

                    ref_len = Integer.parseInt(br.readLine());

                     reference = new int[ref_len];

                     mem_layout = new int[ref_len][frames];

                      buffer = new int[frames];

                       for(int j = 0; j < frames; j++) {

                              buffer[j] = -1;

                      }

                    System.out.println("Enter the reference string -> ");

                    for(int i = 0; i < ref_len; i++) {

                          reference[i] = Integer.parseInt(br.readLine());

                   }

       
                    System.out.println();

                   for(int i = 0; i < ref_len; i++) {

                          int search = -1;

                         for(int j = 0; j < frames; j++) {

                               if(buffer[j] == reference[i]) {

                                    search = j;

                                    hit++;

                                    break;

                            }

                       }

                      if(search == -1) {

                         buffer[pointer] = reference[i];

                         fault++;

                        pointer++;

                        if(pointer == frames)

                        pointer = 0;

                }

                for(int j = 0; j < frames; j++)

                mem_layout[i][j] = buffer[j];

    }

            for(int i = 0; i < frames; i++) {

                for(int j = 0; j < ref_len; j++)

                    System.out.printf("%3d ", mem_layout[j][i]);

                     System.out.println();

            }

            System.out.println("The number of Hits -> " + hit);

            System.out.println("Hit Ratio -> " + (float)((float)hit/ref_len));

            System.out.println("The number of faults -> " + fault);


    }

}   

PRACTICAL NO. 9

Q. .     Write a Java program that implements the LRU page-replacement algorithm

CODE:

          import java.util.Scanner;

          public class LRU {

            public static int min(int counter[], int nFrames) {

            int minimum = counter[0];

            int pos = 0;

             for(int i = 0; i < nFrames; i++) {

                 if(minimum > counter[i])

                   pos = i;

              }

        return pos;

    }

   

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        int n, recent = 0, pageFault = 0, nFrames;

        System.out.println("Enter the number of pages -> ");

        n = s.nextInt();

       

        int pageString[] = new int[n];

 
        System.out.println("Enter the page reference string -> ");

        for(int i = 0; i < n; i++)

            pageString[i] = s.nextInt();

        System.out.println("\nEnter the number of frames -> ");

        nFrames = s.nextInt();

        int frames[] = new int[nFrames];

        int counter[] = new int[nFrames];

        for(int i = 0; i < nFrames; i++) {

            frames[i] = 0;

            counter[i] = 0;

        }

        for(int i = 0; i<n; i++) {

            int flag = 0;

            for(int j = 0; j < nFrames; j++) {

                if(frames[j] == pageString[i]) {

                    flag = 1;

                    counter[j] = recent++;

                    break;

                }

            }

            if(flag == 0) {

                for(int j = 0; j < nFrames; j++) {

                    if(frames[j] == 0) {

                        frames[j] = pageString[i];

                        counter[j] = recent++;


                        flag = 1;

                        pageFault++;

                        break;

                    }

                }

            }

            if(flag == 0) {

                int PositionToReplace = min(counter, nFrames);

                frames[PositionToReplace] = pageString[i];

                counter[PositionToReplace] = recent++;

                pageFault++;

            }

        System.out.println();

        for(int j = 0; j < nFrames; j++) {

            System.out.print(frames[j] + " ");

        }

    }

        System.out.print("\nPage Fault: " + pageFault);

}  

PRACTICAL NO.10

Q. Design a file system in java.


import java.io.File;//import the File class

import java.io.IOException;

public class CreateFile{

public static void main(String[] args){

try{

File obj=new File("C:\\Users\ \Kiran\\Desktop\\Filef1.txt"); if(obj.createNewFile())

System.out.println("File created: "+obj.getName());

else

System.out.println("File already exists.");

catch(IOException e){

System.out.println("An error occured"); e.printStackTrace();

Output:-

File created:- File.txt

You might also like