Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Process {
- int pid;
- int arrival_time;
- int burst_time;
- int start_time;
- int finish_time;
- int turnaround_time;
- int waiting_time;
- };
- void fcfs(Process [], int);
- void sjf(Process [], int);
- void printTable(Process [], int);
- int main() {
- int n;
- cout << "Enter the number of processes: ";
- cin >> n;
- Process proc[n];
- for (int i = 0; i < n; i++) {
- cout << "Enter arrival time and burst time for process " << i + 1 << ": ";
- cin >> proc[i].arrival_time >> proc[i].burst_time;
- proc[i].pid = i + 1;
- }
- cout << "\nFCFS Scheduling:\n";
- fcfs(proc, n);
- printTable(proc, n);
- cout << "\nSJF Scheduling:\n";
- sjf(proc, n);
- printTable(proc, n);
- return 0;
- }
- void fcfs(Process proc[], int n) {
- int time = 0;
- for (int i = 0; i < n; i++) {
- if (time < proc[i].arrival_time) {
- time = proc[i].arrival_time;
- }
- proc[i].start_time = time;
- time += proc[i].burst_time;
- proc[i].finish_time = time;
- proc[i].turnaround_time = proc[i].finish_time - proc[i].arrival_time;
- proc[i].waiting_time = proc[i].turnaround_time - proc[i].burst_time;
- }
- }
- void sjf(Process proc[], int n) {
- int time = 0, completed = 0;
- while (completed != n) {
- int shortest = INT_MAX, index = -1;
- for (int i = 0; i < n; i++) {
- if (proc[i].arrival_time <= time && proc[i].burst_time >= 0 && proc[i].burst_time < shortest) {
- shortest = proc[i].burst_time;
- index = i;
- }
- }
- if (index != -1) {
- proc[index].start_time = time;
- time += proc[index].burst_time;
- proc[index].finish_time = time;
- proc[index].turnaround_time = proc[index].finish_time - proc[index].arrival_time;
- proc[index].waiting_time = proc[index].turnaround_time - proc[index].burst_time;
- proc[index].burst_time = 0;
- completed++;
- } else {
- time++;
- }
- }
- }
- void printTable(Process proc[], int n) {
- cout << "Process ID\tArrival Time\tBurst Time\tStart Time\tFinish Time\tTurnaround Time\tWaiting Time\n";
- for (int i = 0; i < n; i++) {
- cout << proc[i].pid << "\t\t" << proc[i].arrival_time << "\t\t" << proc[i].burst_time << "\t\t" << proc[i].start_time << "\t\t" << proc[i].finish_time << "\t\t" << proc[i].turnaround_time << "\t\t" << proc[i].waiting_time << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement