SlideShare a Scribd company logo
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
Solution
//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 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 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 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 readyQueue;
private Process runningProcess;
private ArrayList blockedList;
private ArrayList allProcesses;
private Random generator;
public simulator(int queueSize) {
generator = new Random(9999);
Comparator comparator = new ProcessComparator();
readyQueue = new PriorityQueue(queueSize, comparator);
blockedList = new ArrayList(queueSize);
allProcesses = new ArrayList(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 getBlockedList() {
return blockedList;
}
public ArrayList getAllProcesses() {
return allProcesses;
}
public PriorityQueue getReadyQueue() {
return readyQueue;
}
public String getStatus() {
String init = "Running ";
if (runningProcess != null) {
init = init + "tProcess ID: " + runningProcess.getID() + " "
+ "Priority: " + runningProcess.getPriority() + " ";
} else {
init = init + "tProcess 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 + "tProcess ID: " + tempProc.getID() + " "
+ "Priority: " + tempProc.getPriority() + " ";
}
for (int i = 0; i < tempP.length; i++) {
readyQueue.add((Process) tempP[i]);
}
} else {
init = init + "tEmpty ";
}
init = init + "BlockedList ";
if (!blockedList.isEmpty()) {
for (int i = 0; i < blockedList.size(); i++) {
init = init + "tProcess ID: " + blockedList.get(i).getID()
+ " " + "Priority: "
+ blockedList.get(i).getPriority() + " ";
}
} else {
init = init + "tEmpty ";
}
return init;
}
public void contextSwitch() {
if (!readyQueue.isEmpty()) {
runningProcess = readyQueue.remove();
} else {
runningProcess = null;
}
}
private class ProcessComparator implements Comparator {
public int compare(Process arg0, Process arg1) {
if (arg0.getPriority() < arg1.getPriority()) {
return -1;
}
if (arg0.getPriority() > arg1.getPriority()) {
return 1;
}
return 0;
}
}
}

More Related Content

PDF
Below is the question I need help with. It need to be done in Java. .pdf
DOCX
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
DOCX
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
DOCX
you are to simulate a dispatcher using a priority queue system. .docx
PDF
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
DOCX
i need a output screen shot for this code import java-awt-BorderLayou.docx
PDF
This is the code for the above 5 public class Input extends JFram.pdf
PDF
Java File
Below is the question I need help with. It need to be done in Java. .pdf
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
you are to simulate a dispatcher using a priority queue system. .docx
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
i need a output screen shot for this code import java-awt-BorderLayou.docx
This is the code for the above 5 public class Input extends JFram.pdf
Java File

Similar to You are to simulate a dispatcher using a priority queue system in C+.pdf (8)

RTF
Easy Button
DOCX
New microsoft office word document
PDF
I Need Serious Help with this assignment. Plenty Points to be earned.pdf
DOCX
Programming Assignment #2CSci 430 Spring 2019Dates.docx
DOCX
Programming Assignment #2CSci 430 Spring 2019Dates.docx
PPTX
DOCX
Assignment 02 Process State SimulationCSci 430 Introduction to.docx
Easy Button
New microsoft office word document
I Need Serious Help with this assignment. Plenty Points to be earned.pdf
Programming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docx
Assignment 02 Process State SimulationCSci 430 Introduction to.docx
Ad

More from JUSTSTYLISH3B2MOHALI (20)

PDF
In a business projects What material is typically contained in a pro.pdf
PDF
Implement the following flowchart in java. Start Request integer k f.pdf
PDF
If no chiasma forms between homologous chromosomes, what happens Th.pdf
PDF
If two organims form a symbiotic realtionship where they share t.pdf
PDF
I would appreciate help with these 4 questions. Thank You.1) Expla.pdf
PDF
How do I know when to use the sin^2+cos^2=1 identity....12 angle id.pdf
PDF
I have a stack in Java populated with integers. Im trying to compa.pdf
PDF
Hint List of commands to read and use use of wild card characters .pdf
PDF
Explain in Detail DTE-DCE TransmissionSolutionFirstly the DTE-.pdf
PDF
Explain the data component of social media information systems (SMIS).pdf
PDF
Figure CWhich structure in the cell shown in Figure C above stores.pdf
PDF
Coral reefs. How sensitive to changes in water temperature are coral .pdf
PDF
•0.336 moles of a weak, monoproticacid added to a final volume of 2..pdf
PDF
Write a program that accepts an arithmetic expression of unsigned in.pdf
PDF
Why was the knowledge of macromolecules “structure” very important i.pdf
PDF
Why did the sovereign debt problem of Greece a country that accounts.pdf
PDF
Which of the following could be the most likely cause of a superi.pdf
PDF
What is the theory of public debt managementSolution1. Sove.pdf
PDF
This is a three part question. For each part the answer requires ide.pdf
PDF
Complete a personal SWOT analysis evaluating your understanding and .pdf
In a business projects What material is typically contained in a pro.pdf
Implement the following flowchart in java. Start Request integer k f.pdf
If no chiasma forms between homologous chromosomes, what happens Th.pdf
If two organims form a symbiotic realtionship where they share t.pdf
I would appreciate help with these 4 questions. Thank You.1) Expla.pdf
How do I know when to use the sin^2+cos^2=1 identity....12 angle id.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
Hint List of commands to read and use use of wild card characters .pdf
Explain in Detail DTE-DCE TransmissionSolutionFirstly the DTE-.pdf
Explain the data component of social media information systems (SMIS).pdf
Figure CWhich structure in the cell shown in Figure C above stores.pdf
Coral reefs. How sensitive to changes in water temperature are coral .pdf
•0.336 moles of a weak, monoproticacid added to a final volume of 2..pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
Why was the knowledge of macromolecules “structure” very important i.pdf
Why did the sovereign debt problem of Greece a country that accounts.pdf
Which of the following could be the most likely cause of a superi.pdf
What is the theory of public debt managementSolution1. Sove.pdf
This is a three part question. For each part the answer requires ide.pdf
Complete a personal SWOT analysis evaluating your understanding and .pdf
Ad

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Trump Administration's workforce development strategy
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Classroom Observation Tools for Teachers
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Anesthesia in Laparoscopic Surgery in India
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Microbial diseases, their pathogenesis and prophylaxis
Trump Administration's workforce development strategy
STATICS OF THE RIGID BODIES Hibbelers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Paper A Mock Exam 9_ Attempt review.pdf.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
UNIT III MENTAL HEALTH NURSING ASSESSMENT
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Classroom Observation Tools for Teachers
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Anesthesia in Laparoscopic Surgery in India
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...

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

  • 1. 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 Solution //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); } } ===================================================================== =========
  • 2. //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) {
  • 3. 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) {
  • 4. 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 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() {
  • 5. 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 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 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()
  • 6. .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));
  • 7. 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);
  • 8. 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 "
  • 9. + "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); }
  • 10. } ===================================================================== == //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() {
  • 11. 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 readyQueue; private Process runningProcess; private ArrayList blockedList; private ArrayList allProcesses; private Random generator; public simulator(int queueSize) { generator = new Random(9999); Comparator comparator = new ProcessComparator(); readyQueue = new PriorityQueue(queueSize, comparator); blockedList = new ArrayList(queueSize); allProcesses = new ArrayList(queueSize); } public void start() {
  • 12. 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 getBlockedList() {
  • 13. return blockedList; } public ArrayList getAllProcesses() { return allProcesses; } public PriorityQueue getReadyQueue() { return readyQueue; } public String getStatus() { String init = "Running "; if (runningProcess != null) { init = init + "tProcess ID: " + runningProcess.getID() + " " + "Priority: " + runningProcess.getPriority() + " "; } else { init = init + "tProcess 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 + "tProcess ID: " + tempProc.getID() + " " + "Priority: " + tempProc.getPriority() + " "; } for (int i = 0; i < tempP.length; i++) { readyQueue.add((Process) tempP[i]); } } else { init = init + "tEmpty "; } init = init + "BlockedList "; if (!blockedList.isEmpty()) { for (int i = 0; i < blockedList.size(); i++) { init = init + "tProcess ID: " + blockedList.get(i).getID() + " " + "Priority: "
  • 14. + blockedList.get(i).getPriority() + " "; } } else { init = init + "tEmpty "; } return init; } public void contextSwitch() { if (!readyQueue.isEmpty()) { runningProcess = readyQueue.remove(); } else { runningProcess = null; } } private class ProcessComparator implements Comparator { public int compare(Process arg0, Process arg1) { if (arg0.getPriority() < arg1.getPriority()) { return -1; } if (arg0.getPriority() > arg1.getPriority()) { return 1; } return 0; } } }