Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <queue>
- #include<string>
- #include<limits>
- #include<map>
- #include<algorithm>
- #include<math.h>
- using namespace std;
- struct process{
- string name;
- double run_time;
- int arrival_time;
- int processed_time;
- int deque_flag;
- double start_time;
- double second_start_time;//added//if runtime greater than quantum//processed on two iterations
- double finish_time;
- double stopped_time;//indicate at which process stopped
- bool isFinished;//process done or not
- bool was_proccesed;
- bool was_queued;
- process()//default constructor
- {
- deque_flag = 0;
- processed_time = 0;
- isFinished = false;
- was_proccesed = false;
- was_queued = false;
- }
- };
- //To sort arrival times
- void BubbleSort(process * pro_arr, int len)
- {
- int i, j, flag = 1; // set flag to 1 to start first pass
- process temp; // holding variable
- for (i = 1; (i <= len) && flag; i++)
- {
- flag = 0;
- for (j = 0; j < (len - 1); j++)
- {
- if (pro_arr[j + 1].arrival_time < pro_arr[j].arrival_time)
- {
- temp = pro_arr[j]; // swap elements
- pro_arr[j] = pro_arr[j + 1];
- pro_arr[j + 1] = temp;
- flag = 1; // indicates that a swap occurred.
- }
- }
- }
- }
- int main(){
- string inputFileName;
- while (true){
- cout << "enter File name : ";
- cin >> inputFileName;
- ifstream in(inputFileName);
- if (!in){
- cout << "File not found" << endl;
- exit(1); //exit the programe
- }
- string outFile_name;
- outFile_name = inputFileName.substr(0, inputFileName.length() - 4) + "_output" + inputFileName.substr(inputFileName.length() - 4, 4); //set out file name
- string out_log_file_name;
- out_log_file_name = inputFileName.substr(0, inputFileName.length() - 4) + "_log" + inputFileName.substr(inputFileName.length() - 4, 4);//set log file name
- ofstream myfile;
- myfile.open(outFile_name);
- myfile << "process_id " << "run_time " << "Arrival_time " << "start_time " << "finish_time " << "TA_time " << "weighted_TA_time" << endl;
- ofstream my_log_file;
- my_log_file.open(out_log_file_name);
- cout << " the output is stored at text file " << outFile_name << "\n and the log file is stored at text file " << out_log_file_name << endl;
- //temp is to get rid of unnessacery input strings
- string temp;
- in >> temp;
- int quantum, switch_time;
- in >> quantum >> temp >> switch_time;
- in >> temp >> temp >> temp;
- int iterator = 0;
- process * pro_arr = new process[1000]; //save all coming process
- string temp_name; //tEMP Name of the process
- do {
- in >> temp_name;
- if (iterator != 0 && temp_name == pro_arr[iterator - 1].name) //this is to handle the input
- break;
- else
- {
- //save all process info to the array
- pro_arr[iterator].name = temp_name;
- in >> pro_arr[iterator].run_time >> pro_arr[iterator].arrival_time;
- iterator++;
- }
- } while (!in.eof());
- //sort arrivals time
- BubbleSort(pro_arr, iterator);
- queue<process> my_queue; //queue to handle round robin algorithim
- queue<process> my_printed_queue;
- double time = 0; //current cpu time
- bool finish = true; //all process are queued and finished
- string last_runned; //the last runned process name
- map<string, process> out_process; //to get output in the order of thier input where the string is the process name
- //Start round roubin
- while (true)
- {
- //loop throw the input array
- for (int i = 0; i < iterator; i++){
- if (!pro_arr[i].was_queued){
- finish = false;//we are not done with all process yet
- }
- if (pro_arr[i].arrival_time <= time && !pro_arr[i].was_proccesed&&!pro_arr[i].was_queued){
- pro_arr[i].was_queued = true;
- my_queue.push(pro_arr[i]);//put the process in the queue
- }
- }
- if (finish&&my_queue.empty())
- break; //all process are done
- if (!my_queue.empty()){
- //to make sure that the top of the queue is not the last runned process
- if (my_queue.front().was_proccesed&&my_queue.front().name == last_runned){
- process temp = my_queue.front();
- my_queue.pop();
- time += (switch_time);
- time++;
- my_queue.push(temp);
- }
- //outting the queue members//
- if (my_queue.size()>0)//not to be printed if there is no queue.
- {
- my_log_file << "\n \n Queue: ";
- }
- while (!(my_queue.empty()))
- {
- //cout<<my_queue.front().name;
- my_log_file << my_queue.front().name ;
- process hopa = my_queue.front();
- my_printed_queue.push(hopa);//saving elements of my my_queue
- my_queue.pop();
- if (!my_queue.empty())
- my_log_file << ",";
- }
- //int deque_flag=0;
- //cout << "\n" << endl;
- while (!my_printed_queue.empty())//restore my queue
- {
- process hopa = my_printed_queue.front();
- my_queue.push(hopa);
- my_printed_queue.pop();
- }
- //START SIMULATING
- if (!my_queue.front().was_proccesed){
- my_queue.front().start_time = time;
- my_queue.front().was_proccesed = true;
- }
- else{ //start switching
- time += (switch_time);
- time++;
- }
- last_runned = my_queue.front().name;
- if (my_queue.front().run_time - my_queue.front().processed_time <= quantum){
- time += (my_queue.front().run_time - my_queue.front().processed_time);
- my_queue.front().finish_time = time;
- my_queue.front().second_start_time = time - (my_queue.front().run_time - my_queue.front().processed_time);//current time - remained time from first process iterations..reamining-->(93-50)
- //added//
- my_queue.front().isFinished = true;//to flag that the process is done
- out_process[my_queue.front().name] = my_queue.front();
- my_queue.front().deque_flag = 1;//if we deque here we cant print element that is finished being processed from first iteration//so we deque after printing
- //else my_queue.pop();
- }
- else{ //move time to the next quantam
- time += quantum;
- my_queue.front().processed_time += (quantum);
- my_queue.front().stopped_time = time;
- }
- time++; //move time one step;
- }
- else{
- double temp_time = 10000000;
- for (int i = 0; i < iterator; i++){
- if (!pro_arr[i].was_proccesed&&!pro_arr[i].was_queued){
- temp_time = pro_arr[i].arrival_time;
- break;
- }
- }
- time += (quantum + 1);
- if (time>temp_time)
- time = temp_time;
- }
- ////outting log file//
- if (!my_queue.size() == 0)//if there is no eleemtns dont enter printing region// to avoid runtime error//
- {
- if (my_queue.front().isFinished&&my_queue.front().run_time>quantum)//at the second iteration//...//means process takes two iterations to be done,then at the secind iteration we use secon_start_time
- {
- my_log_file << "\n\n process switching : started at " << my_queue.front().second_start_time - switch_time-1 << ", \tfinished at " << my_queue.front().second_start_time - 1;//switching
- my_log_file << "\n \n Executing process " << my_queue.front().name << " :";
- my_log_file << " started at " << my_queue.front().second_start_time << ",\t" << "finished at " << my_queue.front().finish_time;
- }
- if (my_queue.front().isFinished&&my_queue.front().run_time<quantum)//process toke one iteration only
- {
- my_log_file << "\n \n Executing process " << my_queue.front().name << " :";
- my_log_file << " started at " << my_queue.front().start_time << ", \t" << "finished at " << my_queue.front().finish_time;
- }
- if (!my_queue.front().isFinished)//process not finished yet
- {
- my_log_file << "\n \n Executing process " << my_queue.front().name << " :";
- my_log_file << " started at " << my_queue.front().start_time << ", \tstopped at " << my_queue.front().stopped_time << ",\t" << my_queue.front().run_time - my_queue.front().processed_time << " remaining";
- my_log_file << "\n\n process switching : started at " << my_queue.front().stopped_time + 1 << ", \tfinished at " << my_queue.front().stopped_time + switch_time+1;//switching
- }
- if (my_queue.front().deque_flag == 1){ my_queue.pop(); } //
- finish = true;
- }
- }//end of while
- double total_waiting_time = 0;
- double total_weighted_turn_around = 0;
- double * wegihted_arr = new double[iterator];
- //start outing to the out file
- for (int i = 0; i < iterator; i++){
- process temp = out_process[pro_arr[i].name];
- double weighted_turn_around_time = (temp.finish_time - temp.arrival_time) / temp.run_time;
- wegihted_arr[i] = weighted_turn_around_time;
- total_weighted_turn_around += weighted_turn_around_time;
- total_waiting_time += (temp.start_time - temp.arrival_time);
- myfile << temp.name << " " << temp.run_time << " " << temp.arrival_time << " " << temp.start_time << " " << temp.finish_time << " " << temp.finish_time - temp.arrival_time << " " << weighted_turn_around_time << endl;
- }
- double avg_weighted_turn_around_time = total_weighted_turn_around / (iterator);
- myfile << "Average Weighted TA time: " << avg_weighted_turn_around_time << endl;
- //calculate standard div
- double total = 0;
- for (int i = 0; i < iterator; i++){
- total += pow(avg_weighted_turn_around_time - wegihted_arr[i], 2);
- }
- myfile << "Standard deviation Weighted TA time: " << sqrt(total / iterator) << endl;
- myfile << "Average waiting time :" << total_waiting_time / iterator << endl;
- in.close();
- myfile.close();
- my_log_file.close();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement