You are to simulate a dispatcher using a priority queue system. New processes ar
ID: 3693768 • Letter: Y
Question
You are to simulate a dispatcher using a priority queue system. 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 immaterial. Assume only one CPU. Priorities and numbers of processes can be kept small, just big enough to demonstrate the below listed functionality. You may pre-populate the queues initially from a data file.
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.
4. A process status on GUI command (p#, priority and current state).
You are to turn in your source code, an executable, and screen shots of the output demonstrating the functionality listed above. If you use any data files as initial input, provide them also.
This can be written in Java or C++.
Explanation / Answer
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.even.ActionEvent;
import javax.util.PriorityQueue;
import javax.util.swing.JButton;
import javax.util.swing.JFrame;
import javax.util.swing.JLabel;
import javax.util.swing.JTextArea;
import javax.util.swing.JTextField;
public class Dispatcher extends JFrame implements ActionListener
{
static int processNo=0;
static Process currentlyRunning;
static Queue<Process> readyQueue;
static Queue<Process> blockedQueue;
static JButton newProcess;
static JButton contextSwitch;
static JTextField priority;
static JTextArea Queues;
dispatcher()
{
container cp=getContentpane();
cp.setLayout(new FlowLayout() );
queues=new JTextArea(5,15);
Queues.setPreferredSize(new Dimension(500,400));
cp.add(Queues);
cp.add(new JLabel("Priority"));
priority=new JTextField(6);
newProcess=new JButton("Create Process");
newProcess.addActionListener(this);
cp.add(priority);
cp.add(newProcess);
contextSwitch=new JButton("Context Switch");
contextSwitch.addActionListener(this);
cp.add(contextSwitch);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Dispatcher");
setSize(600,500);
setVisible(true);
}
public static void main(String args[])
{
Dispatcher d=new Dispatcher();
readyQueue=new priorityQueue<>();
blockedQueue=new priorityQueue<>();
Process p1=new Process(ProcessNo++,2,"Running");
Process p2=new Process(ProcessNo++,1,"Blocked");
Process p3=new Process(ProcessNo++4,"Ready");
Process p4=new Process(ProcessNo++,3,"Ready");
Process p5=new Process(ProcessNo++,5,"Ready");
Process p6=new Process(ProcessNo++,8,"Blocked");
readyQueue.add(p3);
readyQueue.add(p4);
readyQueue.add(p5);
blockedQueue.add(p6);
blockedQueue.add(p2);
currentlyRunning=p1;
contextSwitch();
}
static void createProcess(int priority)
{
readyQueue.add(new Process(processNo++,priority,"ready"));
}
static void contextSwitch()
{
String sb;
sb="Currently Running Process"+" "+Process No Priority CurrentState "+currentlyRunning.processNo+" "+currentlyRunning.priority+" " +currenltyRunning.currentstate+" ";
currentlyRunning.currentState="Blocked";
blockedQueue.add(currentlyRunning);
currentlyRunning=readyqueue.poll();
currentlyRunning.currentState="Running";
sb=sb + "Ready Queue "+"Process No Priority currentState ");
for(Process p:readyQueue)
{
sb=sb+p.processNo + " "+p.priority+" "+p.currentState+" ";
Queues.settext(sb);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==newProcess)
{
System.out.print("process");
int p=Integer.parseInt(priority.getText());
p=(p<0)?2:p;
createprocess(p);
}
else
{
System.out.print("context");
contextSwitch();
}
}
}
output:
Currently Running Process
Process No priority CurrentState
3 3 Running
Ready Queue
Process No Priority Current State
4 5 Ready
6 9 Ready
Blocked Queue
Process No Priority Current State
1 1 Blocked
3 3 Blocked
0 2 Blocked
5 8 Blocked
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.