import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.BorderFactory.*;
import java.security.SecureRandom;
public class CPUScheduling extends JFrame {
// Variables declaration
private JPanel ProcessSettingPanel;
private JPanel ProcessListPanel;
private JPanel AlgorithmPanel;
private int[] ArrivalTime = new int[99];
private int[] BurstTime = new int[99];
private int[] PriorityLevel = new int[99];
private int[] ProcessMemory = new int[99];
private JLabel PanelHeading;
private JLabel NoProcessLabel;
private JSpinner NoProcessSpinner;
private JLabel PanelSubHeading;
private JLabel ArrivalLabel;
private JSpinner ArrivalSpinner;
private JLabel BurstLabel;
private JSpinner BurstSpinner;
private JLabel PriorityLabel;
private JSpinner PrioritySpinner;
private JLabel MemoryLabel;
private JSpinner MemorySpinner;
private JButton ResetButton;
private JButton GenerateButton;
private JTextArea ProcessList;
private JScrollPane ProcessListScroll;
private JButton ContinueButton;
private JComboBox AlgorithmComboBox;
private String[] AlgorithmsName = {"", "Round-Robin", "SJF with Priority", "MM - Worst-Fit", "FIFO"};
private JLabel AlgorithmLabel;
private JPanel CPUSchedulingPanel;
private JButton CalculateButton;
private JRadioButton Frames3RadioButton;
private JRadioButton Frames4RadioButton;
private int NoOfFrames;
private ButtonGroup FramesButtonGroup;
private JPanel MemoryManagementPanel;
private JLabel NoFrameLabel;
private JLabel NoPartitionLabel;
private JSpinner NoPartitionSpinner;
private JLabel PageNoLabel;
private JSpinner PageNoSpinner;
private JLabel PartitionLengthLabel;
private JSpinner PartitionLengthSpinner;
private JRadioButton Quantum2RadioButton;
private JRadioButton Quantum3RadioButton;
private JRadioButton Quantum4RadioButton;
private JRadioButton Quantum5RadioButton;
private int TimeQuantum;
private JRadioButton String10RadioButton;
private JRadioButton String20RadioButton;
private int StringLength;
private ButtonGroup TimeQuantumButtonGroup;
private ButtonGroup StringLengthButtonGroup;
private JLabel StringLengthLabel;
private JLabel TimeQuantumLabel;
private JPanel VirtualMemoryPanel;
public static void main(String[] args){
CPUScheduling cs = new CPUScheduling();
}
public CPUScheduling(){
setLayout(new BorderLayout());
// ProcessSettingPanel Stuff ---- Start ----
ProcessSettingPanel = new JPanel();
PanelHeading = new JLabel("Process Setting", JLabel.CENTER);
NoProcessLabel = new JLabel(" No. of Process :");
NoProcessSpinner = new JSpinner(new SpinnerNumberModel(1,1,9,1));
NoProcessSpinner.setBounds(100,96,100,24);
PanelSubHeading = new JLabel("----- Range Setting -----");
ArrivalLabel = new JLabel("Arrival Time : 0 -");
ArrivalSpinner = new JSpinner(new SpinnerNumberModel(0,0,9,1));
ArrivalSpinner.setBounds(100,96,100,24);
BurstLabel = new JLabel("Burst Time : 1 -");
BurstSpinner = new JSpinner(new SpinnerNumberModel(1,1,9,1));
BurstSpinner.setBounds(100,96,100,24);
PriorityLabel = new JLabel("Priority Level : 0 -");
PrioritySpinner = new JSpinner(new SpinnerNumberModel(0,0,9,1));
PrioritySpinner.setBounds(100,96,100,24);
MemoryLabel = new JLabel("Memory (KB): 1 -");
MemorySpinner = new JSpinner(new SpinnerNumberModel(1,1,900,25));
MemorySpinner.setBounds(100,96,100,24);
ResetButton =new JButton("Reset");
GenerateButton =new JButton("Generate");
ResetButton.addActionListener(new resetButtonListener());
GenerateButton.addActionListener(new generateButtonListener());
//Start of Setting Panel Layout
GroupLayout ProcessSettingPanelLayout = new GroupLayout(ProcessSettingPanel);
ProcessSettingPanel.setLayout(ProcessSettingPanelLayout);
ProcessSettingPanelLayout.setHorizontalGroup(
ProcessSettingPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(ProcessSettingPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(ProcessSettingPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(NoProcessLabel)
.addComponent(ArrivalLabel)
.addComponent(BurstLabel)
.addComponent(MemoryLabel)
.addComponent(PriorityLabel))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(ProcessSettingPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(NoProcessSpinner, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
.addComponent(ArrivalSpinner, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
.addComponent(BurstSpinner, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
.addComponent(PrioritySpinner, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
.addComponent(MemorySpinner, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
.addContainerGap(35, Short.MAX_VALUE))
.addGroup(ProcessSettingPanelLayout.createSequentialGroup()
.addGap(48, 48, 48)
.addComponent(PanelHeading)
.addContainerGap(70, Short.MAX_VALUE))
.addGroup(ProcessSettingPanelLayout.createSequentialGroup()
.addGap(65, 65, 65)
.addComponent(PanelSubHeading)
.addContainerGap(75, Short.MAX_VALUE))
.addGroup(GroupLayout.Alignment.TRAILING, ProcessSettingPanelLayout.createSequentialGroup()
.addContainerGap(164, Short.MAX_VALUE)
.addComponent(GenerateButton)
.addContainerGap())
.addGroup(GroupLayout.Alignment.TRAILING, ProcessSettingPanelLayout.createSequentialGroup()
.addContainerGap(180, Short.MAX_VALUE)
.addComponent(ResetButton)
.addContainerGap())
);
ProcessSettingPanelLayout.setVerticalGroup(
ProcessSettingPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(ProcessSettingPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(PanelHeading)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(ProcessSettingPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(NoProcessLabel)
.addComponent(NoProcessSpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(PanelSubHeading, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE)
.addGap(22, 22, 22)
.addGroup(ProcessSettingPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(ArrivalLabel)
.addComponent(ArrivalSpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(ProcessSettingPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(BurstLabel)
.addComponent(BurstSpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(10, 10, 10)
.addGroup(ProcessSettingPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(PriorityLabel)
.addComponent(PrioritySpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(10, 10, 10)
.addGroup(ProcessSettingPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(MemoryLabel)
.addComponent(MemorySpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(53, 53, 53)
.addComponent(ResetButton)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(GenerateButton)
.addContainerGap(52, Short.MAX_VALUE))
);
// End of Setting Panel Layout
// ProcessListPanel ----Start----
ProcessListPanel = new JPanel();
ProcessList = new JTextArea();
ProcessList.setColumns(20);
ProcessList.setRows(5);
ProcessList.setEditable(false);
ProcessListScroll = new JScrollPane(ProcessList);
ContinueButton = new JButton("Continue");
ContinueButton.setEnabled(false);
ContinueButton.addActionListener(new continueButtonListener());
GroupLayout ProcessListPanelLayout = new GroupLayout(ProcessListPanel);
ProcessListPanel.setLayout(ProcessListPanelLayout);
ProcessListPanelLayout.setHorizontalGroup(
ProcessListPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(ProcessListPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(ProcessListPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(ContinueButton, GroupLayout.Alignment.TRAILING)
.addComponent(ProcessListScroll, GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE))
.addContainerGap())
);
ProcessListPanelLayout.setVerticalGroup(
ProcessListPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING, ProcessListPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(ProcessListScroll, GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(ContinueButton)
.addContainerGap())
);
// ProcessListPanel end
// AlgorithmPanel Start
AlgorithmPanel = new JPanel();
AlgorithmLabel = new JLabel("Algorithm");
AlgorithmComboBox = new JComboBox(AlgorithmsName);
AlgorithmComboBox.addItemListener(new AlgorithmComboBoxHandler());
CPUSchedulingPanel = new JPanel();
CPUSchedulingPanel.setBorder(BorderFactory.createTitledBorder(null, "CPU Scheduling", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION));
TimeQuantumLabel = new JLabel("Time Quantum");
Quantum2RadioButton = new JRadioButton("2");
Quantum3RadioButton = new JRadioButton("3");
Quantum4RadioButton = new JRadioButton("4");
Quantum5RadioButton = new JRadioButton("5");
TimeQuantumButtonGroup = new ButtonGroup();
TimeQuantumButtonGroup.add(Quantum2RadioButton);
TimeQuantumButtonGroup.add(Quantum3RadioButton);
TimeQuantumButtonGroup.add(Quantum4RadioButton);
TimeQuantumButtonGroup.add(Quantum5RadioButton);
MemoryManagementPanel = new JPanel();
MemoryManagementPanel.setBorder(BorderFactory.createTitledBorder(null, "Memory Management", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION));
NoPartitionLabel = new JLabel("No. of Partition : 1 -");
NoPartitionSpinner = new JSpinner(new SpinnerNumberModel(1,1,5,1));
PartitionLengthLabel = new JLabel("Partition Length(Kb) : 100 -");
PartitionLengthSpinner = new JSpinner(new SpinnerNumberModel(100,100,900,50));
VirtualMemoryPanel = new JPanel();
VirtualMemoryPanel.setBorder(BorderFactory.createTitledBorder(null, "Virtual Memory", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION));
NoFrameLabel = new JLabel("No. of Frames :");
Frames3RadioButton = new JRadioButton("3");
Frames4RadioButton = new JRadioButton("4");
FramesButtonGroup = new ButtonGroup();
FramesButtonGroup.add(Frames3RadioButton);
FramesButtonGroup.add(Frames4RadioButton);
StringLengthLabel = new JLabel("String Length :");
String10RadioButton = new JRadioButton("10");
String20RadioButton = new JRadioButton("20");
StringLengthButtonGroup = new ButtonGroup();
StringLengthButtonGroup.add(String10RadioButton);
StringLengthButtonGroup.add(String20RadioButton);
PageNoLabel = new JLabel("Page No. : 0 -");
PageNoSpinner = new JSpinner(new SpinnerNumberModel(0,0,9,1));
CalculateButton = new JButton("Calculate");
GroupLayout CPUSchedulingPanelLayout = new GroupLayout(CPUSchedulingPanel);
CPUSchedulingPanel.setLayout(CPUSchedulingPanelLayout);
CPUSchedulingPanelLayout.setHorizontalGroup(
CPUSchedulingPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(CPUSchedulingPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(TimeQuantumLabel)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Quantum2RadioButton)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Quantum3RadioButton)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Quantum4RadioButton)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Quantum5RadioButton)
.addContainerGap(18, Short.MAX_VALUE))
);
CPUSchedulingPanelLayout.setVerticalGroup(
CPUSchedulingPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(CPUSchedulingPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(CPUSchedulingPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(TimeQuantumLabel)
.addComponent(Quantum2RadioButton)
.addComponent(Quantum3RadioButton)
.addComponent(Quantum4RadioButton)
.addComponent(Quantum5RadioButton))
.addContainerGap(17, Short.MAX_VALUE))
);
GroupLayout MemoryManagementPanelLayout = new GroupLayout(MemoryManagementPanel);
MemoryManagementPanel.setLayout(MemoryManagementPanelLayout);
MemoryManagementPanelLayout.setHorizontalGroup(
MemoryManagementPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(MemoryManagementPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(MemoryManagementPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(MemoryManagementPanelLayout.createSequentialGroup()
.addComponent(NoPartitionLabel)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
.addComponent(NoPartitionSpinner, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))
.addGroup(MemoryManagementPanelLayout.createSequentialGroup()
.addComponent(PartitionLengthLabel)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(PartitionLengthSpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
MemoryManagementPanelLayout.setVerticalGroup(
MemoryManagementPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(MemoryManagementPanelLayout.createSequentialGroup()
.addGroup(MemoryManagementPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(NoPartitionLabel)
.addComponent(NoPartitionSpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(MemoryManagementPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(PartitionLengthLabel)
.addComponent(PartitionLengthSpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
GroupLayout VirtualMemoryPanelLayout = new GroupLayout(VirtualMemoryPanel);
VirtualMemoryPanel.setLayout(VirtualMemoryPanelLayout);
VirtualMemoryPanelLayout.setHorizontalGroup(
VirtualMemoryPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(VirtualMemoryPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(VirtualMemoryPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addGroup(VirtualMemoryPanelLayout.createSequentialGroup()
.addGroup(VirtualMemoryPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(NoFrameLabel)
.addComponent(StringLengthLabel))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(VirtualMemoryPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(String10RadioButton)
.addComponent(Frames3RadioButton))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(VirtualMemoryPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(String20RadioButton)
.addComponent(Frames4RadioButton)))
.addGroup(VirtualMemoryPanelLayout.createSequentialGroup()
.addComponent(PageNoLabel)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(PageNoSpinner)))
.addContainerGap(75, Short.MAX_VALUE))
);
VirtualMemoryPanelLayout.setVerticalGroup(
VirtualMemoryPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(VirtualMemoryPanelLayout.createSequentialGroup()
.addGroup(VirtualMemoryPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(VirtualMemoryPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(NoFrameLabel)
.addComponent(Frames3RadioButton)
.addComponent(Frames4RadioButton))
.addGroup(VirtualMemoryPanelLayout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(VirtualMemoryPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(String10RadioButton)
.addComponent(String20RadioButton)
.addComponent(StringLengthLabel))))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(VirtualMemoryPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(PageNoLabel)
.addComponent(PageNoSpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
GroupLayout AlgorithmPanelLayout = new GroupLayout(AlgorithmPanel);
AlgorithmPanel.setLayout(AlgorithmPanelLayout);
AlgorithmPanelLayout.setHorizontalGroup(
AlgorithmPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(AlgorithmPanelLayout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(AlgorithmLabel)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(AlgorithmComboBox, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
.addContainerGap(24, Short.MAX_VALUE))
.addGroup(AlgorithmPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(CPUSchedulingPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(19, 19, 19))
.addGroup(AlgorithmPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(MemoryManagementPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(19, 19, 19))
.addGroup(GroupLayout.Alignment.TRAILING, AlgorithmPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(AlgorithmPanelLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(CalculateButton)
.addComponent(VirtualMemoryPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(19, 19, 19))
);
AlgorithmPanelLayout.setVerticalGroup(
AlgorithmPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(AlgorithmPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(AlgorithmPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(AlgorithmLabel)
.addComponent(AlgorithmComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(CPUSchedulingPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(MemoryManagementPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(VirtualMemoryPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(CalculateButton)
.addContainerGap(60, Short.MAX_VALUE))
);
enableChildren(CPUSchedulingPanel,false);
enableChildren(MemoryManagementPanel,false);
enableChildren(VirtualMemoryPanel,false);
CalculateButton.setEnabled(false);
getContentPane().add(ProcessSettingPanel, "Center");
getContentPane().add(ProcessListPanel, "East");
setVisible (true);
setSize(800,500);
setResizable(false);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}} );
}
private class resetButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
NoProcessSpinner.setValue(1);
ArrivalSpinner.setValue(0);
BurstSpinner.setValue(1);
PrioritySpinner.setValue(0);
MemorySpinner.setValue(100);
ProcessList.setText("");
ContinueButton.setEnabled(false);
}
}
private class generateButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
SpinnerNumberModel ProcessModel = (SpinnerNumberModel)NoProcessSpinner.getModel();
SpinnerNumberModel ArrivalModel = (SpinnerNumberModel)ArrivalSpinner.getModel();
SpinnerNumberModel BurstModel = (SpinnerNumberModel)BurstSpinner.getModel();
SpinnerNumberModel PriorityModel = (SpinnerNumberModel)PrioritySpinner.getModel();
SpinnerNumberModel MemoryModel = (SpinnerNumberModel)MemorySpinner.getModel();
SpinnerNumberModel PartitionLengthModel = (SpinnerNumberModel)PartitionLengthSpinner.getModel();
int ProcessObject = ProcessModel.getNumber().intValue();
int ArrivalObject = ArrivalModel.getNumber().intValue();
int BurstObject = BurstModel.getNumber().intValue();
int PriorityObject = PriorityModel.getNumber().intValue();
int MemoryObject = MemoryModel.getNumber().intValue();
Random randomPlease = new Random();
ProcessList.setText("Process No.\tArrival Time\tBurst Time\tPriority\tMemory\n");
for(int i = 0; i < ProcessObject; i ++){
if(ArrivalObject == 0)ArrivalTime[i] = 0;
else ArrivalTime[i] = randomPlease.nextInt(ArrivalObject);
if(BurstObject == 1)BurstTime[i] = 1;
else if(randomPlease.nextInt(BurstObject)== 0)
BurstTime[i] = randomPlease.nextInt(BurstObject)+1;
else BurstTime[i] = randomPlease.nextInt(BurstObject);
if(PriorityObject == 0)PriorityLevel[i] = 0;
else PriorityLevel[i] = randomPlease.nextInt(PriorityObject);
if(MemoryObject == 1)ProcessMemory[i] = 1;
else ProcessMemory[i] = randomPlease.nextInt(MemoryObject);
ProcessList.append(i+"\t"+ArrivalTime[i]+"\t"+BurstTime[i]+"\t"+PriorityLevel[i]+"\t"+ProcessMemory[i]+"\n");
}
ContinueButton.setEnabled(true);
}
}
private class continueButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
getContentPane().invalidate();
getContentPane().removeAll();
getContentPane().add(AlgorithmPanel, "Center");
getContentPane().add(ProcessListPanel, "East");
getContentPane().validate();
}
}
private void enableChildren(Container container, boolean isEnabled)
{
// get an arry of all the components in this container
Component[] components = container.getComponents();
// for each element in the container enable/disable it
for (int i = 0; i < components.length; i++)
{
components[i].setEnabled(isEnabled);
}
}
private class TimeQuantumHandler implements ItemListener {
// handle radio button es
public void itemStateChanged( ItemEvent e )
{// user clicked Quantum2RadioButton
if ( e.getSource() == Quantum2RadioButton )
TimeQuantum = 2;
// user clicked Quantum3RadioButton
else if ( e.getSource() == Quantum3RadioButton )
TimeQuantum = 3;
// user clicked Quantum4RadioButton
else if ( e.getSource() == Quantum4RadioButton )
TimeQuantum = 4;
// user clicked Quantum5RadioButton
else if ( e.getSource() == Quantum5RadioButton )
TimeQuantum = 5;
}
}
private class StringLengthHandler implements ItemListener {
// handle radio button events
public void itemStateChanged( ItemEvent e )
{// user clicked String10RadioButton
if ( e.getSource() == String10RadioButton )
StringLength = 10;
// user clicked String20RadioButton
else if ( e.getSource() == String20RadioButton )
StringLength = 20;
}
}
private class NoOfFramesHandler implements ItemListener {
// handle radio button events
public void itemStateChanged( ItemEvent e )
{// user clicked Frames3RadioButton
if ( e.getSource() == Frames3RadioButton)
NoOfFrames = 3;
// user clicked Frames4RadioButton
else if ( e.getSource() == Frames4RadioButton )
NoOfFrames = 4;
}
}
private class AlgorithmComboBoxHandler implements ItemListener{
public void itemStateChanged(ItemEvent e){
int x = AlgorithmComboBox.getSelectedIndex();
switch(x){
case 0 :
break;
case 1 : enableChildren(CPUSchedulingPanel, true);
enableChildren(MemoryManagementPanel,false);
enableChildren(VirtualMemoryPanel,false);
CalculateButton.setEnabled(true);
break;
case 2 : CalculateButton.setEnabled(true);
enableChildren(CPUSchedulingPanel,false);
enableChildren(MemoryManagementPanel,false);
enableChildren(VirtualMemoryPanel,false);
break;
case 3 : enableChildren(MemoryManagementPanel, true);
enableChildren(CPUSchedulingPanel,false);
enableChildren(VirtualMemoryPanel,false);
CalculateButton.setEnabled(true);
/**
//FORMAT:
//| 1stBLK | 2ndBLK | 3rdBLK | 4thBLK | 5thBLK
//| memA[1] | memA[2]| memA[3]| memA[4]| memA[5] --- MEMORY SIZE (ROWA)
//| memB[1] | memB[2]| memB[3]| memB[4]| memB[5] --- MEMORY ALLOCATION (ROWB)
//| memC[1] | memC[2]| memC[3]| memC[4]| memC[5] --- REMAINING MEMORY LEFT AFTER ROW A-B (ROWC) *** NOT SHOWN, COmmented OUt but it is calculated.
*/
// Needed to generate random numbers
SecureRandom generator = new SecureRandom();
//memory Top Blocks, ROW A - Memory Size
//Generate random values, max value taken from PartitionLengthSpinner
SpinnerNumberModel PartitionLengthModel = (SpinnerNumberModel)PartitionLengthSpinner.getModel();
int PartitionLengthMax = PartitionLengthModel.getNumber().intValue();
int memA1 = generator.nextInt(PartitionLengthMax+1);
int memA2 = generator.nextInt(PartitionLengthMax+1);
int memA3 = generator.nextInt(PartitionLengthMax+1);
int memA4 = generator.nextInt(PartitionLengthMax+1);
int memA5 = generator.nextInt(PartitionLengthMax+1);
int[] memA={0, memA1, memA2, memA3, memA4, memA5};
//Memory Bottom Blocks, row B - EMPTY, to show memory allocation
int memB1=0, memB2=0, memB3=0, memB4=0, memB5=0;
int[] memB={0, memB1, memB2, memB3, memB4, memB5};
//Memory C Blocks, to find the real value after allocation
int memC1=0, memC2=0, memC3=0, memC4=0, memC5=0;
int[] memC={0, memC1, memC2, memC3, memC4, memC5};
//Internal and external Fragmentation
int iFrag1=0,iFrag2=0,iFrag3=0,iFrag4=0,iFrag5=0;
int eFrag1=0,eFrag2=0,eFrag3=0,eFrag4=0,eFrag5=0;
int temp1=0, temp2=0, temp3=0, temp4=0, temp5=0;
//Memory alocation, ignore Ac4 and Ac5. Only Memory Allocation 1-3 (ONLY memAc1-memAc3)
int memAc1 = generator.nextInt(999);
int memAc2 = generator.nextInt(999);
int memAc3 = generator.nextInt(999);
int memAc4 = generator.nextInt(1);
int memAc5 = generator.nextInt(1);
int[] memAc={memAc1,memAc2,memAc3,memAc4,memAc5};
int unallocatedAc1=0, unallocatedAc2=0, unallocatedAc3=0;
//Compare all the memA memory sizes, find the biggest memory among them.
int[] myNum={0, memA[1],memA[2],memA[3],memA[4],memA[5]};
int maxIndex = findMax(myNum);
//Can 1st memory Allocation(MemAc1) fit into 1st biggest memory Size?(memA[maxIndex])
if(memA[maxIndex]>=memAc1)
{
memB[maxIndex]=memAc1;
}
// If not found, it becomes unallocated
else
{
unallocatedAc1=memAc1;
}
//MemC calculates the remainding memory after RowA-RowB
memC[1]=memA[1]-memB[1];
memC[2]=memA[2]-memB[2];
memC[3]=memA[3]-memB[3];
memC[4]=memA[4]-memB[4];
memC[5]=memA[5]-memB[5];
//Start of 2nd allocation
//Can 2nd memory allocation fit into 1st biggest memory size?
//Note it must fit into the remaining size left after the 1st allocation
//ex: if memory allocation: memAc1=100kb, memAc2=200kb
// 100|200|300|400|500
// ---|---|---|---|100 >>>> 1st allocation
// 100|200|300|400|500
// ---|---|---|---|300 >>>> 1st allocation plus 2nd allocation(100+200)
if(memC[maxIndex]>=memAc2)
{
memB[maxIndex]=memB[maxIndex]+memAc2;
}
else //if 2nd allocation doesn't fit, it'll find the 2nd biggest memory size
{
int[] myNum2={0, memC[1],memC[2],memC[3],memC[4],memC[5]};
maxIndex = findMax(myNum2);
//Can 2nd memory allocation fit into 2nd biggest memory size?
if(memC[maxIndex]>=memAc2)
{
memB[maxIndex]=memAc2;
}
else //Else it is unlocatted
{
unallocatedAc2=memAc2;
}
}
//The remaining memory after 2nd memory allocation
memC[1]=memA[1]-memB[1];
memC[2]=memA[2]-memB[2];
memC[3]=memA[3]-memB[3];
memC[4]=memA[4]-memB[4];
memC[5]=memA[5]-memB[5];
//Start of 3rd allocation
//Find out the biggest memory size again
int largestIndex = findMax(myNum);
//If 3rd allocated memory is bigger than remaining space in biggest memory size,
//add 3rd memory allocation to rowB of the biggest memory size
if(memC[largestIndex]>=memAc3)
{
memB[largestIndex]=memB[largestIndex]+memAc3;
}
//if it can't fit into the biggest memory, it try to fit into 2nd biggest memory size
// add 3rd memory allocation to rowB of the 2nd biggest memory size
else if(memC[maxIndex]>=memAc3)
{
memB[maxIndex]=memB[maxIndex]+memAc3;
}
else //if it cannot slot into 1st and 2nd biggest memory size, Slot into the 3rd biggest memory size
{
int[] myNum3={0, memC[1],memC[2],memC[3],memC[4],memC[5]};
maxIndex = findMax(myNum3);
//check if 3rd biggest memory size can fit 3rd memory allocation
if(memC[maxIndex]>=memAc3)
{
memB[maxIndex]=memAc3;
}
else //Else it is unlocatted
{
unallocatedAc3=memAc3;
}
}
//The remaining memory values after 3nd allocation
memC[1]=memA[1]-memB[1];
memC[2]=memA[2]-memB[2];
memC[3]=memA[3]-memB[3];
memC[4]=memA[4]-memB[4];
memC[5]=memA[5]-memB[5];
//Int Fragmentation & Ext Fragmentation calculation
//FORMAT:
//| 1stBLK | 2ndBLK | 3rdBLK | 4thBLK | 5thBLK
//| memA[1] | memA[2]| memA[3]| memA[4]| memA[5] --- MEMORY SIZE
//| memB[1] | memB[2]| memB[3]| memB[4]| memB[5] --- MEMORY ALLOCATION
//| memC[1] | memC[2]| memC[3]| memC[4]| memC[5] --- REMAINING MEMORY LEFT AFTER ROW A-B
//1st Block
//If 1st memory allocation is not 0, then memA[1]-memB[1], which is internal frag for 1st Blk
// else, if 1st memory allocation is 0, then exteral frag for 1st block is memory size 1
if(memB[1]!=0)
{
iFrag1=memA[1]-memB[1];
}
else
{
eFrag1=memA[1];
}
//2nd Block
if(memB[2]!=0)
{
iFrag2=memA[2]-memB[2];
}
else
{
eFrag2=memA[2];
}
//3rd Block
if(memB[3]!=0)
{
iFrag3=memA[3]-memB[3];
}
else
{
eFrag3=memA[3];
}
//4th Block
if(memB[4]!=0)
{
iFrag4=memA[4]-memB[4];
}
else
{
eFrag4=memA[4];
}
//5th Block
if(memB[5]!=0)
{
iFrag5=memA[5]-memB[5];
}
else
{
eFrag5=memA[5];
}
//Internal & External Frag Calculation, add up all internal and external fragmentation.
int iFragT= iFrag1+iFrag2+iFrag3+iFrag4+iFrag5;
int eFragT= eFrag1+eFrag2+eFrag3+eFrag4+eFrag5;
//Display Output
String memAlcTxt= "Memory to be allocated: "+memAc1 +"Kb , " +memAc2 +"Kb , " +memAc3+"kb\n\n";
String memSizeTxt= "| "+memA[1] +"Kb \t| " +memA[2] +"Kb \t| " +memA[3] +"Kb \t| " +memA[4] +"Kb \t| " +memA[5] +"kb\n";
String memAlcedTxt= "| "+memB[1] +"Kb \t| " +memB[2] +"Kb \t| " +memB[3] +"Kb \t| " +memB[4] +"Kb \t| " +memB[5] +"kb \n\n";
// System.out.println("| "+memC[1] +"Kb | " +memC[2] +"Kb | " +memC[3] +"Kb | " +memC[4] +"Kb | " +memC[5] +"kb \n\n");
String intFragTxt= "Internal Frag: " +iFragT +" Kb\n";
String extFragTxt= "External Fragmentation: " +eFragT+" Kb\n";
String unAlcTxt= "Unallocated: " +unallocatedAc1+" Kb, "+unallocatedAc2+" Kb ,"+unallocatedAc3+" Kb";
JOptionPane.showMessageDialog(null, memAlcTxt + memSizeTxt + memAlcedTxt + intFragTxt + extFragTxt + unAlcTxt, "Main Memory - Worst Fit". JOptionPane.PLAIN_MESSAGE);
break;
case 4 : enableChildren(VirtualMemoryPanel, true);
enableChildren(CPUSchedulingPanel,false);
enableChildren(MemoryManagementPanel,false);
CalculateButton.setEnabled(true);
break;
}
}
}
//not working.
private class CalculateButtonListener implements ActionListener{
public void actionPerformed(ActionEvent evt){
//when calculate button is clicked.
if(evt.getSource()==CalculateButton)
ProcessList.setText("Main Memory Tested!");
else
ProcessList.setText("Main Memory NOT-Tested!");
}
}
//Seperate Method to Finding Maximum Memory
public static int findMax(int[]num)
{
int maxNum=num[0];
int index=0;
for(int i=0; i<num.length;i++)
{
if(num[i]>maxNum)
{
maxNum=num[i];
index=i;
}
} return index;
}
} |