Assignment1 Os 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

ASSIGNMENT-3

NAME: - VAIbhAV ShuklA


Sub:- OpErATING SySTEM
STd: - Ty bSc (cS)
dIV: -A
rOll NO.: - 233331072
bATch: - b4
Set A:

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>

// Define a process structure to hold process information

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

Void calculateTimes(struct Process processes[], int n) {

Int currentTime = 0;

For (int I = 0; I < n; i++) {

If (currentTime < processes[i].arrivalTime)

currentTime = processes[i].arrivalTime;

processes[i].completionTime = currentTime + processes[i].burstTime;

processes[i].turnaroundTime = processes[i].completionTime – processes[i].arrivalTime;

processes[i].waitingTime = processes[i].turnaroundTime – processes[i].burstTime;


currentTime = processes[i].completionTime;

// Function to perform FCFS scheduling

Void fcfsScheduling(struct Process processes[], int n) {

calculateTimes(processes, n);

printf(“Gantt Chart:\n”);

printf(“ Process\tStart Time\tEnd Time\n”);

for (int I = 0; I < n; i++) {

printf(“ P%d\t\t%d\t\t%d\n”, processes[i].id, processes[i].arrivalTime,


processes[i].completionTime);

Double totalTurnaroundTime = 0, totalWaitingTime = 0;

For (int I = 0; I < n; i++) {

totalTurnaroundTime += processes[i].turnaroundTime;

totalWaitingTime += processes[i].waitingTime;

Printf(“\nAverage Turnaround Time: %.2lf\n”, totalTurnaroundTime / n);

Printf(“Average Waiting Time: %.2lf\n”, totalWaitingTime / n);

Int main() {

Int n;

Printf(“Enter the number of processes: “);


Scanf(“%d”, &n);

Struct Process processes[n];

// Input arrival time and burst time for each process

For (int I = 0; I < n; i++) {

Processes[i].id = I + 1;

Printf(“Enter Arrival Time for P%d: “, I + 1);

Scanf(“%d”, &processes[i].arrivalTime);

Printf(“Enter Burst Time for P%d: “, I + 1);

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>

// Define a process structure to hold process information

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

Void calculateTimes(struct Process processes[], int n) {

Int currentTime = 0;

Int completedProcesses = 0;

While (completedProcesses < n) {

Int shortestJob = -1;

Int shortestTime = INT_MAX;

For (int I = 0; I < n; i++) {

If (processes[i].arrivalTime <= currentTime && processes[i].burstTime < shortestTime &&


processes[i].burstTime > 0) {
shortestJob = I;

shortestTime = processes[i].burstTime;

If (shortestJob == -1) {

currentTime++;

} else {

Processes[shortestJob].completionTime = currentTime + processes[shortestJob].burstTime;

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++;

// Function to perform Non-preemptive SJF scheduling

Void sjfScheduling(struct Process processes[], int n) {

calculateTimes(processes, n);

printf(“Gantt Chart:\n”);

printf(“ Process\tStart Time\tEnd Time\n”);

for (int I = 0; I < n; i++) {

printf(“ P%d\t\t%d\t\t%d\n”, processes[i].id, processes[i].arrivalTime,


processes[i].completionTime);

}
Double totalTurnaroundTime = 0, totalWaitingTime = 0;

For (int I = 0; I < n; i++) {

totalTurnaroundTime += processes[i].turnaroundTime;

totalWaitingTime += processes[i].waitingTime;

Printf(“\nAverage Turnaround Time: %.2lf\n”, totalTurnaroundTime / n);

Printf(“Average Waiting Time: %.2lf\n”, totalWaitingTime / n);

Int main() {

Int n;

Printf(“Enter the number of processes: “);

Scanf(“%d”, &n);

Struct Process processes[n];

// Input arrival time and burst time for each process

For (int I = 0; I < n; i++) {

Processes[i].id = I + 1;

Printf(“Enter Arrival Time for P%d: “, I + 1);

Scanf(“%d”, &processes[i].arrivalTime);

Printf(“Enter Burst Time for P%d: “, I + 1);

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>

// Define a process structure to hold process information

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

Void calculateTimes(struct Process processes[], int n) {

Int currentTime = 0;

Int completedProcesses = 0;

While (completedProcesses < n) {

Int shortestJob = -1;

Int shortestTime = INT_MAX;

For (int I = 0; I < n; i++) {

If (processes[i].arrivalTime <= currentTime && processes[i].remainingTime < shortestTime &&


processes[i].remainingTime > 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++;

}
}

// Function to perform Preemptive SJF scheduling

Void preemptiveSjfScheduling(struct Process processes[], int n) {

For (int I = 0; I < n; i++) {

Processes[i].remainingTime = processes[i].burstTime;

calculateTimes(processes, n);

printf(“Gantt Chart:\n”);

printf(“ Process\tStart Time\tEnd Time\n”);

for (int I = 0; I < n; i++) {

printf(“ P%d\t\t%d\t\t%d\n”, processes[i].id, processes[i].arrivalTime,


processes[i].completionTime);

Double totalTurnaroundTime = 0, totalWaitingTime = 0;

For (int I = 0; I < n; i++) {

totalTurnaroundTime += processes[i].turnaroundTime;

totalWaitingTime += processes[i].waitingTime;

Printf(“\nAverage Turnaround Time: %.2lf\n”, totalTurnaroundTime / n);

Printf(“Average Waiting Time: %.2lf\n”, totalWaitingTime / n);

Int main() {

Int n;
Printf(“Enter the number of processes: “);

Scanf(“%d”, &n);

Struct Process processes[n];

// Input arrival time and burst time for each process

For (int I = 0; I < n; i++) {

Processes[i].id = I + 1;

Printf(“Enter Arrival Time for P%d: “, I + 1);

Scanf(“%d”, &processes[i].arrivalTime);

Printf(“Enter Burst Time for P%d: “, I + 1);

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>

// Define a process structure to hold process information

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

Void calculateTimes(struct Process processes[], int n) {

Int currentTime = 0;
Int completedProcesses = 0;

While (completedProcesses < n) {

Int highestPriorityJob = -1;

Int highestPriority = INT_MAX;

For (int I = 0; I < n; i++) {

If (processes[i].arrivalTime <= currentTime && processes[i].priority < highestPriority &&


processes[i].burstTime > 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++;

// Function to perform Non-preemptive Priority scheduling

Void nonPreemptivePriorityScheduling(struct Process processes[], int n) {


calculateTimes(processes, n);

printf(“Gantt Chart:\n”);

printf(“ Process\tStart Time\tEnd Time\n”);

for (int I = 0; I < n; i++) {

printf(“ P%d\t\t%d\t\t%d\n”, processes[i].id, processes[i].arrivalTime,


processes[i].completionTime);

Double totalTurnaroundTime = 0, totalWaitingTime = 0;

For (int I = 0; I < n; i++) {

totalTurnaroundTime += processes[i].turnaroundTime;

totalWaitingTime += processes[i].waitingTime;

Printf(“\nAverage Turnaround Time: %.2lf\n”, totalTurnaroundTime / n);

Printf(“Average Waiting Time: %.2lf\n”, totalWaitingTime / n);

Int main() {

Int n;

Printf(“Enter the number of processes: “);

Scanf(“%d”, &n);

Struct Process processes[n];

// Input arrival time, burst time, and priority for each process

For (int I = 0; I < n; i++) {

Processes[i].id = I + 1;
Printf(“Enter Arrival Time for P%d: “, I + 1);

Scanf(“%d”, &processes[i].arrivalTime);

Printf(“Enter Burst Time for P%d: “, I + 1);

Scanf(“%d”, &processes[i].burstTime);

Printf(“Enter Priority for P%d: “, I + 1);

Scanf(“%d”, &processes[i].priority);

nonPreemptivePriorityScheduling(processes, n);

return 0;

You might also like