fifo rule modified

This commit is contained in:
Jai Sharma Sharma 2024-09-17 17:51:50 +05:30
parent 805d1bb949
commit a6772502e8
1 changed files with 32 additions and 23 deletions

View File

@ -12,8 +12,8 @@ using namespace std;
struct process_detail { struct process_detail {
//cpu_burst_times[0] is arrival time //cpu_burst_times[0] is arrival time
int pid; int pid;
vector<int> cpu_burst_times; vector<int> burst_times;
vector<int> io_burst_times; // vector<int> io_burst_times;
int in_cpu; int in_cpu;
int ptr = 0; int ptr = 0;
}; };
@ -21,7 +21,7 @@ struct process_detail {
struct clock{ struct clock{
int push_signal; //boolean int push_signal; //boolean
int timer; int timer;
}; };
//// operator overloading //// operator overloading
@ -43,29 +43,29 @@ struct process_detail* CPU = NULL;
int clock = 0; int clock = 0;
void fifo() { void fifo() {
//clock initialized to 0 //clock initialized to 0
struct clock time; struct clock time;
memset(&time, 0, sizeof(struct clock)); memset(&time, 0, sizeof(struct clock));
int process_count = processes.size(); int process_count = processes.size();
//ready queue initialized as process 1 will arrive at time 0 //ready queue initialized as process 1 will arrive at time 0
ready_queue_fifo.push(processes[0]); ready_queue_fifo.push(processes[0]);
processes[0].i++; processes[0].i++;
while(true){ while(true){
//managing arrival times //managing arrival times
for(int i = 0; i < process_count; ++i) { for(int i = 0; i < process_count; ++i) {
//if process not in cpu //if process not in cpu
if(proccesses[i].in_cpu != 1) { if(proccesses[i].in_cpu != 1) {
if(time.timer == processes[i].cpu_birst_times[0]) { if(time.timer == processes[i].burst_times[0]) {
ready_queue_fifo.push(processes[ptr]); ready_queue_fifo.push(processes[ptr]);
processes[i].i++; processes[i].i++;
} }
} }
} }
//THE FIFO RULE //THE FIFO RULE
if(CPU == NULL) { if(CPU == NULL) {
CPU = ready_queue_fifo.front(); CPU = ready_queue_fifo.front();
@ -76,16 +76,25 @@ void fifo() {
//check cpu_burst complete //check cpu_burst complete
for(int i = 0; i < process_count; ++i) { for(int i = 0; i < process_count; ++i) {
if(proccesses[i].in_cpu == 1) { if(proccesses[i].in_cpu == 1) {
if(timer.push_signal + CPU->cpu_birst_times[1] == time.timer){ if(timer.push_signal + CPU->burst_times[ptr] == time.timer){
waiting.push_back(CPU); waiting.push_back(CPU); // process added to waiting queue
CPU->in_cpu = 0;
CPU = ready_queue_fifo.front(); // process added to CPU
CPU->in_cpu = 1;
ready_queue_fifo.pop();
timer.push_signal = timer.push_signal + CPU->burst_times[ptr] ;
} }
} }
} }
// removing form wait
} }
time.timer++; time.timer++;
} }
cout << "fifo" << endl; cout << "fifo" << endl;
return; return;
} }
@ -117,16 +126,16 @@ int main(int argc, char **argv) {
memset(&pd,0,sizeof(struct process_detail)); memset(&pd,0,sizeof(struct process_detail));
while(iss>>word){ while(iss>>word){
if(i == 0){ // if(i == 0){
pd.cpu_burst_times.push_back(stoi(word)); // pd.cpu_burst_times.push_back(stoi(word));
} // }
else if(i % 2 == 0){ // else if(i % 2 == 0){
pd.io_burst_times.push_back(stoi(word)); // pd.io_burst_times.push_back(stoi(word));
} // }
else if(i % 2 == 1){ // else if(i % 2 == 1){
pd.cpu_burst_times.push_back(stoi(word)); // }
} pd.burst_times.push_back(stoi(word));
i++; // i++;
// cout << stoi(word) << endl; // cout << stoi(word) << endl;
} }
pd.pid = pid; pd.pid = pid;