Analog implementation of operating system scheduling algorithm __ algorithm

Source: Internet
Author: User
Tags int size prev

Before learning the operating system of the operating system some of the scheduling algorithm is more interested, so I simulate the implementation of the operating system algorithm scheduling, I mainly simulate the implementation of a short job first priority and service scheduling algorithm. The code is a bit long, plus the test code estimated to be about 300 lines, it seems inconvenient to put here (forget it, or put it below, so that people do not see the trouble). Let me put the results of the implementation of the screenshot below, and then attach the code, in fact, in my GitHub above also have this code, address: https://github.com/admin-zou/DS/tree/master/scheduling





Header file://scheduling.h

#ifndef _scheduling_ #define _scheduling_ #include <iostream> #include <stdlib.h> using namespace std; Enum TAG{UNSHD,SHD};			Whether the mark is adjusted to pass struct PCB {int pcbid;			Process number size_t Arrtime;			Arrival time size_t Sertime;			Service time size_t Begtime;			Start time size_t Endtime;		Completion time size_t Turntime;		Revolving time float Weighttime;			With power turnaround time PCB * NEXT;				Pointer to the next node tag tag; Whether the tag is tuned through the PCB (int n=0,size_t a=0,size_t s=0):p cbid (n), Arrtime (a), Sertime (s), Begtime (0), Endtime (0), Turntime (0), we

Ighttime (0), Next (NULL), tag (unshd) {}};
	Class Scheduling {public:scheduling (): _curtime (0), _tasknum (0) {_head = new PCB ();
			/////first comes first service algorithm void Fifs () {if (empty ()) {cout<< "no task";
		Exit (-1);  } _clear (); Clean up, can be repeatedly computed _sort_t ();
		Sorted by arrival time pcb* cur = _head->next; 
			while (NULL!= cur) {if (_curtime < cur->arrtime) {_curtime = cur->arrtime; 
			} cur->begtime = _curtime; Cur->endtime = _curtime + cur-> sertime;	 Finish time equals start time plus service time Cur->turntime = cur->endtime-cur->arrtime;  Turnaround time = Finish Time-arrival time Cur->weighttime = (float) cur->turntime/(float) cur->sertime; Weighted turnaround time = turnaround time/service time Cur->tag = SHD;
			Marked as already serviced _curtime + + cur->sertime;
		Cur = cur->next;
			}}/////Short job void Brief () {if (empty ()) {cout << "no task";
		Exit (-1);  } _clear (); Clean up, can be repeatedly computed _sort_t ();
		Sorted by arrival time pcb* cur = _head->next;
			while (NULL!= cur) {if (_curtime < cur->arrtime) {_curtime = cur->arrtime;
			} cur->begtime = _curtime;		Cur->endtime = _curtime + cur->sertime;	 Finish time equals start time plus service time Cur->turntime = cur->endtime-cur->arrtime; Turnaround time = Finish Time-arrival time Cur->weighttime = (float) cur->turntime/(float) cur->sertime; Weighted turnaround time = turnaround time/service time Cur->tag = SHD;
			Marked as already serviced _curtime + + cur->sertime;
		
			Cur = cur->next;  The process of scheduling the process has already arrived at the time of short job prioritization _sort_l (cur,_curtime); Start a short job sort from this process

		} void Init_task () {int tasknum=0;
		size_t id=0;
		size_t atime=0;
		size_t stime=0;
		cout<< "Please enter the number of tasks:";	
		cin>>tasknum;
			for (int i = 0; i<tasknum;i++) {cout<<] Please enter the number of the task, arrival time, elapsed time: ";
			cin>>id>>atime>>stime;
		Push (Id,atime,stime);
		} void Push () {size_t id=0;
		size_t atime=0;
		size_t stime=0;
		cout<< "Please enter the number of the task, arrival time, Running time:";
		cin>>id>>atime>>stime;
	Push (Id,atime,stime);
		void Print () {if (empty ()) return;
		pcb* cur = _head->next;
		printf ("Process number arrival Time service time start time completion time turnaround time/power turnaround time \ n"); while (NULL!= cur) {printf ("%4d%6d%8d%9d%7d%8d\t%0.2f\n", Cur->pcbid, Cur->arrtime, Cur->sertime, cur
			->begtime, Cur->endtime, Cur->turntime, cur->weighttime);
		Cur = cur->next;
	} Protected:bool empty () {return _tasknum = 0;
		BOOL Push (int n,size_t a,size_t s)//insert to the tail of the list {PCB * NewTask = new PCB (n,a,s);
		PCB * cur = _head; WhIle (NULL!= cur->next) cur =cur->next;
		cur->next=newtask;
		_tasknum++;
	return true;
		} void _clear () {if (empty ()) return;
		pcb* cur = _head->next;
			while (NULL!= cur) {cur->begtime = 0;
			Cur->endtime = 0;
			Cur->turntime = 0;
			Cur->weighttime = 0;
			Cur->tag = unshd;
		cur = cur->next;
	} _curtime = 0;
		///According to arrival time void _sort_t () {if (empty () | | | _tasknum = 1) return;
		pcb* prev = _head->next;
		pcb* cur = prev->next; for (int i = 0; i< _tasknum-1. i++) {for (int j = 0; j<_tasknum-i-1; j + +) {if prev->arrtime > cur
				->arrtime) {_swap (prev, cur);
				} prev = cur;
			Cur = cur->next;
			} prev=_head->next;
		Cur = prev->next; sort void _sort_l (pcb*& head,size_t curtime) {if (NULL = = Head | |) according to job length
		NULL = = Head->next) return;
		pcb* prev = head;
		pcb* cur = prev->next;  int size = 0;
		Number of calculation processes pcb* tmp = head; While(TMP)
			{++size;
		TMP = tmp->next; for (int i = 0; cur->arrtime < curtime && i < size-1; i++) {if (Prev->arrtime > Curtim
			e) {//The job has not yet arrived on the order of return; for (int j = 0; J < size-i-1; j) {if (cur && cur->arrtime <= curtime) {int P
					Time = prev->sertime;
					int CTime = cur->sertime;
					if (Ptime > CTime) {_swap (prev, cur);
				} prev = cur;
			Cur = cur->next;
			} prev = head;
		Cur = prev->next;
		} void _swap (PCB * PREV,PCB * cur) {Swap (prev->arrtime,cur->arrtime);
		Swap (prev->pcbid, cur->pcbid);
	Swap (Prev->sertime, cur->sertime);
	} PRIVATE:PCB * _head;	
	size_t _curtime;	size_t _tasknum;

Number of jobs}; #endif

Test files:

#define	_crt_nowanrings
#include "scheduling.h"

int main ()
{
	int select=1;
	scheduling mytask;
	while (select)
	{
		cout<< "****************************" <<endl;
		cout<< "*   1. Initialize               *" <<endl;
		cout<< "*   2. Insert a new process       *" <<endl;
		cout<< "*   3. First come first service scheduling algorithm   *" <<endl;
		cout<< "*   4. Short Job scheduling algorithm       * <<endl;
		cout<< "*   5. Display dispatch status         * <<endl;
		cout<< "*   0. Exit                 *" <<endl;
		cout<< "****************************" <<endl;
		int item=0;
		cout<< "Please enter:";
		cin>>select;
		Switch (Select)
		{case
		1:
			mytask. Init_task ();		
			break;
		Case 2:
			mytask. Push ();
			break;
		Case 3:
			mytask. Fifs ();
			break;
        Case 4:
			mytask. Short ();
			break;
		Case 5:
			mytask. Print ();
			cout << Endl;
			break;
		Default: Break
			;
		}
	}
	return 0;
}

Test conditions/
*
5 1 0 4 2 2 4 3 3 3 4 5 6 5 6-
3
* *

Summarize:

1. The above procedure is based on my understanding of the operating system scheduling algorithm, mainly simulates the first to first service and short job priority scheduling algorithms, which involves the C + +, data structure and operating systems and algorithms, and so on, to achieve it is really big benefit.

2. In the small project above, I calculated the efficiency of the scheduling algorithm by calculating the turnaround time of various scheduling algorithms, the weighted turnaround time and so on, and can evaluate the performance of the scheduling algorithm in a certain range, and some scenarios are suitable for using which scheduling algorithm.

3. I am through a single linked list to organize the relevant job scheduling data needs, so there are good, there are defects, this and linked list of advantages and disadvantages correspond. But it's pretty good when the data is small.

The main difficulty is that it is difficult to analyze all the situations that may arise and the behavior in all situations. There is a need for the operating system scheduling algorithm has a certain understanding, otherwise, it is easy to appear in the actual mismatch of results, such as short job priority scheduling, how to understand this scheduling method, I give an example: the beginning of only one job in the scheduling, but it takes a long time, in his execution process to other operations, But the job time is very short, the first scheduling this short job may be more excellent, then we need to deal with it, this involves whether to allow the preemption of resources (of course, in my implementation is not to preempt resources). Finally, the development of the time must be more debugging, reduce bugs, increase the robustness of the program.



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.