Cpu Scheduling Programs
Cpu Scheduling Programs
void fcfs(int at[], int bt[], int wt[], int tat[], int n)
{
int completion_time = 0;
for (int i = 0; i < n; i++)
{
if (at[i] > completion_time)
{
// If the process hasn't arrived yet, wait for it to
arrive
completion_time = at[i];
}
wt[i] = completion_time - at[i];
tat[i] = wt[i] + bt[i];
completion_time += bt[i];
}
}
printf("P\tAT\tBT\tWT\tTAT\n");
for (int i = 0; i < n; i++)
{
printf("P%d\t%d\t%d\t%d\t%d\n", p[i], at[i], bt[i], wt[i],
tat[i]);
}
int main()
{
int n;
int p[100], bt[100], at[100];
printf("Enter the number of processes:");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Process %d (Burst Time):", i + 1);
scanf("%d",&bt[i]);
at[i]=0;
p[i] = i + 1;
}
sort_by_at(p, at, bt, n);
findAvgTime(p, at, bt, n);
return 0;
}
int main()
{
int n;
printf("Enter the number of processes:");
scanf("%d", &n);
Process *p;
p = (Process *)malloc(sizeof(Process) * n);
for (int i = 0; i < n; i++)
{
printf("Process %d (Burst Time):", i + 1);
scanf("%d",&p[i].bt);
p[i].at=0;
p[i].p_num = i + 1;
}
sort_by_at(p, n);
findAvgTime(p, n);
free(p);
return 0;
}
3. non-preemptive SJF without process structure
#include <stdio.h>
int main()
{
int at[10], bt[10], p[10];
int n, i, j, temp, exit_time = 0, count, over = 0, sum_wait = 0,
sum_turnaround = 0, start;
float avg_wait, avg_turn;
printf("\nProcess\t\tAT\t\tBT\t\tStart\t\tWT\t\tTAT\n");
if (count > 1)
{
for (i=over; i<over+count-1; i++)
{
for (j=i+1; j<over+count; j++)
{
if (bt[i] > bt[j])
{
temp = at[i]; at[i] = at[j]; at[j] = temp;
temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;
temp = p[i]; p[i] = p[j]; p[j] = temp;
}
}
}
}
start = exit_time;
exit_time += bt[over];
int waiting_time = start - at[over];
int turnaround_time = exit_time - at[over];
sum_wait += waiting_time;
sum_turnaround += turnaround_time;
over++;
}
struct Process {
int pid;
int arrival_time;
int burst_time;
};
int main()
{
struct Process *processes;
int n, temp, exit_time = 0, count, over = 0, sum_wait = 0, sum_turnaround =
0, start;
float avg_wait, avg_turn;
printf("\nProcess\t\tAT\t\tBT\t\tStart\t\tWT\t\tTAT\n");
if (count > 1)
{
for (int i = over; i < over + count - 1; i++)
{
for (int j = i + 1; j < over + count; j++)
{
if (processes[i].burst_time > processes[j].burst_time)
{
struct Process temp_proc = processes[i];
processes[i] = processes[j];
processes[j] = temp_proc;
}
}
}
}
start = exit_time;
exit_time += processes[over].burst_time;
int waiting_time = start - processes[over].arrival_time;
int turnaround_time = exit_time - processes[over].arrival_time;
printf("p[%d]\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[over].pid,
processes[over].arrival_time, processes[over].burst_time, start, waiting_time,
turnaround_time);
sum_wait += waiting_time;
sum_turnaround += turnaround_time;
over++;
}
free(processes);
return 0;
}
5. round robin without process structure
#include <stdio.h>
void main()
{
int i=0, sum = 0, wt = 0, tat = 0, count = 0, quant, at[10], bt[10],
temp[10], NOP, y = 0, time = 0;
printf("Enter the number of processes: ");
scanf("%d", &NOP);
y = NOP;
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=time)
{
i++;
}
else
{
i=0;
}
}
printf("Average Waiting Time: %d\n", wt/NOP);
printf("Average Turn Around Time: %d\n", tat/NOP);
}
struct process{
int at;
int bt;
int wt;
int tat;
};
void main()
{
int i = 0, sum = 0, count = 0, quant, NOP, y = 0, time = 0, total_wt = 0,
total_tat = 0;
printf("Enter the number of processes: ");
scanf("%d", &NOP);
y = NOP;
if(i==NOP-1)
{
i=0;
}
else if(p[i+1].at<=time)
{
i++;
}
else
{
i=0;
}
}
printf("Average Waiting Time: %d\n", total_wt/NOP);
printf("Average Turn Around Time: %d\n", total_tat/NOP);
}
7. non-preemptive priority without process structure
#include <stdio.h>
int main()
{
int p[100], bt[100], index[100];
int n;
printf("Enter the no. of processes: ");
scanf("%d", &n);
for(int i=0; i<n; i++)
{
printf("Enter the burst time and priority value for the
process %d\n", i+1);
scanf("%d %d", &bt[i], &p[i]);
index[i]=i+1;
}
swap(&p[i], &p[m]);
swap(&bt[i], &bt[m]);
swap(&index[i], &index[m]);
}
int t=0;
printf("\nORDER OF PROCESS EXECUTION\n");
for(int i=0; i<n; i++)
{
printf("p%d is executed from %d to %d \n", index[i], t, t+bt[i]);
t+=bt[i];
}
typedef struct
{
int pid;
int bt;
int priority;
int wt;
int tat;
} Process;
int main()
{
Process *p;
int n;
swap(&p[i], &p[m]);
}
int t = 0;
printf("\nORDER OF PROCESS EXECUTION\n");
for (int i = 0; i < n; i++)
{
printf("Process %d is executed from %d to %d\n", p[i].pid, t, t +
p[i].bt);
t += p[i].bt;
}
printf("\nPID\tBT\tWT\tTAT\n");
int wt = 0;
for (int i = 0; i < n; i++)
{
p[i].wt = wt;
p[i].tat = wt + p[i].bt;
printf("%d\t%d\t%d\t%d\n", p[i].pid, p[i].bt, p[i].wt, p[i].tat);
wt += p[i].bt;
}
free(p);
return 0;
}