Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are to simulate a dispatcher using a priority queue system in C++. New proce

ID: 3792818 • Letter: Y

Question

You are to simulate a dispatcher using a priority queue system in C++. New processes are to be entered using a GUI with priority included (numbering should be automatic). Processes are also to be terminated by GUI command. Context switches are to be by command with the cause of the switch being either a blocking call, time slice exceeded or termination. Assume only one CPU. Priorities and numbers of processes can be kept small, just big enough to demonstrate the required functionality. You may pre-populate the queues initially from a data file. I am looking at the mechanism as you are NOT creating actual processes, just simulating them. Functionality to be provided by you: 1. Priority based Ready Queue(s). 2. Blocked list. 3. Output of complete system status after every context switch showing ready, blocked, and running processes.

Sample Output:

A3 Antrian WAKTU TUN... TA1Telah diEksekusi Ambil No Antrian

Explanation / Answer

//dispatcherSim.java

public class dispatcherSim {

   public static void main(String[] args)

{

       simulator sim=new simulator(1000);

       sim.addProcessReady(1);                       //test data

       sim.addProcessReady(3);

       sim.addProcessReady(2);

       sim.addProcessReady(7);

       sim.addProcessReady(4);

       sim.addProcessBlocked(1);

       sim.addProcessBlocked(3);

       sim.addProcessBlocked(2);

       sim.addProcessBlocked(7);

       sim.addProcessBlocked(4);

       GUI gui=new GUI(sim);

       gui.setVisible(true);

     

}

}

==============================================================================

//GUI.java

import java.awt.BorderLayout;

import java.awt.Dialog.ModalityType;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class GUI extends JFrame {

   private JPanel panel;

   private JMenuBar menuBar;

   private JMenu menu;

   private JMenuItem addNew;

   private JMenuItem unBlock;

   private JMenuItem terminate;

   private JMenuItem readMe;

   private JMenuItem exit;

   private JButton cSwitch;

   private JTextArea info;

   private JScrollPane scroll;

   private simulator sim;

   private final String CS = "CONTEXT SWITCH: ";

   private int count = 1;

   public GUI(simulator disSim) {

       sim = disSim;

       initComponents();

   }

   private void addNewBox() {

       final JDialog addDialog = new JDialog();

       JPanel miniPanel = new JPanel(new BorderLayout());

       miniPanel.setPreferredSize(new Dimension(175, 100));

       ButtonGroup radioGroup = new ButtonGroup();

       final JRadioButton blocked = new JRadioButton("Blocked List");

       final JRadioButton ready = new JRadioButton("Ready Queue");

       ready.setSelected(true);

       radioGroup.add(ready);

       radioGroup.add(blocked);

       JPanel radios = new JPanel();

       radios.setPreferredSize(new Dimension(50, 50));

       radios.add(ready);

       radios.add(blocked);

       final JTextField text = new JTextField(10);

       JButton addButton = new JButton("Add");

       addButton.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {

               try {

                   int temp = Integer.parseInt(text.getText());

                   if (blocked.isSelected()) {

                       sim.addProcessBlocked(temp);

                       info.setText(info.getText()

                               + " Added Process to the Blocked List "

                               + sim.getStatus());

                   } else {

                       sim.addProcessReady(temp);

                       info.setText(info.getText()

                               + " Added Process to the Ready Queue "

                               + sim.getStatus());

                   }

                   addDialog.dispose();

               } catch (NumberFormatException n) {

                   GUI.errorMessage("Please Enter a Number");

               }

           }

       });

       JLabel title = new JLabel(" Type in priority");

       miniPanel.add(title, BorderLayout.NORTH);

       miniPanel.add(addButton, BorderLayout.WEST);

       miniPanel.add(text, BorderLayout.EAST);

       miniPanel.add(radios, BorderLayout.SOUTH);

       addDialog.setTitle("Add New Process");

       addDialog.add(miniPanel);

       addDialog.setResizable(false);

       addDialog.pack();

       addDialog.setLocationRelativeTo(null);

       addDialog.setModal(true);

       addDialog.setAlwaysOnTop(true);

       addDialog.setModalityType(ModalityType.APPLICATION_MODAL);

       addDialog.setVisible(true);

   }

   private void blockerBox() {

       final JDialog addDialog = new JDialog();

       JPanel miniPanel = new JPanel(new BorderLayout());

       miniPanel.setPreferredSize(new Dimension(220, 100));

       JLabel title = new JLabel(" Select Process ID");

       JPanel leftPanel = new JPanel();

       leftPanel.setPreferredSize(new Dimension(110, 75));

       JPanel rightPanel = new JPanel();

       rightPanel.setPreferredSize(new Dimension(110, 75));

       final JComboBox<String> blockBox = new JComboBox();

       Object[] tempP = sim.getReadyQueue().toArray();

       for (int i = 0; i < tempP.length; i++) {

           blockBox.addItem(((Process) tempP[i]).getID());

       }

       blockBox.setEditable(false);

       JButton block = new JButton("Block");

       block.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {

               Object[] tempArray = sim.getReadyQueue().toArray();

               for (int i = 0; i < tempArray.length; i++) {

                   if (((Process) tempArray[i]).getID().equals(

                           (String) blockBox.getSelectedItem())) {

                       sim.getReadyQueue().remove(((Process) tempArray[i]));

                       sim.getBlockedList().add(((Process) tempArray[i]));

                   }

               }

               info.setText(info.getText() + " Blocked Process "

                       + (String) blockBox.getSelectedItem() + " "

                       + sim.getStatus());

               addDialog.dispose();

           }

       });

       leftPanel.add(block, BorderLayout.NORTH);

       leftPanel.add(blockBox, BorderLayout.SOUTH);

       final JComboBox<String> unBlockBox = new JComboBox();

       for (int i = 0; i < sim.getBlockedList().size(); i++) {

           unBlockBox.addItem(sim.getBlockedList().get(i).getID());

       }

       unBlockBox.setEditable(false);

       JButton unBlock = new JButton("UnBlock");

       unBlock.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {

               for(int i=0; i<sim.getBlockedList().size();i++)

               {

                   if(sim.getBlockedList().get(i).getID().equals((String)unBlockBox.getSelectedItem()))

                   {

                       sim.getReadyQueue().add(sim.getBlockedList().get(i));

                     sim.getBlockedList().remove(i);

                   }

               }

               info.setText(info.getText() + " UnBlocked Process "

                       + (String) unBlockBox.getSelectedItem() + " "

                       + sim.getStatus());

               addDialog.dispose();

           }

       });

       rightPanel.add(unBlock, BorderLayout.NORTH);

       rightPanel.add(unBlockBox, BorderLayout.SOUTH);

       miniPanel.add(title, BorderLayout.NORTH);

       miniPanel.add(leftPanel, BorderLayout.WEST);

       miniPanel.add(rightPanel, BorderLayout.EAST);

       addDialog.setTitle("Block/Unblock Process");

       addDialog.add(miniPanel);

       addDialog.setResizable(false);

       addDialog.pack();

       addDialog.setLocationRelativeTo(null);

       addDialog.setModal(true);

       addDialog.setAlwaysOnTop(true);

       addDialog.setModalityType(ModalityType.APPLICATION_MODAL);

       addDialog.setVisible(true);

   }

   private void terminationBox() {

       final JDialog addDialog = new JDialog();

       JPanel miniPanel = new JPanel(new BorderLayout());

       miniPanel.setPreferredSize(new Dimension(125, 75));

       JLabel title = new JLabel(" Select Process");

       JButton kill = new JButton("Kill");

       final JComboBox<String> termBox = new JComboBox();

       kill.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {

               Object[] tempArray = sim.getReadyQueue().toArray();

               for (int i = 0; i < tempArray.length; i++) {

                   if (((Process) tempArray[i]).getID().equals(

                           (String) termBox.getSelectedItem()))

                       sim.getReadyQueue().remove(((Process) tempArray[i]));

               }

               for (int i = 0; i < sim.getBlockedList().size(); i++) {

                   if (sim.getBlockedList().get(i).getID()

                           .equals((String) termBox.getSelectedItem()))

                       sim.getBlockedList().remove(i);

               }

               info.setText(info.getText() + " Terminated Process "

                       + (String) termBox.getSelectedItem() + " "

                       + sim.getStatus());

               addDialog.dispose();

           }

       });

       Object[] tempP = sim.getReadyQueue().toArray();

       for (int i = 0; i < tempP.length; i++) {

           termBox.addItem(((Process) tempP[i]).getID());

       }

       for (int i = 0; i < sim.getBlockedList().size(); i++) {

           termBox.addItem(sim.getBlockedList().get(i).getID());

       }

       termBox.setEditable(false);

       miniPanel.add(title, BorderLayout.NORTH);

       miniPanel.add(kill, BorderLayout.CENTER);

       miniPanel.add(termBox, BorderLayout.SOUTH);

       addDialog.setTitle("Kill Process");

       addDialog.add(miniPanel);

       addDialog.setResizable(false);

       addDialog.pack();

       addDialog.setLocationRelativeTo(null);

       addDialog.setModal(true);

       addDialog.setAlwaysOnTop(true);

       addDialog.setModalityType(ModalityType.APPLICATION_MODAL);

       addDialog.setVisible(true);

   }

   private void initComponents() {

       setTitle("CS 471 Dispatcher Simulator");

       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       setResizable(false);

       panel = new JPanel(new BorderLayout());

       panel.setPreferredSize(new Dimension(550, 380));

       add(panel);

       menuBar = new JMenuBar();

       menu = new JMenu("Options");

       addNew = new JMenuItem("Add New");

       addNew.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {

               addNewBox();

           }

       });

       readMe = new JMenuItem("Readme");

       readMe.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {

               readMe();

           }

       });

       unBlock = new JMenuItem("(Un)Block");

       unBlock.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {

               blockerBox();

           }

       });

       terminate = new JMenuItem("Terminate");

       terminate.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {

               terminationBox();

           }

       });

       exit = new JMenuItem("Exit");

       exit.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {

               dispose();

           }

       });

       menu.add(addNew);

       menu.add(unBlock);

       menu.add(terminate);

       menu.add(readMe);

       menu.add(exit);

       menuBar.add(menu);

       cSwitch = new JButton("Context Switch");

       cSwitch.setPreferredSize(new Dimension(150, 50));

       cSwitch.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {

               sim.contextSwitch();

               info.setText(info.getText() + " " + CS + (count++) + " "

                       + sim.getStatus());

           }

       });

       info = new JTextArea();

       info.setEditable(false);

       scroll = new JScrollPane(info, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

               JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

       scroll.setToolTipText("Output");

       info.setText(sim.getStatus());

       scroll.setPreferredSize(new Dimension(400, 300));

       panel.add(scroll, BorderLayout.EAST);

       panel.add(cSwitch, BorderLayout.WEST);

       setJMenuBar(menuBar);

       setLocation(650, 350);

       setVisible(true);

       pack();

   }

   private void readMe() {

       String readMe = "CS471 Operating Systems Summer Session Term Project "

               + "Author: Matthew Redenius " + "UIN: 00773960 "

               + "Name: Dispatcher Simulator 1.0 "

               + "This program is intended to simulate a dispatcher "

               + "using a priority queue system. This program has a "

               + "fake priority queue, blocked list, and running "

               + "process. It allows the user to add and terminate "

               + "processes in the ready queue and blocked list. "

               + "It also allows the user to block processss in the "

               + "ready queue, and unblock processes in the blocked "

               + "list "

               + "When adding a process, the user only needs to enter "

               + "the desired priority and the ID is automatically "

               + "generated. "

               + "Once all desired processes are loaded, the user can "

               + "perform a context switch to load the next process in "

               + "the ready queue into execution. "

               + " In main, there is test data provided.";

       JFrame frame = new JFrame("README");

       JPanel panel = new JPanel();

       JTextArea label = new JTextArea(readMe);

       panel.add(label);

       frame.add(panel);

       frame.setResizable(false);

       frame.pack();

       frame.setLocationRelativeTo(null);

       frame.setVisible(true);

   }

   public static void errorMessage(String message) {

       JDialog addDialog = new JDialog();

       JPanel miniPanel = new JPanel(new BorderLayout());

       miniPanel.setPreferredSize(new Dimension(175, 100));

       JLabel mess = new JLabel(message);

       miniPanel.add(mess);

     

       addDialog.add(miniPanel);

       addDialog.setTitle("ERROR");

       addDialog.setResizable(false);

       addDialog.pack();

       addDialog.setLocationRelativeTo(null);

       addDialog.setModal(true);

       addDialog.setAlwaysOnTop(true);

       addDialog.setModalityType(ModalityType.APPLICATION_MODAL);

       addDialog.setVisible(true);

   }

}

=======================================================================

//Process.java

public class Process {

   private int priority;

   private String id;

   private boolean executing;

   private boolean blocked;

   private boolean ready;

   public Process(int p, String i)

   {

       id=i;

       priority=p;

       executing=false;

       blocked=false;

       ready=false;

   }

   public String getID()

   {

       return id;

   }

   public int getPriority()

   {

       return priority;

   }

   public boolean isExecuting()

   {

       return executing;

   }

   public boolean isReady()

   {

       return ready;

   }

   public boolean isBlocked()

   {

       return blocked;

   }

   public void setExec(boolean e)

   {

       executing=e;

   }

   public void setBlocked(boolean b)

   {

       blocked=b;

   }

   public void setReady(boolean r)

   {

       ready=r;

   }

}

========================================================================

//simulator.java

import java.util.ArrayList;

import java.util.Comparator;

import java.util.PriorityQueue;

import java.util.Random;

public class simulator {

   private PriorityQueue<Process> readyQueue;

   private Process runningProcess;

   private ArrayList<Process> blockedList;

   private ArrayList<Process> allProcesses;

   private Random generator;

   public simulator(int queueSize) {

       generator = new Random(9999);

       Comparator<Process> comparator = new ProcessComparator();

       readyQueue = new PriorityQueue<Process>(queueSize, comparator);

       blockedList = new ArrayList<Process>(queueSize);

       allProcesses = new ArrayList<Process>(queueSize);

   }

   public void start() {

       runningProcess = removeProcess();

       runningProcess.setReady(false);

       runningProcess.setBlocked(false);

       runningProcess.setExec(true);

   }

   public void print() {

       while (readyQueue.size() != 0) {

           System.out.println(removeProcess().getID());

       }

   }

   public void addProcessReady(int pr) {

       Process p = makeNewProcess(pr);

       p.setReady(true);

       p.setBlocked(false);

       p.setExec(false);

       readyQueue.add(p);

   }

   public void addProcessBlocked(int pr) {

       Process p = makeNewProcess(pr);

       p.setReady(false);

       p.setBlocked(true);

       p.setExec(false);

       blockedList.add(p);

   }

   public Process makeNewProcess(int p) {

       String ran = Integer.toString(generator.nextInt(9999));

       Process i = new Process(p, ran);

       return i;

   }

   public Process removeProcess() {

       return readyQueue.remove();

   }

   public Process getRunningProcess() {

       return runningProcess;

   }

   public ArrayList<Process> getBlockedList() {

       return blockedList;

   }

   public ArrayList<Process> getAllProcesses() {

       return allProcesses;

   }

   public PriorityQueue<Process> getReadyQueue() {

       return readyQueue;

   }

   public String getStatus() {

       String init = "Running ";

       if (runningProcess != null) {

          init = init + " Process ID: " + runningProcess.getID() + " "

                   + "Priority: " + runningProcess.getPriority() + " ";

       } else {

           init = init + " Process ID: " + "NULL" + " " + "Priority: "

                   + "NULL" + " ";

       }

       init = init + "Ready Queue ";

       if (!readyQueue.isEmpty()) {

           Object[] tempP = getReadyQueue().toArray();

           for (int i = 0; i < tempP.length; i++) {

               Process tempProc = readyQueue.remove();

               init = init + " Process ID: " + tempProc.getID() + " "

                       + "Priority: " + tempProc.getPriority() + " ";

           }

           for (int i = 0; i < tempP.length; i++) {

               readyQueue.add((Process) tempP[i]);

           }

       } else {

           init = init + " Empty ";

       }

       init = init + "BlockedList ";

       if (!blockedList.isEmpty()) {

           for (int i = 0; i < blockedList.size(); i++) {

               init = init + " Process ID: " + blockedList.get(i).getID()

                       + " " + "Priority: "

                       + blockedList.get(i).getPriority() + " ";

           }

       } else {

           init = init + " Empty ";

       }

       return init;

   }

   public void contextSwitch() {

       if (!readyQueue.isEmpty()) {

           runningProcess = readyQueue.remove();

       } else {

           runningProcess = null;

       }

   }

   private class ProcessComparator implements Comparator<Process> {

       public int compare(Process arg0, Process arg1) {

           if (arg0.getPriority() < arg1.getPriority()) {

               return -1;

           }

           if (arg0.getPriority() > arg1.getPriority()) {

               return 1;

           }

           return 0;

       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote