Process scheduling: Time slice rotation scheduling algorithm

Source: Internet
Author: User


First, the purpose of the experiment

(1) Deepen understanding of the process

(2) Understanding the structure of the Process control block

(3) Understanding the concurrency of process operations

(4) Mastering time-slice rotation process scheduling algorithm

Second, the principle of experiment

(1) Building Process Control blocks

(2) Design two chain queues, representing the ready queue and the completion queue, respectively

(3) User input process identifier, process arrival time, process time required, application space storage process, PCB information.

(4) Each time slice end outputs process identifiers for each process, CPU run time

, process time required, time to reach, turnaround time, and status (run Complete or ready)



Third, the experimental steps, data recording and processing


1. Algorithmic flow


This procedure uses the definition of an abstract data type.


struct _pcbnode
{
	char name[20];       Process name
	int arrive_time;     Arrival time
	int need_time;      Service time
	int cputime;         The CPU has executed the time
	int accomplish_time;//completion time
	int turn_over_time;  Turnaround time
	bool tag;            is complete
};  
Class _PCB
{
	private:
	queue<struct _pcbnode> _pnode;
	Queue<struct _pcbnode> _cnode;
	int time;
}



The process of the main program and the hierarchy (invocation) relationship between each program module.

Implement the main functions defined in the summary design, write out the core algorithm for the main function (requires comments), and draw as much

Flow chart) This program is written in the ready queue to put the customer outside input process, so in the time slice rotation to determine the current time and arrival time, the arrival time is greater than the current time to the CPU scheduling

void Pop ()//mimic time rotation schedule
	{
		struct _pcbnode node;
		unsigned int n = _pnode.size ();
		unsigned int  i = 0;
		for (; i< n; ++i)//For loop to find the process when the arrival time in the queue is greater than the current time
		{
			node = _pnode.front (); _pnode.pop ();
			if (node.arrive_time <= time)
			{break
				;
			}
			_pnode.push (node);

		}

		Time = time+time_pice;//When the simulation process
		
		is running if (i = = N)//The process		{
			DISPLAY_PCB () is not found when the arrival times are greater than the current time;
			return;
		}

		Node.cputime +=time_pice; The process occupies the time when the CPU is running

		if (node.cputime >= node.need_time)//completes the run, enters the completion queue
		{
			node.accomplish_time = times;
			Node.tag = true;
			Node.turn_over_time  = node.accomplish_time-node.arrive_time;
			_cnode.push (node);  
			cout<<node.name<< "   run Over" <<endl;
		}
		else
		{
			_pnode.push (node);
		}
		DISPLAY_PCB (); 
	}

2. Analysis of running results

The time slice is set to 2, the ready queue is printed at time and the process state of the completion queue is run

Iv. Summary and experience

By doing this experiment, I simulated the time slice rotation scheduling algorithm in CPU process scheduling. The time slice wheel scheduling algorithm can realize the process sharing CPU. In the experiment, I found that the time slices can not be too large, otherwise it will cause most of the process in a time slice can be completed, can not realize the process of sharing CPU resources.

Appendix: Source Program

#include <iostream> #include <queue> #include <string> using namespace std;       #define Time_pice 2 struct _pcbnode {char name[20];     Process name int arrive_time;      Arrival time int need_time;         service time int cputime; The CPU has executed the time int accomplish_time;  completion time int turn_over_time;            Turnaround time BOOL tag;  

is complete};
	Class _PCB {public:void Push (struct _pcbnode& node) {_pnode.push (node);
	} void Init_time () {time = 0;
	} bool Empty () {return _pnode.empty ();
		} void Pop () {struct _pcbnode node;
		unsigned int n = _pnode.size ();
		unsigned int i = 0;
			for (; i< N; ++i) {node = _pnode.front (); _pnode.pop ();
			if (node.arrive_time <= time) {break;
		} _pnode.push (node);		
		} time = Time+time_pice;
			if (i = = N) {DISPLAY_PCB ();
		return;

		} node.cputime +=time_pice;
			if (node.cputime >= node.need_time) {node.accomplish_time = time;
			Node.tag = true; Node.turn_over_time = node.accomplish_tIme-node.arrive_time;  
			_cnode.push (node);
		cout<<node.name<< "Run Over" <<endl;
		} else {_pnode.push (node); 
	} DISPLAY_PCB ();
		} void Display_pcb () {cout<< "Time" <<TIME<<endl;
		int n = _pnode.size ();
			if (n! = 0) {cout<< "Ready ..." <<endl;
			cout<< "name Arrive_time need_time cputime Tag" <<endl;
				for (int i = 0;i<n; ++i) {struct _pcbnode node = _pnode.front (); _pnode.pop ();           if (node.arrive_time <= time) {cout<<node.name<< "" <<node.arrive_time<< " "<<node.need_time<<" "<<node.cputime <<" "<<node.tag <<e
				Ndl
			} _pnode.push (node);
		} cout<<endl;
		} n = _cnode.size ();
			if (n! = 0) {cout<< "Run over" <<endl; cout<< "name Arrive_time need_time accomplish_time turn_over_time Tag" <<endl;
			for (int i = 0;i<n; ++i) {struct _pcbnode node = _cnode.front (); _cnode.pop (); cout<<node.name<< "" <<node.arrive_time<< "" &LT;&LT;NODE.NEED_TIME&LT;&L t; "" <<node.accomplish_Time<< "<< node.turn_over_time<<" <<
				Node.tag <<endl;
			_cnode.push (node);
		} cout<<endl;		
	} cout<< "**************************************************" <<endl;
	} private:queue<struct _pcbnode> _pnode;
	Queue<struct _pcbnode> _cnode;
int time;

};
	void Main () {_PCB PCB;
	Pcb.init_time ();

	int pcb_numble;
	Char pcbname[20];
	int arrive_time;

	int need_time;
	cout<< "input pcb_numble:";

	cin>>pcb_numble;
	cout<< "Input pcbname arrive_time need_time example A 0 5\n";
		for (int i = 0; i<pcb_numble; ++i) {cin>>pcbname>>arrive_time>>need_time;
		struct _pcbnode node; strcpy (Node.name, PCBName);
		Node.arrive_time = Arrive_time;
		Node.need_time = Need_time;
		Node.accomplish_time = 0;
		Node.cputime = 0;
		Node.tag = 0; Pcb.
	Push (node); } PCB.
	DISPLAY_PCB (); while (!pcb.empty ()) {PCB.
	Pop (); 
 }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.