Simulate concurrent execution of processes

3, Experimental content
Use C language or JAVA language or C + + language (unlimited means) to simulate the concurrent execution of the process. requirement:
1: The competition for CPU should be reflected in the single CPU environment;
2: It should reflect the disorder of process scheduling without the support of explicit scheduling algorithm;
3: Is to fully reflect the asynchrony of the promotion process;
4: Is to fully reflect the micro performance of concurrent execution process;
5: It is necessary to seriously study the demonstration experiment on CPU competition completed by teachers of University of Electronic Science and technology.
4, Experimental principle (Theory)
Process is the basic unit of resource allocation and scheduling in the system. In the computer, parallel execution in a time period can be realized through scheduling. Because the process advances at an unpredictable speed in the process of concurrent execution, it leads to non reproducibility - asynchrony. Simulate the concurrent execution of processes. By creating multiple processes and giving different priorities, the competition against a single CPU is realized, and the execution status of different processes is displayed according to the graphical interface.
5, List of instruments, equipment and materials (or software) involved in the experiment
Using Java language to program on the eclipse platform, many controls are used.
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
6, Experimental steps and code analysis
Simulate the concurrent execution of processes, observe the execution or running status of each process, and select the execution or pause of the selected process, then we need a simple graphical interface to show the running process of the simulated process. In order to achieve this goal, we write it from bottom to top through the following three processes.
Atomic part - used to display the amount of process change. The class declaration is:
class Atom {
public int x;
public Atom(int x) {
this.x=x;
}
public int getAtom() {
return this.x;
}
}
An integer variable is used as the content and displayed in jtextfield textfile.
Middle level container framework Department - contains a variety of controls such as label, button and textfile, and rewrites JPanel
class myJP extends JPanel {
private JLabel label;// Display process status
JButton button;// The operation button is used for function switching to control the process execution
JTextField textFiled ;// Text box to display the process execution content
public myJP() {/ / used to initialize the control location and content
setLayout(null);
Setborder (borderfactory. Createitledorder ("simulation process");
Textfile = new jtextfield ("specified content + specified length (read-only status)", 30;
Textfile.setfont (new font ("harmonic body", Font.BOLD|Font.ITALIC,16));
//Sets the horizontal alignment of text
textFiled.setHorizontalAlignment(JTextField.CENTER);
textFiled.setBounds(30, 50, 200, 30);
button=new JButton("pause");
button.setBounds(730, 50, 70, 35);
label=new JLabel("simulation process is executing");
label.setBounds(10, 10, 200, 50);
/Add all three controls to the current lightweight container/
add(label);
add(textFiled);
add(button);
setVisible(true);
/Create a listening event for the button and listen to whether the name of the button changes to change the execution state of the process/
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {/ / the problem listener should target its own upper layer
if(button.getText() = = "pause"){
button.setText("execute");
label.setText("simulation process paused");
}
else {
button.setText("pause");
label.setText("simulation process is executing");
}
}
});
}
public void perform(String x) / / used to modify the empty display of text. It will be called in the inherited class rewritten in the lower level
{
textFiled.setText(x);
}
}
Upper use class -- implemented by inheriting Thread class
class myThread extends Thread {
public Atom num; / / atomic class object, which is used as data in the current process
public myJP myjp; / / component framework of the current process
public myThread(Atom b,myJP myjp ) {
this.num=b;
this.myjp=myjp;
}
@Override / / override run(), accumulate one data each time, and call back myjp's modify function perform()
public void run() {
if(num.x<100) {
num.x+=1;
myjp.perform(String.valueOf(num.x));
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
num.x=0;
}
}
}
Because simple JPanel cannot be displayed, it can only be displayed on JFrame.
class myJF extends JFrame{
public static Atom atom=new Atom(0);
public static myJP myjp1=new myJP();
public static myJP myjp2=new myJP();
public static myJP myjp3=new myJP();
public myJF() {
super("concurrent execution of simulation program");
setSize(850,500);
setLocationRelativeTo(null); / / set the window position to the center of the screen
Container container=this.getContentPane();
GridLayout gridLayout=new GridLayout(3,1);
container.setLayout(gridLayout);
container.add(myjp1);
container.add(myjp2);
container.add(myjp3);
setVisible(true);
}
}
Now that the display framework and functions of the program have been set, the last problem we need to solve is how to simulate a resource competition of the process in the case of single CPU. The idea is as follows:

	int i=0;
	int j=0;
	int[] array=new int[1000];
	for(i=0;i<1000;i++) {
	    j=(int)(Math.random()*10%3);
	    array[i]=j;
	}

First, 1000 numbers are randomly generated (each time is different to meet the asynchrony of process execution), and the execution of each process is attached to the switch case structure in the dead loop while.
while (true) {
for(i=0;i<1000;i++) {
switch (array[i]) {
case 0:
if(myth1.myjp.button.getText() "pause"){
myth1.run();
}
else {
myth1.yield();
}
break;
case 1:
if(myth2.myjp.button.getText() "pause"){
myth2.run();
}
else {
myth2.yield();
}
break;
case 2:
if(myth3.myjp.button.getText() = = "pause"){
myth3.run();
}
else {
myth3.yield();
}
break;
default: break;
}
}
}
7, Experimental results and analysis
The experimental results meet the expectations. Finally, a complete source code is attached.

package myos;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class myThreadProcess {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		myJF threadFrame=new myJF();
		threadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Exit the program when the frame is closed
		myThread myth1=new myThread(threadFrame.atom,threadFrame.myjp1);
		myThread myth2=new myThread(threadFrame.atom,threadFrame.myjp2);
		myThread myth3=new myThread(threadFrame.atom,threadFrame.myjp3);
		int i=0;
		int j=0;
		int[] array=new int[1000];
		for(i=0;i<1000;i++) {
		    j=(int)(Math.random()*10%3);
		    array[i]=j;
		}
		/*First, 1000 numbers are randomly generated (each time is different to meet the asynchrony of process execution), and the execution of each process is attached to the switch case structure in the dead loop while.*/
		while (true) {
		    for(i=0;i<1000;i++) {
		        switch (array[i]) {
		            case 0:
		                if(myth1.myjp.button.getText()=="suspend") {
		                	myth1.run();
		                }
		                else {
		                	myth1.yield();
		                }
		                break;
		            case 1:
		                if(myth2.myjp.button.getText()=="suspend") {
		                	myth2.run();
		                }
		                else {
		                	myth2.yield();
		                }
		                break;
		            case 2:
		                if(myth3.myjp.button.getText()=="suspend") {
		                	myth3.run();
		                }
		                else {
		                	myth3.yield();
		                }
		                break;
		            default: break;
		        }
		    }
	}

}
	}
class myJF extends JFrame{
	public static Atom atom=new Atom(0);
public static myJP myjp1=new myJP();
public  static myJP myjp2=new myJP();
public static myJP myjp3=new myJP();
	public myJF() {
		super("Concurrent execution of simulation program");
	setSize(850,500);
	     setLocationRelativeTo(null);//Set the window position to the center of the screen
	     Container container=this.getContentPane();
	        GridLayout gridLayout=new GridLayout(3,1);
	        container.setLayout(gridLayout);
	        container.add(myjp1);
	        container.add(myjp2);
	        container.add(myjp3);
	        setVisible(true);
	}
}
/*Middle level container framework Department - contains a variety of controls such as label, button and textfile, and rewrites JPanel*/
class myJP extends JPanel {
	private JLabel label;//Display process status
	JButton button;//The operation button is used for function switching to control the process execution
	JTextField textFiled ;//Text box to display the process execution content
	public myJP() {//Used to initialize control location and content
		   setLayout(null);
	        setBorder(BorderFactory.createTitledBorder("Simulation process"));
	        textFiled=new JTextField("Specify content+Specify length(Read-only status )",30);
	        textFiled.setFont(new Font("Harmonic body",Font.BOLD|Font.ITALIC,16));
	    	//Sets the horizontal alignment of text
	        textFiled.setHorizontalAlignment(JTextField.CENTER);
	        textFiled.setBounds(30, 50, 200, 30);
	        button=new JButton("suspend");
	        button.setBounds(730, 50, 70, 35);
	        label=new JLabel("The simulation process is executing");
	        label.setBounds(10, 10, 200, 50);
		        add(label);
		        add(textFiled);
		        add(button);
		        setVisible(true);
		        /*Create a listening event for the button and listen to whether the name of the button changes to change the execution state of the process*/
	     button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {//Problem monitoring should be aimed at their own upper level
                if(button.getText()=="suspend") {
                    button.setText("implement");
                    label.setText("Simulation process paused");
                }
                else {
                	button.setText("suspend");
                	label.setText("The simulation process is executing");
                }
            }
        });
	}
	public void perform(String x)//Used to modify the empty display of text, which will be called in the inherited class overridden by the lower level
	{
		textFiled.setText(x);
	}
}
	class myThread extends Thread {
		 public Atom num;
		 public myJP myjp ;
		 //public myJP myjp =new myJP();
		    public myThread(Atom b,myJP myjp ) {
		        this.num=b;
		        this.myjp=myjp;
		    }
		    @Override
		    public void run() {
		        if(num.x<100) {
		        	num.x+=1;
		        	myjp.perform(String.valueOf(num.x));
		            try {
		                Thread.sleep(30);
		            } catch (InterruptedException e) {
		                e.printStackTrace();
		            }
		        }
		        else {
		        	num.x=0;
		        }
		    }
	}

/*Atomic part - used to display the amount of process change. The class declaration is:*/
class Atom {
	public int x;//An integer variable is used as the content and displayed in jtextfield textfile.
	public Atom(int x) {
	this.x=x;
	}
	public int getAtom() {
		return this.x;
}	
}

Keywords: Java Windows Ubuntu

Added by bookbuyer2000 on Mon, 18 Oct 2021 21:00:58 +0300