assignmnt
assignmnt
First-Come-First-Serve (FCFS):
In FCFS, the process that arrives first is executed first. This
scheduling algorithm is simple and easy to implement, but it can
result in longer wait times for processes that arrive later.
Priority Scheduling:
In priority scheduling, each process is assigned a priority, and the
process with the highest priority is executed first. This scheduling
algorithm is useful when some processes are more important than
others, but can result in lower priority processes waiting for long
periods of time.
Round Robin:
In Round Robin, each process is given a fixed amount of time to
execute before being preempted and allowing the next process to run.
This algorithm ensures that each process gets a fair share of the CPU
time, but can result in longer turnaround times for processes with
longer burst times.
Assumptions:
int main() {
Process processes[] = {
{1, 0, 10, 0, 0},
{2, 2, 5, 0, 0},
{3, 4, 2, 0, 0},
};
int num_processes = sizeof(processes) / sizeof(Process);
fcfs(processes, num_processes);
printf("FCFS Scheduling:\n");
for (int i = 0; i < num_processes; i++) {
printf("Process %d: Waiting Time=%d, Turnaround
Time=%d\n", processes[i].pid, processes[i].waiting_time,
processes[i].turnaround_time);
}
return 0;
}
Output
/tmp/CjgLvtuEQt.o
FCFS Scheduling:
Process 1: Waiting Time=0, Turnaround Time=10
Process 2: Waiting Time=8, Turnaround Time=13
Process 3: Waiting Time=11, Turnaround Time=13
int main() {
int n, i, j, temp, total_time = 0, avg_waiting_time = 0,
avg_turnaround_time = 0;
int burst_time[20], process[20], waiting_time[20],
turnaround_time[20];
printf("Enter the number of processes: ");
scanf("%d", &n);
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];
}
total_time += waiting_time[i];
}
avg_waiting_time = total_time / n;
total_time = 0;
avg_turnaround_time = total_time / n;
return 0;
}
output
Enter the number of processes: 10
Enter the burst time for process 1: 20
Enter the burst time for process 2: 14
Enter the burst time for process 3: 25
Enter the burst time for process 4: 45
Enter the burst time for process 5: 41
Enter the burst time for process 6: 10
Enter the burst time for process 7: 24
Enter the burst time for process 8: 1
Enter the burst time for process 9: 3
Enter the burst time for process 10: 1
Process Burst Time Waiting Time Turnaround Time
8 1 0 1
10 1 1 2
9 3 2 5
6 10 5 15
2 14 15 29
1 20 29 49
7 24 49 73
3 25 73 98
5 41 98 139
4 45 139 184
#include<stdio.h>
int main() {
int n, i, j, temp, total = 0;
float avg_waiting_time, avg_turnaround_time;
int burst_time[20], priority[20], waiting_time[20],
turnaround_time[20];
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];
}
total += waiting_time[i];
}
avg_waiting_time = (float)total / n;
total = 0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround
Time");
for(i = 0; i < n; i++) {
turnaround_time[i] = burst_time[i] + waiting_time[i];
total += turnaround_time[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d", i+1, burst_time[i],
waiting_time[i], turnaround_time[i]);
}
avg_turnaround_time = (float)total / n;
printf("\n\nAverage Waiting Time: %.2f", avg_waiting_time);
printf("\nAverage Turnaround Time: %.2f\n",
avg_turnaround_time);
return 0;
}
Output
Enter the number of processes: 2
Enter burst time and priority for each process:
Process 1: 2
1
Process 2: 2
1
Process Burst Time Waiting Time Turnaround Time
P[1] 2 0 2
P[2] 2 2 4
Average Waiting Time: 1.00
Average Turnaround Time: 3.00
#define MAX_PROCESSES 10
#define TIME_QUANTUM 2
struct Process {
int pid;
int arrivalTime;
int burstTime;
int remainingTime;
};
int main() {
struct Process processes[MAX_PROCESSES];
int numProcesses, i, j, time, totalBurstTime = 0;
float avgWaitingTime = 0;
avgWaitingTime /= numProcesses;
printf("Average waiting time: %.2f\n", avgWaitingTime);
return 0;
}
Output:-
Enter the number of processes: 1
Enter the arrival time and burst time for process 1: 2