SchedulingAlgos-3_tanweer
SchedulingAlgos-3_tanweer
SchedulingAlgos-3_tanweer
Date: 07/08/24
Program
#include <stdio.h>
int main() {
int p[10], at[10], bt[10], ct[10], tat[10], wt[10];
int i, j, temp = 0, n;
float awt = 0, atat = 0;
// Calculate averages
atat = atat / n;
awt = awt / n;
// Average times
printf("\n\nAverage Turnaround Time: %.2f", atat);
printf("\nAverage Waiting Time: %.2f\n", awt);
return 0;
}
Output
tanweer@Tanweer:~/CpuScheduling$ gcc FCFS.c
tanweer@Tanweer:~/CpuScheduling$ ./a.out
Enter the number of processes: 4
Enter the process IDs: 1 2 3 4
Enter the arrival times: 0 1 2 3
Enter the burst times: 7 6 4 8
P1 0 7 7 7 0
P2 1 6 13 12 6
P3 2 4 17 15 11
P4 3 8 25 22 14
Shortest Job First (SJF) is a CPU Scheduling algorithm in which CPU allocation is based
on burst time. The job with less burst time will get scheduled first.
#include<stdio.h>
#include<stdlib.h>
void swap(int *x, int *y) {
int temp=*x;
*x=*y;
*y=temp;
}
void sortat(int p[], int at[], int bt[], int n) {
int i, j;
for(i=0;i<n;i++) {
for(j=i+1;j<n;j++) { /* sort the process having less arrival*/
if(at[i]>at[j]) {
swap(&p[i], &p[j]);
swap(&at[i], &at[j]);
swap(&bt[i], &bt[j]);
}
/* if two processes have the same arrival time than sort them having less burst
time */
else if(at[i]==at[j]) {
if(bt[i]>bt[j])
swap(&p[i], &p[j]);
swap(&at[i], &at[j]);
swap(&bt[i], &bt[j]);
}
}
}
}
/* calculate turnaround time and waiting time */
void tatwt( int ct[], int at[], int bt[], int tat[], int wt[], int n) {
int i;
for(i=0;i<n;i++) {
tat[i]=ct[i]-at[i];
wt[i]=tat[i]-bt[I];
}
}
int main() {
int *p, *at, *bt, *tat, *wt, *ct, pos, i, j, min=1000, n;
float awt=0, atat=0;
printf("\nEnter the number of process: ");
scanf("%d", &n);
p=(int*)malloc(n*sizeof(int));
at=(int*)malloc(n*sizeof(int));
bt=(int*)malloc(n*sizeof(int));
ct=(int*)malloc(n*sizeof(int));
wt=(int*)malloc(n*sizeof(int));
tat=(int*)malloc(n*sizeof(int));
printf("enter the process");
for(i=0;i<n;i++) {
scanf("%d",&p[i]);
}
printf("Enter the arrival time: ");
for(i=0;i<n;i++) {
scanf("%d",&at[i]);
}
printf("Enter the burst time: ");
for(i=0;i<n;i++) {
scanf("%d",&bt[i]);
}
sortat(p, at, bt, n);
ct[0]=at[0] + bt[0];
for(i=1; i<n; i++) {
for(j=i; j<n; j++) {
if(at[j]<=ct[i-1]) {
if(bt[j]<min) {
min=bt[j];
pos=j;
}
}
}
/* when you get less burst time process, swap p, at, bt at position 2, and when getting
2nd less burst time swap at position 3rd and so on. */
swap(&p[i], &p[pos]);
swap(&at[i], &at[pos]);
swap(&bt[i], &bt[pos]);
min=1000;
ct[i]=ct[i-1]+bt[i];
}
tatwt(ct, at, bt, tat, wt, n);
printf("\n Process Arrival Time Burst Time Completion TAT WT ");
for(i=0;i<n;i++) {
printf("\n%d\t %d\t %d\t %d\t %d\t %d",p[i], at[i], bt[i], ct[i], tat[i], wt[i]);
}
for(i=0;i<n;i++) {
atat+=tat[i];
awt+=wt[i];
}
// average turnaround time and average waiting time
atat=atat/n;
awt=awt/n;
printf("\n avg tat=%.2f and avg wt=%.2f",atat, awt);
return 0;
}
Output:
tanweer@Tanweer:~/CpuScheduling$ gcc SJF.c
tanweer@Tanweer:~/CpuScheduling$ ./a.out
Enter the number of process: 4
Enter the process: 1 2 3 4
Enter the arrival time: 0 1 3 0
Enter the burst time: 5 3 4 3
#include <stdio.h>
#include <limits.h>
while (completed != n) {
// Find the process with the smallest remaining burst time
for (int i = 0; i < n; i++) {
if (at[i] <= time && remaining_bt[i] > 0 && remaining_bt[i] < min_bt) {
min_bt = remaining_bt[i];
shortest = i;
check = 1;
}
}
if (check == 0) {
time++;
continue;
}
if (wt[shortest] < 0)
wt[shortest] = 0;
}
time++;
}
}
printf("\nProcess\tAT\tBT\tWT\tTAT\n");
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
printf("P%d\t%d\t%d\t%d\t%d\n", i + 1, at[i], bt[i], wt[i], tat[i]);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
Process AT BT WT TAT
P1 0 8 9 17
P2 1 4 0 4
P3 2 9 15 24
P4 3 5 2 7
A round-robin scheduling algorithm is used to schedule the process fairly for each job
in a time slot or quantum and interrupt the job if it is not completed by then the job
comes after the other job which arrives in the quantum time making these scheduling
fairly.
#include <stdio.h>
#include <stdbool.h>
void queueUpdation(int queue[], int timer, int arrival[], int n, int maxProccessIndex) {
int zeroIndex = -1;
for (int i = 0; i < n; i++) {
if (queue[i] == 0) {
zeroIndex = i;
break;
}
}
if (zeroIndex != -1) {
queue[zeroIndex] = maxProccessIndex + 1;
}
}
void checkNewArrival(int timer, int arrival[], int n, int *maxProccessIndex, int queue[]) {
if (timer <= arrival[n - 1]) {
bool newArrival = false;
for (int j = (*maxProccessIndex + 1); j < n; j++) {
if (arrival[j] <= timer) {
if (*maxProccessIndex < j) {
*maxProccessIndex = j;
newArrival = true;
}
}
}
if (newArrival) {
queueUpdation(queue, timer, arrival, n, *maxProccessIndex);
}
}
}
int main() {
int n, tq, timer = 0, maxProccessIndex = 0;
float avgWait = 0, avgTT = 0;
while (1) {
bool flag = true;
for (int i = 0; i < n; i++) {
if (temp_burst[i] != 0) {
flag = false;
break;
}
}
if (flag)
break;
if (idle) {
timer++;
checkNewArrival(timer, arrival, n, &maxProccessIndex, queue);
}
queueMaintainence(queue, n);
}
}
return 0;
}
OUTPUT
tanweer@Tanweer:~/CpuScheduling$ gcc RR.c
tanweer@Tanweer:~/CpuScheduling$ ./a.out
Enter the time quanta: 3
Enter the number of processes: 4
Enter the arrival time of the processes: 0 1 2 3
Enter the burst time of the processes: 6 5 3 1
Program No. Arrival Time Burst Time Wait Time Turn Around Time
1 0 6 7 13
2 1 5 9 14
3 2 3 4 7
4 3 1 6 7
Average wait time: 6.50
Average Turn Around Time: 10.25
if (procA->at == procB->at) {
return procA->pr - procB->pr; // Sort by priority if arrival times are the same
} else {
return procA->at - procB->at; // Otherwise, sort by arrival time
}
}
printf("Process_no\tStart_time\tComplete_time\tTurn_Around_Time\tWaiting_Time\n");
// Input priorities
printf("Enter the priorities of the processes: ");
for (int i = 0; i < totalprocess; i++) {
scanf("%d", &proc[i].pr);
}
return 0;
}
OUTPUT