Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <math.h>
- #include <stdio.h>
- #include <process.h>
- #include <stdlib.h>
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <time.h>
- #include <queue>
- #include <vector>
- #include <fstream>
- using namespace std;
- struct ThreadInfo
- {
- ThreadInfo()
- {
- isStarted = false;
- runPermission = false;
- isFinished = false;
- priority = 0;
- age = 0;
- }
- int id;
- //when thread should be started
- int startTime;
- //How long thread should work
- int duration;
- //Scheduler set this boolean to simulate giving time shared to each thread
- int priority;
- //priority of current thread
- int age;
- //age of the thread
- bool runPermission;
- //Indicate if the thread is started
- bool isStarted;
- //Indicate if the thread is finished
- bool isFinished;
- };
- struct QueueInfo
- {
- int timeSlice;
- //Time slice of the queue
- int maxAge;
- //maximum age of the thread before moving to next queue
- };
- static QueueInfo* qinfo;
- ofstream out;
- unsigned __stdcall threadWord(void* a)
- {
- ThreadInfo* info = (ThreadInfo*)a;
- //Print out start of this thread
- stringstream ss1(stringstream::in | stringstream::out);
- ss1 << "Clock: " << clock() << ", Thread " << info->id << ": Started." << endl;
- cout << ss1.str();
- out << ss1.str();
- long isStarted = clock();
- long executionDuration = 0;
- //Continue until total execution reaches the duration of the thread
- while(executionDuration < info->duration)
- {
- //Wait until get time slice from the scheduler
- while(!info->runPermission){}
- //Calculate current cycle execution time
- long executionStart = clock();
- long cycleDuration = 0;
- //Continue the calculation until the time shared expires or thread finishes
- while(info->runPermission && executionDuration + cycleDuration < info->duration){
- cycleDuration = clock() - executionStart;
- }
- executionDuration += cycleDuration;
- info->age=clock()-info->startTime;
- //Print out the execution time in each time slice
- stringstream ss(stringstream::in | stringstream::out);
- ss << "Clock: " << clock() << ", Thread " << info->id << ": Execution: " << executionDuration<<" Age: "<<info->age<<" Priority: "<<info->priority<<endl;
- cout << ss.str();
- out << ss.str();
- }
- long end = clock();
- //Print out end of this thread
- stringstream ss2(stringstream::in | stringstream::out);
- ss2 << "Clock: " << clock() << ", Thread " << info->id << ": Finished. Duration=" << end - isStarted << endl;
- cout << ss2.str();
- out << ss2.str();
- //Set the thread as finished
- info->isFinished = true;
- return 0;
- }
- void priority(ThreadInfo* obj)
- {
- int maxAge=qinfo[obj->priority].maxAge; //assigns maxAge the value of that queue's max age using the thread's priority number
- if((obj->age)>maxAge) //if the thread's age is greater than the max age, increase the priority by 1
- {
- obj->priority++;
- }
- }
- //Round-robin Scheduler
- unsigned __stdcall scheduler_RR(void* a)
- {
- //Input thread infos
- queue<ThreadInfo*>* waitingThreads = (queue<ThreadInfo*>*)a;
- while(!waitingThreads->empty())
- {
- //Get first element of the queue
- ThreadInfo *curThreadInfo = waitingThreads->front();
- waitingThreads->pop();
- //Check if start time has been reached
- if(curThreadInfo->startTime > clock())
- {
- //push back this thread and go for next thread
- waitingThreads->push(curThreadInfo);
- continue;
- }
- //Check if the thread has already been started.
- //int start_time=clock()-curThreadInfo->startTime;
- priority(curThreadInfo);
- if(!curThreadInfo->isStarted)
- {
- curThreadInfo->isStarted = true;
- //start thread
- _beginthreadex(NULL, 0, threadWord, curThreadInfo, 0, 0);
- }
- //Give time share to the current thread
- curThreadInfo->runPermission = true;
- //Sleep(qinfo[curThreadInfo->priority].timeSlice);
- if(curThreadInfo->priority==2) //occurs when time slice is -1 (lets the thread finish)
- {
- while(!curThreadInfo->isFinished){} //loop runs until thread has finished
- }
- else
- Sleep(qinfo[curThreadInfo->priority].timeSlice); //time slice is the queue array value using the thread priority 0,1 or 2
- //Take the time share back from the current thread
- curThreadInfo->runPermission = false;
- //If the thread is finished
- if(!curThreadInfo->isFinished)
- waitingThreads->push(curThreadInfo);
- }
- return 0;
- }
- //Read input thread infos and return them as a queue of pointers
- queue<ThreadInfo*>* readThreadInfos(string fileName)
- {
- ifstream in(fileName);
- queue<ThreadInfo*>* threadInfos = new queue<ThreadInfo*>();
- int idTemp = 0;
- while(in.good())
- {
- //Create each threadInfo instance
- ThreadInfo* info = new ThreadInfo();
- info->id = idTemp;
- in >> info->startTime;
- in >> info->duration;
- info->startTime *= 1000;
- info->duration *= 1000;
- threadInfos->push(info);
- idTemp++;
- }
- return threadInfos;
- }
- void main()
- {
- //output file
- out = ofstream("c:\\output.txt");
- //reading thread infos (start time and duration of each thread)
- queue<ThreadInfo*>* threadInfos = readThreadInfos("c:\\RR-input.txt");
- ifstream myFile("config.txt");
- int queueNum=0;
- myFile >> queueNum;
- qinfo = new QueueInfo[queueNum];
- for(int i=0; !myFile.eof(); i++)
- {
- if(i==0){}
- else
- {
- myFile >> qinfo[i-1].timeSlice;
- qinfo[i-1].timeSlice=qinfo[i-1].timeSlice*1000;
- myFile >> qinfo[i-1].maxAge;
- qinfo[i-1].maxAge=qinfo[i-1].maxAge*1000;
- }
- }
- //Start scheduler thread
- _beginthreadex(NULL, 0, scheduler_RR, threadInfos, 0, 0);
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement