Java ~ operating system experiment simulates the elevator scheduling algorithm to achieve the disk scheduling.

Article catalog

Experiment content

  • Simulation of elevator scheduling algorithm to achieve disk scheduling.

Purpose of the experiment

  • Disk is a kind of high-speed, large number of rotating, direct access storage device. As the auxiliary memory of computer system, it is burdened with heavy input and output tasks. In multi-channel programming system, there are often several input and output requests that require access to the disk to be processed at the same time. The system can adopt a strategy to execute all input and output requests that require access to the disk in the best order possible, which is called disk scheduling, and the algorithm used is called disk scheduling algorithm. Disk scheduling can reduce the total time required to serve several I / O requests, thus improving system efficiency. In this experiment, students are required to design a disk scheduler to observe the dynamic process of the scheduler.

Experimental principle

  • Simulation of elevator scheduling algorithm, disk scheduling.
    Disk is a storage device to be shared by multiple processes, but a disk can only serve one process at a time.
    When a process is accessing a disk, other processes that want to access the disk must wait until the disk finishes working once.
    When more than one process puts forward input and output requests and is in a waiting state, elevator scheduling algorithm can be used to select a process from several waiting visitors and let it access the disk. When the access arm only needs to move to the requested cylinder farthest in one direction, if there is no access request, the access arm changes direction.

Complete code + test

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

//This is a magnetic head that describes which subscript he is in and whether he is going left or right
class Head {
    public int index;
    public String direction;

    public Head(int index, String direction) {
        this.index = index;
        this.direction = direction;
    }
}

//This is a class describing a track access task
class MyRunnable implements Runnable {

    //Tracks to access
    public int track;
    public boolean key = true;

    public MyRunnable(int track) {
        this.track = track;
    }

    @Override
    public void run() {
        System.out.print(track + " ");
    }
}

public class DiskScheduling extends Thread {

    //A linked list is needed to store the threads to access the disk
    //A count records the number of visits
    //sum to record the visit distance
    //A magnetic head is also needed
    //What is the maximum track needed
    private List<MyRunnable> threadList = new LinkedList<>();
    private int count;
    private int sum;
    private Head head;
    public static final int MaximumTrack = 200;

    //Initializing the head in the constructor
    public DiskScheduling(int index, String direction) {
        this.head = new Head(index, direction);
    }

    //Need a method to add program to linked list
    public void add(int[] tracks) throws InterruptedException {

        //The name of the thread creation thread is that it has the track address it is looking for

        for (int track : tracks
             ) {
            MyRunnable myRunnable = new MyRunnable(track);
            threadList.add(myRunnable);
        }


        //Sort the linked list by the address to be searched
       threadList.sort(new Comparator<MyRunnable>() {
           @Override
           public int compare(MyRunnable o1, MyRunnable o2) {
               return o1.track - o2.track;
           }
       });

        this.start();
        this.join();
        //worke();
    }

    @Override
    public void run() {

        while (this.count != threadList.size()) {
            while (this.head.direction.equals("right")) {

                for (MyRunnable m : threadList
                        ) {
                    if (m.track >= head.index && m.key) {
                        count++;
                        int distance = m.track - this.head.index;
                        sum += distance;
                        this.head.index = m.track;
                        m.key = false;
                        m.run();
                        //threadList.remove(m);
                    }
                    this.head.direction = "left";
                }

            }


            while (this.head.direction.equals("left")) {

                for (int i = threadList.size() - 1; i >= 0; i--) {

                    if (threadList.get(i).track > this.head.index || !threadList.get(i).key) {
                        continue;
                    }

                    //At this time, it means that we have found a smaller subscript
                    this.count++;
                    int distance = this.head.index - threadList.get(i).track;
                    sum += distance;
                    this.head.index = threadList.get(i).track;
                    threadList.get(i).key = false;
                    this.threadList.get(i).run();
                    //this.threadList.remove(this.threadList.get(i));
                }

                this.head.direction = "right";
            }
        }
    }

    /* //A method is needed to execute the program
    private void worke() throws InterruptedException {

        while (! this.threadList.isEmpty()) {

            while (this.head.direction.equals("right")) {
                synchronized (this) {
                    for (Thread t : this.threadList
                    ) {
                        if (Integer.parseInt(t.getName()) > this.head.index) {
                            this.count++;
                            int distance = Integer.parseInt(t.getName()) - this.head.index;
                            sum += distance;
                            this.head.index = Integer.parseInt(t.getName());
                            t.start();
                            t.join();
                            this.threadList.remove(t);
                        }
                    }
                }
                this.head.direction = "left";
            }

            while (this.head.direction.equals("left")) {
                synchronized (this) {
                    for (int i = this.threadList.size() - 1; i >= 0; i--) {
                        if (Integer.parseInt(this.threadList.get(i).getName()) > this.head.index) {
                            continue;
                        }

                        //At this time, it means that we have found a smaller subscript
                        this.count++;
                        int distance = this.head.index - Integer.parseInt(this.threadList.get(i).getName());
                        sum += distance;
                        this.head.index = Integer.parseInt(this.threadList.get(i).getName());
                        this.threadList.get(i).start();
                        this.threadList.get(i).join();
                        this.threadList.remove(this.threadList.get(i));
                    }
                }
                this.head.direction = "right";
            }

        }
    }*/

    //Need a method to return the average seek length
    public float averageSeekLength () {

        return (float) ((this.sum * 1.0) / this.count);
    }
}




  • test
    Assuming that the disk has 200 tracks, a track request sequence (no less than 15) is randomly generated by a random function in C language and put into the simulated disk request queue. Assuming that the current head is on Track 100 and moves in the direction of increasing the track number. Please give the order of satisfying the request when scheduling the disk according to the elevator scheduling algorithm, and calculate their average seek length.
import java.util.Arrays;
import java.util.Random;

public class DiskTest {

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

        //You need to specify the location and direction of the head when creating the disk schedule
        DiskScheduling diskScheduling = new DiskScheduling(100, "right");

        //Generate 15 random numbers
        int[] nums = new int[15];
        for (int i = 0; i < 15; i++) {
            Random random = new Random();
            nums[i] = random.nextInt(DiskScheduling.MaximumTrack);
        }

        System.out.println("Initial application sequence:");
        System.out.println(Arrays.toString(nums));

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

        System.out.println("Elevator dispatching execution sequence");
        diskScheduling.add(nums);

        System.out.println();
        //Wait for the thread to finish executing before printing seek
        diskScheduling.join();
        System.out.print("The average seek is:");
        System.out.println(diskScheduling.averageSeekLength());
    }
}

  • result

Tags: Java Programming less C

Posted on Tue, 16 Jun 2020 04:39:09 -0400 by Mr Chris