Assignment-03
Title
Implement the C program for CPU Scheduling Algorithms: Shortest Job First (Preemptive) and Round Robin with different arrival time.
Theory
Shortest Job First (SJF) is a non-preemptive or preemptive scheduling algorithm that selects the waiting process with the smallest execution time to execute next. In the preemptive version, if a new process arrives with a shorter burst time than the currently running process, the currently running process is preempted and placed back into the ready queue.
Code Implementation
#include<stdio.h>
struct Process {
int pid;
int arrival_time;
int burst_time;
int remaining_time;
int completion_time;
int turnaround_time;
int waiting_time;
};
void swap(struct Process *xp, struct Process *yp) {
struct Process temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(struct Process arr[], int n) {
int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j].arrival_time > arr[j+1].arrival_time) {
swap(&arr[j], &arr[j+1]);
}
}
}
}
int main() {
int n, i, time = 0, total_burst_time = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
// Input process details
for(i = 0; i < n; i++) {
processes[i].pid = i + 1;
printf("Enter the arrival time for process %d: ", processes[i].pid);
scanf("%d", &processes[i].arrival_time);
printf("Enter the burst time for process %d: ", processes[i].pid);
scanf("%d", &processes[i].burst_time);
processes[i].remaining_time = processes[i].burst_time;
total_burst_time += processes[i].burst_time;
}
bubbleSort(processes, n);
printf("\nGantt Chart: ");
printf("\n| Time | Process |");
while(total_burst_time > 0) {
int shortest_index = -1, shortest_burst_time = 99999;
for(i = 0; i < n; i++) {
if(processes[i].arrival_time <= time && processes[i].remaining_time > 0) {
if(processes[i].remaining_time < shortest_burst_time) {
shortest_burst_time = processes[i].remaining_time;
shortest_index = i;
}
}
}
if(shortest_index == -1) {
printf("\n| %3d | Idle |", time);
time++;
continue;
}
processes[shortest_index].remaining_time--;
total_burst_time--;
time++;
printf("\n| %3d | P%d |", time, processes[shortest_index].pid);
}
printf("\n\nProcess\t Turnaround Time\t Waiting Time\n");
for(i = 0; i < n; i++) {
processes[i].turnaround_time = processes[i].completion_time - processes[i].arrival_time;
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
printf("P%d\t\t%d\t\t\t%d\n", processes[i].pid, processes[i].turnaround_time, processes[i].waiting_time);
}
return 0;
}
Setup and Run
- Compile the program using any C compiler (e.g., gcc).
- Run the compiled executable file.
- Enter the number of processes, arrival time, and burst time for each process when prompted.
- View the Gantt chart and the process turnaround time and waiting time.
Example
Enter the number of processes: 3
Enter the arrival time for process 1: 0
Enter the burst time for process 1: 6
Enter the arrival time for process 2: 1
Enter the burst time for process 2: 4
Enter the arrival time for process 3: 2
Enter the burst time for process 3: 8
Output:
Gantt Chart:
| Time | Process |
| 1 | P1 |
| 2 | P2 |
| 3 | P2 |
| 4 | P2 |
| 5 | Idle |
| 6 | P1 |
| 7 | P3 |
| 8 | P3 |
| 9 | P3 |
| 10 | P3 |
| 11 | P3 |
| 12 | P3 |
| 13 | P3 |
| 14 | P3 |
Process Turnaround Time Waiting Time
P1 6 0
P2 3 0
P3 11 3
Contact
For any questions or clarifications, please contact Parth Sali at parthsali04@gmail.com.