#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
struct process
{
int name;
int id;
int processing_time;
int priority;
int arrival_time;
int start_time;
int finish_time;
bool waiting;
bool serving;
bool finished;
};
struct node
{
process data;
node* next;
};
struct head
{
node* rear;
node* front;
};
///////////////////////////////
void pause();
void enqueue(process newprocess,head*& h);
process dequeue(head*& h);
node* convert_from_Array_to_Linked_List(process* table,int size);
process* convert_from_Linked_List_to_Array(node *head,int size);
void prepend(process a,node *&head);
bool check_process(int data,node*head);
bool check_quantum(int i,node* head,int q,int find,int end);
process search(int data1,node *&head);
void printqueue(process table,head*& s);
void Round_Rubin(process*&table,int process_No,int quantum);
/////////////////////////////
void push(int*&,int&,int);
bool pop(int*&,int&,int&);
int convert_char_to_int(char);
int power_function(int);
int get_equi_number(int[10],int&);
void sort_ascendingly_according_to_processing_time(process*&,int);
process* read_input_from_textfile(char&,int&,int&,int&,int&);
void FCFS(process*&,int,int,ofstream&);
void sort_ascendingly_according_to_arrival_time(process*&,int);
void check_waiting(process*&,int,int); //used for FCFS
void check_current_process_finished(process&); //used for FCFS
void writing_output_textfile(process*&,int,ofstream&,int); //used for FCFS and SJF and HPF
void SJF(process*&,int,int,ofstream&);
int get_shortest_job(process*&,int,int);
void SRTN(process*&,int,int,ofstream&);
bool check_if_process_arrived(process*&,int,int); //used for the PREM algorithms
void HPF(process*&,int,int,ofstream&);
int get_highest_priority(process*&,int,int);
void HPF_PREM(process*&,int,int,ofstream&);
void writing_upper_part_of_output_textfile(ofstream&,char,int,int,int);
int main ()
{
ofstream output("output.txt",ios::app);
ofstream myfile;
myfile.open("output.txt",ios::app);
char prem; //stores the status of prem 't' or 'f'
int algorithm; //stores the number of algorithm
int quantum = 4; //stores the value of the quantum
int switching_time; //stores the value of the switching time
int size; //contains the number of processes in the text file
process* table = read_input_from_textfile(prem,algorithm,quantum,switching_time,size);
writing_upper_part_of_output_textfile(output,prem,algorithm,quantum,switching_time);
for(int i=0; i<size; i++)
{
table[i].waiting = false;
table[i].serving = false;
table[i].finished = false;
}
if(algorithm == 1)
{
FCFS(table,switching_time,size,output);
}
if(algorithm == 2)
{
if(prem == 'f')
{
SJF(table,switching_time,size,output);
}
else
{
}
}
if(algorithm == 3)
{
SRTN(table,switching_time,size,output);
}
if(algorithm == 4)
{
if(prem == 'f')
{
HPF(table,switching_time,size,output);
}
else
{
HPF_PREM(table,switching_time,size,output);
}
}
if(algorithm==5)
{
Round_Rubin(table,size,quantum);
}
myfile.close();
}
void push(int*& stack, int& top, int temp)
{
top++;
stack[top] = temp;
}
bool pop(int*& stack, int& top, int& box)
{
if(top == -1)
return false;
else
{
box = stack[top];
top--;
return true;
}
}
int convert_char_to_int(char ch)
{
int temp = int(ch);
if(temp == 48)
return 0;
if(temp == 49)
return 1;
if(temp == 50)
return 2;
if(temp == 51)
return 3;
if(temp == 52)
return 4;
if(temp == 53)
return 5;
if(temp == 54)
return 6;
if(temp == 55)
return 7;
if(temp == 56)
return 8;
if(temp == 57)
return 9;
}
int get_equi_number(int stack[10], int& top)
{
int equi_number = -1;
int temp = 0;
int count = 0;
int prev = 0;
while(pop(stack,top,temp))
{
equi_number = temp * power_function(count) + prev;
count++;
prev = equi_number;
}
return equi_number;
}
int power_function(int count)
{
if(count == 0)
return 1;
else
{
int temp = 1;
for(int i=0; i<count; i++)
{
temp = temp * 10;
}
return temp;
}
}
void FCFS(process*& table, int switching_time, int size, ofstream& output)
{
output<<"#Timeline"<<endl;
sort_ascendingly_according_to_arrival_time(table,size);
int* storing_processing_time_of_processes = new int[size];
for(int i=0; i<size; i++)
{
storing_processing_time_of_processes[i] = table[i].processing_time;
}
int time = table[0].arrival_time;
int shifted_time = 0;
int current_time = time + shifted_time;
int counter = 0;
bool start_time_evaluation = true;
while(counter < size)
{
if(start_time_evaluation)
{
table[counter].start_time = time;
table[counter].serving = true;
table[counter].waiting = false;
current_time = table[counter].start_time + shifted_time;
check_waiting(table,current_time,size);
writing_output_textfile(table,current_time,output,size);
start_time_evaluation = false;
}
table[counter].serving = true;
table[counter].waiting = false;
shifted_time++;
table[counter].processing_time--;
table[counter].finish_time = table[counter].start_time + shifted_time;
current_time = table[counter].start_time + shifted_time;
check_waiting(table,current_time,size);
writing_output_textfile(table,current_time,output,size);
check_current_process_finished(table[counter]);
if(table[counter].finished == true)
{
start_time_evaluation = true;
table[counter].serving = false;
counter++;
time = shifted_time + switching_time + time;
shifted_time = 0;
}
}
for(int i=0; i<size; i++)
{
table[i].processing_time = storing_processing_time_of_processes[i];
}
}
void sort_ascendingly_according_to_arrival_time(process*& table,int size)
{
process temp;
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
if(j+1 == size)
{
break;
}
if(table[j].arrival_time > table[j+1].arrival_time)
{
temp = table[j+1];
table[j+1] = table[j];
table[j] = temp;
}
}
}
}
void sort_ascendingly_according_to_processing_time(process*& table, int size)
{
process temp;
for(int i=1; i<size; i++)
{
for(int j=0; j<size; j++)
{
if(j+1 == size)
{
break;
}
if(table[j].processing_time >= table[j+1].processing_time)
{
temp = table[j+1];
table[j+1] = table[j];
table[j] = temp;
}
}
}
}
process* read_input_from_textfile(char& prem, int& algorithm, int& quantum, int& switching_time, int& size)
{
process list[20]; //array of processes
char detect;
ifstream input("input.txt");
for(int i=0; i<13; i++)
{
input>>detect;
}
prem = detect;
if(prem == 'f')
input>>detect;
for(int i=0; i<15; i++)
{
input>>detect;
}
////////////////////////////////////////////////////////////////////////////////////////
algorithm = convert_char_to_int(detect);
////////////////////////////////////////////////////////////////////////////////////////
int* stack = new int[10]; //this is the stack
int top = -1; //used for the stack
int loop_count = 3; //just to know how the limit of the coming loop
if(algorithm == 5) //then the user wants ROUND ROBIN algorithm bec 53 is the asci code of 5
{
for(int i=0; i<9; i++)
{
input>>detect;
}
int temp;
while(true)
{
input>>detect;
if(detect == '#')
{
break;
}
temp = convert_char_to_int(detect);
push(stack,top,temp);
}
loop_count = 2;
quantum = get_equi_number(stack,top);
}
//Now the stack is empty in order to use it again if i needed it
else
{
input>>detect;
string templine; //this block of three lines is for skipping the line containing the quantum if the process is not RR
g