시간 편 윤전 알고리즘

코드:
#include
#include
#include
#include
#include
#include
#include
using namespace std;

//      
//       
//RR  

 struct PCB {
	string name;
	int atime;  //    
	int rtime;  //      
	char status;  //     R   ,W   ,C   
	PCB* next;  //  

	PCB(){
		this->name = "";
		this->atime = -1;
		this->rtime = -1;
		this->status = NULL;
		this->next = NULL;
	}

	PCB(string name, int atime, int rtime, char status, PCB* next=NULL) {
		this->name = name;
		this->atime = atime;
		this->rtime = rtime;
		this->status = status;
		this->next = next;
	}

	//    
	PCB(const PCB& pcb) {
		this->name = pcb.name;
		this->atime = pcb.atime;
		this->rtime = pcb.rtime;
		this->status = pcb.status;
	}

	void show() {
		cout << name << " " << atime << " " << rtime << " " << status << endl;
	}
};

 bool cmp(PCB& a, PCB& b) {
	 return a.atime < b.atime;
 }

 //           
 void show_status(PCB* Q_head) {
	 if (!Q_head)return;
	 PCB* node = Q_head;
	 node->show();
	 while (node->next != Q_head) {
		 node = node->next;
		 node->show();
	 }
 }

 //      
 void initPool(vector& PCBPool, string fileName) {
	 ifstream inFile(fileName);
	 if (!inFile.is_open()) {
		 cout << "      !
"; return; } string name; int atime; // int rtime; // char status; // // while (inFile >> name >> atime >> rtime >> status) { PCB pcb(name, atime, rtime, status); PCBPool.push_back(pcb); } // sort(PCBPool.begin(), PCBPool.end(),cmp); cout << " !
"; inFile.close(); return; } // void headToTail(PCB*& Q_head, PCB*& Q_tail) { if (Q_head) { Q_tail = Q_head; Q_head = Q_tail->next = Q_head->next; } } // void delHead(PCB*& Q_head, PCB*& Q_tail) { if (Q_head) { PCB* node = Q_head; if (Q_head == Q_tail) { Q_head = Q_tail = NULL; } else { Q_head = Q_tail->next = Q_head->next; } delete node; } } // void scanAndAdd(PCB*& Q_head,PCB*& Q_tail, vector& PCBPool,int time) { while (PCBPool.size()&&PCBPool[0].atime == time) { PCB * node = new PCB(PCBPool[0]); // PCBPool.erase(PCBPool.begin()); // if (Q_head == NULL) { node->status = 'R'; Q_head = Q_tail = node; Q_tail->next = Q_head; } else{ node->status = 'W'; // Q_tail->next = node; // Q_tail = Q_tail->next; // Q_tail->next = Q_head; } } } // void RR(PCB*& Q_head, PCB*& Q_tail, vector& PCBPool) { // , , , , ; , int time = 0; while (!Q_head == NULL || !PCBPool.empty()) { cout << "===========================
"; cout << " :" << time << endl; // scanAndAdd(Q_head, Q_tail, PCBPool, time); bool notRun = true; while (notRun&&Q_head) { if (Q_head->status == 'R') { cout << " :" << Q_head->name << endl; Q_head->rtime--; notRun = false; // // if (Q_head->rtime == 0) { Q_head->status = 'C'; } // else { Q_head->status = 'W'; } show_status(Q_head); } else if (Q_head->status == 'W') { headToTail(Q_head, Q_tail); Q_head->status = 'R'; } else if (Q_head->status == 'C') { delHead(Q_head, Q_tail); if (Q_head) { Q_head->status = 'R'; } } } time++; } } // void createData(string fileName, int n) { ofstream outFile(fileName); if (!outFile.is_open()) { cout << " !" << endl; return; } srand((int)time(0)); string name; int atime; // int rtime; // char status='W'; // for (int i = 0; i < n; i++) { name = "P" + to_string(i); atime = rand() % n; rtime = rand() % 10 + 1; outFile << name << " " << atime << " " << rtime << " "< pool; initPool(pool,"test.txt"); PCB* Q_head = NULL; PCB* Q_tail = NULL; RR(Q_head, Q_tail, pool); system("pause"); }

좋은 웹페이지 즐겨찾기