What's new

Help Cpu sched with idle time

cirex

Forum Veteran
Elite
Joined
Nov 10, 2018
Posts
2,602
Solutions
5
Reaction
2,645
Points
1,021
deleted
 
Last edited:
Magandang araw!

Maaari po bang pakilagay ang inyong code dito para mas maging malinaw kung saan kayo nangangailangan ng tulong? Kung mayroon din po kayong mga error messages o mga detalye na maaaring makatulong sa pag-unawa ng inyong problema, maaari rin pong isama ito.

Nag-aabang po ako ng inyong tugon upang makatulong sa inyong suliranin. Salamat po!
 
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>

using namespace std;

// define a struct named Process
struct Process {
int pid; // process id
int burst_time; // burst time of process
int arrival_time; // arrival time of process
int completion_time; // completion time of process
int turnaround_time; // turnaround time of process
int waiting_time; // waiting time of process
};

// comparison function to sort processes based on arrival time and process id.
bool compare(Process a, Process b) {
if (a.arrival_time != b.arrival_time) {
return a.arrival_time < b.arrival_time;
} else {
return a.pid < b.pid;
}
}

// main function
int main() {
int n, i;
t:
system("cls");
while (true) {

cout << "SHORTEST JOB FIRST - CPU SCHEDULING";
// take input number of processes from user (must be between 3 to 6)
cout << "\n\nEnter Number of Processes (min 3 and max 6): ";
cin >> n;

if (n >= 3 && n <= 6) {
break;
}

cout << "INVALID! You want to try again? (y/n): ";
char c;
cin >> c;

if (c != 'y') {
return 0;
}
}

while (true) {
vector<Process> processes(n);
// take input each process arrival time and burst time and assign them to struct.
for(i=0; i<n; i++) {
cout<<"Enter AT and BT of P" << i+1 <<": ";
cin>>processes.arrival_time>>processes.burst_time;
processes.pid = i+1;
}

// sort the processes based on arrival time using the comparison function
sort(processes.begin(), processes.end(), compare);

// calculate the completion time, turnaround time and waiting time for each process.
processes[0].completion_time = processes[0].burst_time;
processes[0].turnaround_time = processes[0].completion_time - processes[0].arrival_time;
processes[0].waiting_time = processes[0].turnaround_time - processes[0].burst_time;

for(i=1; i<n; i++) {
int min_burst_index = i;
for(int j=i; j<n; j++) {
if(processes[j].arrival_time <= processes[i-1].completion_time) {
if(processes[j].burst_time < processes[min_burst_index].burst_time) {
min_burst_index = j;
}
}
else {
break;
}
}
swap(processes, processes[min_burst_index]);
processes.completion_time = processes[i-1].completion_time + processes.burst_time;
processes.turnaround_time = processes.completion_time - processes.arrival_time;
processes.waiting_time = processes.turnaround_time - processes.burst_time;
}

// calculate the average turnaround time, average waiting time and CPU utilization.
float avg_turnaround_time=0, avg_waiting_time=0;
int total_burst_time=0;
for(i=0; i<n; i++) {
avg_turnaround_time += processes.turnaround_time;
avg_waiting_time += processes.waiting_time;
total_burst_time += processes.burst_time;
}
avg_turnaround_time /= n;
avg_waiting_time /= n;

// print the Gantt Chart, showing the start and end times of each process.
cout << "\n\n____________________________________________________________________________________________";
cout<<"\nGantt Chart:"<<endl <<endl;
for(i=0; i<n; i++) {
cout<<"| P" << processes.pid << " |";
}
cout<<"\n0";
for(i=0; i<n; i++) {
cout<<" "<<processes.completion_time;
}
cout<<endl;
cout << "____________________________________________________________________________________________";


// print the details of each process, including completion time, arrival time, turnaround time, burst time and waiting time.
cout<<"\nProcess ID\tCompletion Time\tArrival Time\tTurnaround Time\tBurst Time\tWaiting Time"<<endl;
for(i=1; i<=n; i++) {
int index = 0;
while(processes[index].pid != i) {
index++;
}
cout<<"P" << processes[index].pid << "\t\t" << processes[index].completion_time << "\t\t" << processes[index].arrival_time << "\t\t" << processes[index].turnaround_time << "\t\t" << processes[index].burst_time << "\t\t" << processes[index].waiting_time << endl;
}
cout << "____________________________________________________________________________________________";

// calculate and print the total burst time, average turnaround time, average waiting time and CPU utilization.
float cpu_utilization = ((float)total_burst_time / (float)(processes[n-1].completion_time - processes[0].arrival_time)) * 100;

//cout<<"\nTotal Burst Time: "<<total_burst_time<<endl;
cout<<"\n\nAverage Turnaround Time: "<<fixed<<setprecision(2)<<avg_turnaround_time<< "ms"<<endl;
cout<<"Average Waiting Time: "<<fixed<<setprecision(2)<<avg_waiting_time<< "ms"<<endl;
cout<<"CPU Utilization: "<<fixed<<setprecision(2)<<cpu_utilization<<"%"<<endl;
cout << "____________________________________________________________________________________________";
// Ask the user to enter another process or exit.
cout << "\nDo you want to enter another process? (y/n): ";
char c;
cin >> c;

if (c == 'y') {
goto t;
}
else {
cout << "\nThank You!" << endl;
exit(0);
}
}

return 0;
}
Nakita ko na po ang inyong code. Ano po ba ang mga problema o errors na nararanasan ninyo sa kasalukuyan? Nais ko po kayong matulungan sa pamamagitan ng pagbibigay ng mga detalyadong solusyon sa mga suliranin na inyong kinakaharap.
 
Nakita ko na po ang inyong code. Ano po ba ang mga problema o errors na nararanasan ninyo sa kasalukuyan? Nais ko po kayong matulungan sa pamamagitan ng pagbibigay ng mga detalyadong solusyon sa mga suliranin na inyong kinakaharap.
lalagyan po ng idle time sa gantt chart
 
lalagyan po ng idle time sa gantt chart
Upang mapakita ang idle time sa gantt chart, maaari ninyong subukan ang sumusunod na mga hakbang:

1. Idagdag ang sumusunod na variable sa inyong struct Process:

int idle_time;

Ang variable na ito ay gagamitin upang matukoy ang oras ng idle time.

2. Idagdag ang sumusunod na code sa loob ng loop na nagsasaayos ng proseso (process sorting loop):

Code:
   int idle = processes[i].arrival_time - processes[i-1].completion_time;
   if(idle > 0) {
       cout << "| idle |";
       processes[i].idle_time = idle;
       i++;
       continue;
   }

Ang code na ito ay magpapakita ng oras ng idle time kung ang kasalukuyang proseso ay hindi pa handa na magamit ang CPU dahil hindi pa tapos ang nakaraang proseso sa paggamit nito. Ang idle variable ay magre-representa ng oras ng idle time. Kung ang idle ay mas malaki kaysa sa 0, gagamitin ito upang mag-print ng idle time sa gantt chart.

3. Idagdag ang sumusunod na code sa dulo ng loop na nagsasaayos ng proseso (process sorting loop):

Code:
   if(processes[p-1].completion_time < total_burst_time) {
       cout << "| idle |";
   }
   cout << endl;

Ang code na ito ay magpapakita ng idle time sa dulo ng gantt chart kung hindi pa tapos ang CPU sa pagproseso ng lahat ng mga proseso.

Narito ang kabuuan ng inyong kodigo na may mga pagbabago upang maipakita ang idle time sa gantt chart:

Code:
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>

using namespace std;

// define a struct named Process
struct Process {
    int pid;                // process id
    int burst_time;         // burst time of process
    int arrival_time;       // arrival time of process
    int completion_time;    // completion time of process
    int turnaround_time;    // turnaround time of process
    int waiting_time;       // waiting time of process
    int idle_time;          // idle time of CPU
};

// comparison function to sort processes based on arrival time and process id.
bool compare(Process a, Process b) {
    if (a.arrival_time != b.arrival_time) {
        return a.arrival_time < b.arrival_time;
    } else {
        return a.pid < b.pid;
    }
}

// main function
int main() {
    int p, i;
    t:
    system("cls");
    while (true) {

        cout << "SHORTEST JOB FIRST - CPU SCHEDULING";
        // take input number of processes from user (must be between 3 to 6)
        cout << "\n\nEnter Number of Processes (min 3 and max 6): ";
        cin >> p;

        if (p >= 3 && p <= 6) {
            break;
        }

        cout << "INVALID! You want to try again? (y/n): ";
        char c;
        cin >> c;

        if (c != 'y') {
            return 0;
        }
    }

    while (true) {
        vector<Process> processes(p);
        // take input each process arrival time and burst time and assign them to struct.
        for(i=0; i<p; i++) {
            cout<<"Enter AT and BT of P" << i+1 <<": ";
            cin>>processes[i].arrival_time>>processes[i].burst_time;
            processes[i].pid = i+1;
        }

        // sort the processes based on arrival time using the comparison function
        sort(processes.begin(), processes.end(), compare);

        // calculate the completion time, turnaround time, waiting time and idle time for each process.
        processes[0].completion_time = processes[0].burst_time;
        processes[0].turnaround_time = processes[0].completion_time - processes[0].arrival_time;
        processes[0].waiting_time = processes[0].turnaround_time - processes[0].burst_time;
        processes[0].idle_time = processes[0].arrival_time;

        for(i=1; i<p; i++) {
            int idle = processes[i].arrival_time - processes[i-1].completion_time;
            if(idle > 0) {
                cout << "| idle |";
                processes[i].idle_time = idle;
                i++;
                continue;
            }
            int min_burst_index = i;
            for(int j=i; j<p; j++) {
                if(processes[j].arrival_time <= processes[i-1].completion_time) {
                    if(processes[j].burst_time < processes[min_burst_index].burst_time) {
                        min_burst_index = j;
                    }
                }
                else {
                    break;
                }
            }
            swap(processes[i], processes[min_burst_index]);
            processes[i].completion_time = processes[i-1].completion_time + processes[i].burst_time;
            processes[i].turnaround_time = processes[i].completion_time - processes[i].arrival_time;
            processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
            processes[i].idle_time = processes[i-1].completion_time;
        }

        // calculate the average turnaround time, average waiting time and CPU utilization.
        float avg_turnaround_time=0, avg_waiting_time=0;
        int total_burst_time=0;
        for(i=0; i<p; i++) {
            avg_turnaround_time += processes[i].turnaround_time;
            avg_waiting_time += processes[i].waiting_time;
            total_burst_time += processes[i].burst_time;
        }
        avg_turnaround_time /= p;
        avg_waiting_time /= p;

        // print the Gantt Chart, showing the start and end times of each process.
        cout << "\n\n____________________________________________________________________________________________";
        cout<<"\nGantt Chart:"<<endl <<endl;
        for(i=0; i<p; i++) {
            cout<<"| P" << processes[i].pid << "    ";
            if(processes[i].idle_time != 0) {
                cout << "| idle |";
            }
        }
        if(processes[p-1].completion_time < total_burst_time) {
            cout << "| idle |";
        }
        cout<<"\n0";
        for(i=0; i<p; i++) {
            cout<<"        "<<processes[i].completion_time;
            if(processes[i].idle_time != 0) {
                cout << "        ";
                cout << processes[i].idle_time;
            }
        }
        if(processes[p-1].completion_time < total_burst_time) {
            cout << "        ";
            cout << processes[p-1].completion_time;
        }
        cout<<endl;
        cout << "____________________________________________________________________________________________";


        // print the details of each process, including completion time, arrival time, turnaround time, burst time and waiting time.
        cout<<"\nProcess ID\tCompletion Time\tArrival Time\tTurnaround Time\tBurst Time\tWaiting Time"<<endl;
        for(i=1; i<=p; i++) {
            int index = 0;
            while(processes[index].pid != i) {
                index++;
            }
            cout<<"P" << processes[index].pid << "\t\t" << processes[index].completion_time << "\t\t" << processes[index].arrival_time << "\t\t" << processes[index].turnaround_time << "\t\t" << processes[index].burst_time << "\t\t" << processes[index].waiting_time << endl;
        }
        cout << "____________________________________________________________________________________________";

        // calculate and print the total burst time, average turnaround time, average waiting time and CPU utilization.
        float cpu_utilization = ((float)total_burst_time / (float)(processes[p-1].completion_time - processes[0].arrival_time)) * 100;

        cout<<"\n\nAverage Turnaround Time: "<<fixed<<setprecision(2)<<avg_turnaround_time<< "ms"<<endl;
        cout<<"Average Waiting Time: "<<fixed<<setprecision(2)<<avg_waiting_time<< "ms"<<endl;
        cout<<"CPU Utilization: "<<fixed<<setprecision(2)<<cpu_utilization<<"%"<<endl;
        cout << "____________________________________________________________________________________________";
        // Ask the user to enter another process or exit.
        cout << "\nDo you want to enter another process? (y/n): ";
        char c;
        cin >> c;

        if (c == 'y') {
            goto t;
        }
        else {
          cout << "\nThank You!" << endl;
          exit(0);
        }
    }

    return 0;
}

Pagkatapos ng mga pagbabago, makikita ninyo na ang oras ng idle time ay nakalagay na sa gantt chart.
 

Similar threads

Back
Top