Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.5 kB
2
Indexable
Never
#include <stdio.h>

int main() {
    int n, i, j, time = 0, smallest, process = -1, sum_waiting_time = 0, sum_turnaround_time = 0;
    float avg_waiting_time, avg_turnaround_time;
    printf("Enter the number of processes: ");
    scanf("%d", &n);
    int pid[n], arrival_time[n], burst_time[n], remaining_time[n], waiting_time[n], turnaround_time[n];
    for(i = 0; i < n; i++) {
        printf("Enter the process ID, arrival time and burst time for process %d: ", i+1);
        scanf("%d%d%d", &pid[i], &arrival_time[i], &burst_time[i]);
        remaining_time[i] = burst_time[i];
    }
    while(process != n-1) {
        smallest = n;
        process = n-1;
        for(i = 0; i < n; i++) {
            if(arrival_time[i] <= time && remaining_time[i] < remaining_time[smallest] && remaining_time[i] > 0) {
                smallest = i;
            }
        }
        remaining_time[smallest]--;
        if(remaining_time[smallest] == 0) {
            process++;
            waiting_time[smallest] = time - arrival_time[smallest] - burst_time[smallest];
            turnaround_time[smallest] = time - arrival_time[smallest];
            sum_waiting_time += waiting_time[smallest];
            sum_turnaround_time += turnaround_time[smallest];
        }
        time++;
    }
    avg_waiting_time = (float)sum_waiting_time/n;
    avg_turnaround_time = (float)sum_turnaround_time/n;
    printf("Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
    for(i = 0; i < n; i++) {
        printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", pid[i], arrival_time[i], burst_time[i], waiting_time[i], turnaround_time[i]);
    }
    printf("Average waiting time: %.2f\n", avg_waiting_time);
    printf("Average turnaround time: %.2f\n", avg_turnaround_time);
    return 0;
}


above srtf

below ex

Enter the number of processes: 5
Enter the process ID, arrival time and burst time for process 1: 1 0 7
Enter the process ID, arrival time and burst time for process 2: 2 2 4
Enter the process ID, arrival time and burst time for process 3: 3 4 1
Enter the process ID, arrival time and burst time for process 4: 4 5 4
Enter the process ID, arrival time and burst time for process 5: 5 6 3

Process		Arrival Time	Burst Time	Waiting Time	Turnaround Time
1			0			7			5			12
2			2			4			1			5
3			4			1			0			1
4			5			4			2			6
5			6			3			1			4
Average Waiting Time: 1.800000
Average Turnaround Time: 5.600000


priority schedule below



#include<iostream>
using namespace std;

int main() {
    int n, i, j, pos, temp, sum_waiting_time = 0, sum_turnaround_time = 0;
    float avg_waiting_time, avg_turnaround_time;
    cout<<"Enter the number of processes: ";
    cin>>n;
    int pid[n], arrival_time[n], burst_time[n], priority[n], waiting_time[n], turnaround_time[n];
    for(i = 0; i < n; i++) {
        cout<<"Enter the process ID, arrival time, burst time and priority for process "<<i+1<<": ";
        cin>>pid[i]>>arrival_time[i]>>burst_time[i]>>priority[i];
    }
    for(i = 0; i < n; i++) {
        pos = i;
        for(j = i+1; j < n; j++) {
            if(priority[j] < priority[pos]) {
                pos = j;
            }
        }
        temp = priority[i];
        priority[i] = priority[pos];
        priority[pos] = temp;

        temp = burst_time[i];
        burst_time[i] = burst_time[pos];
        burst_time[pos] = temp;

        temp = pid[i];
        pid[i] = pid[pos];
        pid[pos] = temp;

        temp = arrival_time[i];
        arrival_time[i] = arrival_time[pos];
        arrival_time[pos] = temp;
    }
    waiting_time[0] = 0;
    for(i = 1; i < n; i++) {
        waiting_time[i] = 0;
        for(j = 0; j < i; j++) {
            waiting_time[i] += burst_time[j];
        }
        waiting_time[i] = waiting_time[i] - arrival_time[i];
        sum_waiting_time += waiting_time[i];
    }
    avg_waiting_time = (float)sum_waiting_time/n;
    cout<<"Process ID\tArrival Time\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n";
    for(i = 0; i < n; i++) {
        turnaround_time[i] = burst_time[i] + waiting_time[i];
        sum_turnaround_time += turnaround_time[i];
        cout<<pid[i]<<"\t\t"<<arrival_time[i]<<"\t\t"<<burst_time[i]<<"\t\t"<<priority[i]<<"\t\t"<<waiting_time[i]<<"\t\t"<<turnaround_time[i]<<endl;
    }
    avg_turnaround_time = (float)sum_turnaround_time/n;
    cout<<"Average waiting time: "<<avg_waiting_time<<endl;
    cout<<"Average turnaround time: "<<avg_turnaround_time<<endl;
    return 0;
}



pririty premtive below


#include <stdio.h>

int main() {
    int n, i, j, time = 0, highest_priority, process = -1, sum_waiting_time = 0, sum_turnaround_time = 0;
    float avg_waiting_time, avg_turnaround_time;
    printf("Enter the number of processes: ");
    scanf("%d", &n);
    int pid[n], arrival_time[n], burst_time[n], priority[n], remaining_time[n], waiting_time[n], turnaround_time[n];
    for(i = 0; i < n; i++) {
        printf("Enter the process ID, arrival time, burst time and priority for process %d: ", i+1);
        scanf("%d%d%d%d", &pid[i], &arrival_time[i], &burst_time[i], &priority[i]);
        remaining_time[i] = burst_time[i];
    }
    while(process != n-1) {
        highest_priority = -1;
        for(i = 0; i < n; i++) {
            if(arrival_time[i] <= time && remaining_time[i] > 0) {
                if(highest_priority == -1 || priority[i] < priority[highest_priority]) {
                    highest_priority = i;
                }
            }
        }
        remaining_time[highest_priority]--;
        if(remaining_time[highest_priority] == 0) {
            process++;
            waiting_time[highest_priority] = time - arrival_time[highest_priority] - burst_time[highest_priority];
            turnaround_time[highest_priority] = time - arrival_time[highest_priority];
            sum_waiting_time += waiting_time[highest_priority];
            sum_turnaround_time += turnaround_time[highest_priority];
        }
        time++;
    }
    avg_waiting_time = (float)sum_waiting_time/n;
    avg_turnaround_time = (float)sum_turnaround_time/n;
    printf("Process ID\tArrival Time\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");
    for(i = 0; i < n; i++) {
        printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", pid[i], arrival_time[i], burst_time[i], priority[i], waiting_time[i], turnaround_time[i]);
    }
    printf("Average waiting time: %.2f\n", avg_waiting_time);
    printf("Average turnaround time: %.2f\n", avg_turnaround_time);
    return 0;
}