0% found this document useful (0 votes)
12 views

OS

Uploaded by

pullurishikhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

OS

Uploaded by

pullurishikhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1.

Write a program for FCFS scheduling algorithm

Code :

#include <stdio.h>

#define N 10

Int main()

Int process[N], burst[N], arrival[N], wait[N], turnaround[N], n;

Int total_wait = 0, total_turnaround = 0;

Printf(“Enter number of processes: “);

Scanf(“%d”, &n);

Printf(“Enter the process id, arrival time, and burst time of all processes:\n”);

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

Printf(“Process %d:\n”, I + 1);

Printf(“Process ID: “);

Scanf(“%d”, &process[i]);

Printf(“Arrival Time: “);

Scanf(“%d”, &arrival[i]);

Printf(“Burst Time: “);

Scanf(“%d”, &burst[i]);

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

For (int j = 0; j < n – I – 1; j++)


{

If (arrival[j] > arrival[j + 1])

Int temp = arrival[j];

Arrival[j] = arrival[j + 1];

Arrival[j + 1] = temp;

Temp = burst[j];

Burst[j] = burst[j + 1];

Burst[j + 1] = temp;

Temp = process[j];

Process[j] = process[j + 1];

Process[j + 1] = temp;

Int currentTime = 0;

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

If (currentTime < arrival[i])

currentTime = arrival[i];

Wait[i] = currentTime – arrival[i];

currentTime += burst[i];

turnaround[i] = wait[i] + burst[i];

total_wait += wait[i];
total_turnaround += turnaround[i];

Printf(“Process ID \t Arrival Time \t Burst Time \t Waiting Time \t Turnaround Time\n”);

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

Printf(“%10d %15d %15d %15d %15d\n”, process[i], arrival[i], burst[i], wait[i],


turnaround[i]);

Printf(“Average waiting time: %.2f\n”, (float)total_wait / n);

Printf(“Average turnaround time: %.2f\n”, (float)total_turnaround / n);

Return 0;

2.Write a program for SJF – non preemptive algorithm

Code :
#include <stdio.h>

#define N 10

Int main()

Int process[N], burst[N], arrival[N], wait[N], turnaround[N];

Int completed[N] = {0};

Int n, currentTime = 0, totalCompleted = 0;

Int twt = 0, tat = 0;

Printf(“Enter number of processes: “);

Scanf(“%d”, &n);

Printf(“Enter the process ID of all processes: “);

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

Scanf(“%d”, &process[i]);

Printf(“Enter the arrival time of all processes: “);

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

Scanf(“%d”, &arrival[i]);

Printf(“Enter the burst time of all processes: “);

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

Scanf(“%d”, &burst[i]);

While (totalCompleted < n)


{

Int minBurst = 99999;

Int index = -1;

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

If (arrival[i] <= currentTime && !completed[i] && burst[i] < minBurst)

minBurst = burst[i];

index = I;

If (index != -1)

Wait[index] = currentTime – arrival[index];

currentTime += burst[index];

turnaround[index] = wait[index] + burst[index];

completed[index] = 1;

totalCompleted++;

Else

currentTime++;

Printf(“Process ID \t Arrival Time \t Burst Time \t Waiting Time \t Turnaround Time\n”);

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


{

Printf(“%9d %15d %15d %15d %15d\n”, process[i], arrival[i], burst[i], wait[i],


turnaround[i]);

Twt += wait[i];

Tat += turnaround[i];

Float awt = (float)twt / n;

Float att = (float)tat / n;

Printf(“Average waiting time: %.2f\n”, awt);

Printf(“Average turnaround time: %.2f\n”, att);

Return 0;

3.Write a program for SJF – preemptive algorithm

Code :
#include <stdio.h>

Struct Process

Int pid;

Int arrival_time;

Int burst_time;

Int remaining_time;

Int completion_time;

Int waiting_time;

Int turnaround_time;

};

Void findWaitingAndTurnaroundTime(struct Process processes[], int n)

Int completed = 0, time = 0, shortest;

Int finish_time;

Int is_completed[n];

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

Is_completed[i] = 0;

Processes[i].remaining_time = processes[i].burst_time;

While (completed != n)

Shortest = -1;

Int min_remaining_time = 1e9;

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


{

If (processes[i].arrival_time <= time && !is_completed[i] &&


processes[i].remaining_time < min_remaining_time)

Min_remaining_time = processes[i].remaining_time;

Shortest = I;

If (shortest != -1)

Processes[shortest].remaining_time--;

Time++;

If (processes[shortest].remaining_time == 0)

Is_completed[shortest] = 1;

Completed++;

Finish_time = time;

Processes[shortest].completion_time = finish_time;

Processes[shortest].waiting_time = finish_time – processes[shortest].burst_time –


processes[shortest].arrival_time;

If (processes[shortest].waiting_time < 0)

Processes[shortest].waiting_time = 0;

Processes[shortest].turnaround_time = finish_time –
processes[shortest].arrival_time;

}
}

Else

Time++;

Void findAverageTime(struct Process processes[], int n)

Int total_waiting_time = 0, total_turnaround_time = 0;

findWaitingAndTurnaroundTime(processes, n);

printf(“Processes | Arrival Time | Burst Time | Waiting Time | Turnaround Time\n”);

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

Total_waiting_time += processes[i].waiting_time;

Total_turnaround_time += processes[i].turnaround_time;

Printf(“%9d | %12d | %10d | %12d | %15d\n”, processes[i].pid,


processes[i].arrival_time, processes[i].burst_time, processes[i].waiting_time,
processes[i].turnaround_time);

Printf(“\nAverage waiting time = %.2f”, (float)total_waiting_time / n);

Printf(“\nAverage turnaround time = %.2f\n”, (float)total_turnaround_time / n);

Int main()

Int n;

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


Scanf(“%d”, &n);

Struct Process processes[n];

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

Printf(“Enter arrival time and burst time for process %d: “, I + 1);

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

Processes[i].pid = I + 1;

findAverageTime(processes, n);

return 0;

4.Write a program for priority scheduling non preemptive algorithm

Code :
#include <stdio.h>

#define N 10

Typedef struct

Int pid;

Int arrival;

Int burst;

Int priority;

Int wait;

Int turnaround;

Int completed;

} Process;

Void calculateTimes(Process proc[], int n)

Int currentTime = 0, totalCompleted = 0;

While (totalCompleted < n)

Int index = -1;

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

If (proc[i].arrival <= currentTime && !proc[i].completed)

If (index == -1 || proc[i].priority > proc[index].priority)

Index = I;
}

If (index != -1)

If (currentTime < proc[index].arrival)

currentTime = proc[index].arrival;

Proc[index].wait = currentTime – proc[index].arrival;

currentTime += proc[index].burst;

proc[index].turnaround = proc[index].wait + proc[index].burst;

proc[index].completed = 1;

totalCompleted++;

Else

currentTime++;

Void printResult(Process proc[], int n)

Int totalWait = 0, totalTurnaround = 0;

Printf(“Process ID | Arrival Time | Priority | Burst Time | Waiting Time | Turnaround


Time\n”);
For (int I = 0; I < n; i++)

totalWait += proc[i].wait;

totalTurnaround += proc[i].turnaround;

printf(“%9d %14d %10d %12d %14d %17d\n”, proc[i].pid, proc[i].arrival,


proc[i].priority, proc[i].burst, proc[i].wait, proc[i].turnaround);

Float avgWait = (float)totalWait / n;

Float avgTurnaround = (float)totalTurnaround / n;

Printf(“\nAverage waiting time: %.2f\n”, avgWait);

Printf(“Average turnaround time: %.2f\n”, avgTurnaround);

Int main()

Process process[N];

Int n;

Printf(“Enter number of processes: “);

Scanf(“%d”, &n);

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

Process[i].pid = I + 1;

Printf(“Enter arrival time of process %d: “, I + 1);

Scanf(“%d”, &process[i].arrival);

Printf(“Enter burst time of process %d: “, I + 1);

Scanf(“%d”, &process[i].burst);

Printf(“Enter priority of process %d: “, I + 1);


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

Process[i].completed = 0;

calculateTimes(process, n);

printResult(process, n);

return 0;

5.Write a program for priority scheduling preemptive algorithm

Code :

#include <stdio.h>

#include <limits.h>

#define MAX_PROCESSES 100

Typedef struct

Int id;

Int burst_time;
Int remaining_time;

Int priority;

Int arrival_time;

Int completion_time;

Int waiting_time;

Int turnaround_time;

} Process;

Void priority_preemptive_scheduling(Process processes[], int n)

Int time = 0, completed = 0;

Int min_priority_index;

Int min_priority;

While (completed != n)

Min_priority_index = -1;

Min_priority = INT_MAX;

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

If (processes[i].arrival_time <= time && processes[i].remaining_time > 0 &&


processes[i].priority < min_priority)

Min_priority = processes[i].priority;

Min_priority_index = I;

If (min_priority_index != -1)
{

Processes[min_priority_index].remaining_time--;

Time++;

If (processes[min_priority_index].remaining_time == 0)

Completed++;

Processes[min_priority_index].completion_time = time;

Processes[min_priority_index].turnaround_time =
processes[min_priority_index].completion_time –
processes[min_priority_index].arrival_time;

Processes[min_priority_index].waiting_time =
processes[min_priority_index].turnaround_time –
processes[min_priority_index].burst_time;

Else

Time++;

Void print_results(Process processes[], int n)

Int total_waiting_time = 0, total_turnaround_time = 0;

Printf(“\nProcess\tBurst Time\tPriority\tArrival Time\tCompletion Time\tTurnaround


Time\tWaiting Time\n”);

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


{

Total_waiting_time += processes[i].waiting_time;

Total_turnaround_time += processes[i].turnaround_time;

Printf(“%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n”,

Processes[i].id,

Processes[i].burst_time,

Processes[i].priority,

Processes[i].arrival_time,

Processes[i].completion_time,

Processes[i].turnaround_time,

Processes[i].waiting_time

);

Float avg_waiting_time = (float)total_waiting_time / n;

Float avg_turnaround_time = (float)total_turnaround_time / n;

Printf(“\nAverage waiting time: %.2f\n”, avg_waiting_time);

Printf(“Average turnaround time: %.2f\n”, avg_turnaround_time);

Int main()

Process processes[MAX_PROCESSES];

Int n;

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

Scanf(“%d”, &n);

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

{
Processes[i].id = I + 1;

Printf(“Enter burst time for process %d: “, I + 1);

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

Processes[i].remaining_time = processes[i].burst_time;

Printf(“Enter priority for process %d: “, I + 1);

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

Printf(“Enter arrival time for process %d: “, I + 1);

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

Priority_preemptive_scheduling(processes, n);

Print_results(processes, n);

Return 0;

6.Write a program for round robin scheduling algorithm

Code :
#include <stdio.h>

#define MAX_PROCESSES 100

Typedef struct

Int id;

Int burst_time;

Int remaining_time;

Int arrival_time;

Int completion_time;

Int waiting_time;

Int turnaround_time;

} Process;

Void round_robin_scheduling(Process processes[], int n, int time_quantum)

Int time = 0, completed = 0;

Int queue[MAX_PROCESSES], front = 0, rear = 0;

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

Processes[i].remaining_time = processes[i].burst_time;

Queue[rear++] = I;

While (completed < n)

Int index = queue[front++ % n];

If (processes[index].remaining_time > 0)

{
If (processes[index].remaining_time > time_quantum)

Time += time_quantum;

Processes[index].remaining_time -= time_quantum;

Else

Time += processes[index].remaining_time;

Processes[index].remaining_time = 0;

Processes[index].completion_time = time;

Processes[index].turnaround_time = processes[index].completion_time –
processes[index].arrival_time;

Processes[index].waiting_time = processes[index].turnaround_time –
processes[index].burst_time;

Completed++;

Queue[rear++ % n] = index;

Void print_results(Process processes[], int n)

Printf(“\nProcess\tBurst Time\tArrival Time\tCompletion Time\tTurnaround Time\tWaiting


Time\n”);

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

Printf(“%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n”,
Processes[i].id,

Processes[i].burst_time,

Processes[i].arrival_time,

Processes[i].completion_time,

Processes[i].turnaround_time,

Processes[i].waiting_time);

Int main()

Process processes[MAX_PROCESSES];

Int n, time_quantum;

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

Scanf(“%d”, &n);

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

Processes[i].id = I + 1;

Printf(“Enter burst time for process %d: “, I + 1);

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

Printf(“Enter arrival time for process %d: “, I + 1);

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

Printf(“Enter the time quantum: “);

Scanf(“%d”, &time_quantum);

Round_robin_scheduling(processes, n, time_quantum);

Print_results(processes, n);
Return 0;

You might also like