Assignment1 Os 3
Assignment1 Os 3
Assignment1 Os 3
i. Write the program to simulate FCFS CPU-scheduling. The arrival time and first CPU burst
for different n number of processes should be input to the algorithm. Assume that The
fixed IO waiting time (2 units). The next CPU-burst should be generated randomly. The
output should give Gantt chart, turnaround time and waiting time for each process.Also
find the average waiting time and turnaround time.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Struct Process {
Int id;
Int arrivalTime;
Int burstTime;
Int completionTime;
Int turnaroundTime;
Int waitingTime;
};
// Function to calculate turnaround time and waiting time for each process
Int currentTime = 0;
currentTime = processes[i].arrivalTime;
calculateTimes(processes, n);
printf(“Gantt Chart:\n”);
totalTurnaroundTime += processes[i].turnaroundTime;
totalWaitingTime += processes[i].waitingTime;
Int main() {
Int n;
Processes[i].id = I + 1;
Scanf(“%d”, &processes[i].arrivalTime);
Scanf(“%d”, &processes[i].burstTime);
fcfsScheduling(processes, n);
return 0;
}
ii. Write the program to simulate Non-preemptive Shortest Job First (SJF) -scheduling. The
Arrival time and first CPU-burst for different n number of processes should be input to
the Algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be
Generated randomly. The output should give Gantt chart, turnaround time and waiting
Time for each process. Also find the average waiting time and turnaround time.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Struct Process {
Int id;
Int arrivalTime;
Int burstTime;
Int completionTime;
Int turnaroundTime;
Int waitingTime;
};
// Function to calculate turnaround time and waiting time for each process
Int currentTime = 0;
Int completedProcesses = 0;
shortestTime = processes[i].burstTime;
If (shortestJob == -1) {
currentTime++;
} else {
Processes[shortestJob].turnaroundTime = processes[shortestJob].completionTime –
processes[shortestJob].arrivalTime;
Processes[shortestJob].waitingTime = processes[shortestJob].turnaroundTime –
processes[shortestJob].burstTime;
Processes[shortestJob].burstTime = 0;
currentTime = processes[shortestJob].completionTime;
completedProcesses++;
calculateTimes(processes, n);
printf(“Gantt Chart:\n”);
}
Double totalTurnaroundTime = 0, totalWaitingTime = 0;
totalTurnaroundTime += processes[i].turnaroundTime;
totalWaitingTime += processes[i].waitingTime;
Int main() {
Int n;
Scanf(“%d”, &n);
Processes[i].id = I + 1;
Scanf(“%d”, &processes[i].arrivalTime);
Scanf(“%d”, &processes[i].burstTime);
sjfScheduling(processes, n);
return 0;
}
Set B:
i. Write the program to simulate Preemptive Shortest Job First (SJF) -scheduling. The
Arrival time and first CPU-burst for different n number of processes should be input to
the Algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be
Generated randomly. The output should give Gantt chart, turnaround time and waiting
Time for each process. Also find the average waiting time and turnaround time.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Struct Process {
Int id;
Int arrivalTime;
Int burstTime;
Int completionTime;
Int turnaroundTime;
Int waitingTime;
Int remainingTime;
};
// Function to calculate turnaround time and waiting time for each process
Int currentTime = 0;
Int completedProcesses = 0;
shortestJob = I;
shortestTime = processes[i].remainingTime;
If (shortestJob == -1) {
currentTime++;
} else {
Processes[shortestJob].remainingTime--;
currentTime++;
if (processes[shortestJob].remainingTime == 0) {
processes[shortestJob].completionTime = currentTime;
processes[shortestJob].turnaroundTime = processes[shortestJob].completionTime –
processes[shortestJob].arrivalTime;
processes[shortestJob].waitingTime = processes[shortestJob].turnaroundTime –
processes[shortestJob].burstTime;
completedProcesses++;
}
}
Processes[i].remainingTime = processes[i].burstTime;
calculateTimes(processes, n);
printf(“Gantt Chart:\n”);
totalTurnaroundTime += processes[i].turnaroundTime;
totalWaitingTime += processes[i].waitingTime;
Int main() {
Int n;
Printf(“Enter the number of processes: “);
Scanf(“%d”, &n);
Processes[i].id = I + 1;
Scanf(“%d”, &processes[i].arrivalTime);
Scanf(“%d”, &processes[i].burstTime);
preemptiveSjfScheduling(processes, n);
return 0;
}
ii. Write the program to simulate Non-preemptive Priority scheduling. The arrival time and
First CPU-burst and priority for different n number of processes should be input to the
Algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be
Generated randomly. The output should give Gantt chart, turnaround time and waiting
Time for each process. Also find the average waiting time and turnaround time.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Struct Process {
Int id;
Int arrivalTime;
Int burstTime;
Int priority;
Int completionTime;
Int turnaroundTime;
Int waitingTime;
};
// Function to calculate turnaround time and waiting time for each process
Int currentTime = 0;
Int completedProcesses = 0;
highestPriorityJob = I;
highestPriority = processes[i].priority;
If (highestPriorityJob == -1) {
currentTime++;
} else {
Processes[highestPriorityJob].completionTime = currentTime +
processes[highestPriorityJob].burstTime;
Processes[highestPriorityJob].turnaroundTime =
processes[highestPriorityJob].completionTime – processes[highestPriorityJob].arrivalTime;
Processes[highestPriorityJob].waitingTime = processes[highestPriorityJob].turnaroundTime –
processes[highestPriorityJob].burstTime;
Processes[highestPriorityJob].burstTime = 0;
currentTime = processes[highestPriorityJob].completionTime;
completedProcesses++;
printf(“Gantt Chart:\n”);
totalTurnaroundTime += processes[i].turnaroundTime;
totalWaitingTime += processes[i].waitingTime;
Int main() {
Int n;
Scanf(“%d”, &n);
// Input arrival time, burst time, and priority for each process
Processes[i].id = I + 1;
Printf(“Enter Arrival Time for P%d: “, I + 1);
Scanf(“%d”, &processes[i].arrivalTime);
Scanf(“%d”, &processes[i].burstTime);
Scanf(“%d”, &processes[i].priority);
nonPreemptivePriorityScheduling(processes, n);
return 0;