0% found this document useful (0 votes)
10 views51 pages

Os

Uploaded by

ayusha.bhandari6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views51 pages

Os

Uploaded by

ayusha.bhandari6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Lab 4

Write a C program to simulate producer and consumer problem using semaphores

The producer consumer problem is a synchronization problem. We have a buffer of fixed size. A producer
can produce an item and can place in the buffer. A consumer can pick items and can consume them. We
need to ensure that when a producer is placing an item in the buffer, then at the same time consumer
should not consume any item. In this problem, buffer is the critical section.

To solve this problem, we need two counting semaphores – Full and Empty. “Full” keeps track of number
of items in the buffer at any given time and “Empty” keeps track of number of unoccupied slots.

Semaphore : A semaphore S is an integer variable that can be accessed only through two standard
operations :

● wait() - The wait() operation reduces the value of semaphore by 1

● signal() - The signal() operation increases its value by 1.

wait(S){
while(S<=0); // busy waiting
S--;
}

signal(S){
S++;
}

Semaphores are of two types:

● Binary Semaphore – This is similar to mutex lock but not the same thing. It can have only two
values – 0 and 1. Its value is initialized to 1. It is used to implement the solution of critical section
problem with multiple processes.

● Counting Semaphore – Its value can range over an unrestricted domain. It is used to control
access to a resource that has multiple instances.

Mutual Exclusion: One or more than one resource are non-sharable (Only one process can use at a
time). A mutex provides mutual exclusion, either producer or consumer can have the key (mutex) and
proceed with their work. As long as the buffer is filled by producer, the consumer needs to wait, and vice
versa.

Initialization of semaphores

mutex = 1
Full = 0 // Initially, all slots are empty. Thus full slots are 0
Empty = n // All slots are empty initially

Solution for Producer


do{

//produce an item

wait(empty);
wait(mutex);

//place in buffer

signal(mutex);
signal(full);

}while(true)

When producer produces an item then the value of “empty” is reduced by 1 because one slot will be
filled now. The value of mutex is also reduced to prevent consumer to access the buffer. Now, the
producer has placed the item and thus the value of “full” is increased by 1. The value of mutex is also
increased by 1 beacuse the task of producer has been completed and consumer can access the buffer.

Solution for Consumer

do{

wait(full);
wait(mutex);

// remove item from buffer

signal(mutex);
signal(empty);

// consumes item

}while(true)

As the consumer is removing an item from buffer, therefore the value of “full” is reduced by 1 and the
value is mutex is also reduced so that the producer cannot access the buffer at this moment. Now, the
consumer has consumed the item, thus increasing the value of “empty” by 1. The value of mutex is also
increased so that producer can access the buffer now.

Program

#include<stdio.h>
#include<stdlib.h>

int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}

return 0;
}

int wait(int s)
{
return (--s);
}

int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}

void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}

Output
Lab 5

PROGRAM TO SIMULATE AND FIND AVERAGE TAT & WAITING TIME FOR PREEMPTIVE & NON
PREEMPTIVE SCHEDULING ALGORITHMS: FCFS, SJF, PRIORITY & ROUND-ROBIN”

Round Robin Scheduling Program in C


et us learn how to implement the Round Robin scheduling program in C language with its
explanation, output, advantages, disadvantages and much more.

What is Round Robin Scheduling Algorithm?

Before beginning with the C program implementation, let us first understand the conceptual
theory of the Round Robin Scheduling Algorithm.

▪ The Round robin algorithm is a pre-emptive process scheduling algorithm. Here,


every job request in the queue is associated with a fixed execution time
called quantum.
▪ A pre-emptive process enables the job scheduler to pause a process under execution
and move to the next process in the job queue.
▪ The job scheduler saves the current state of the job and moves to another job in the
queue as soon as a particular process/job is executed for a given time quantum.
▪ The context switch saves the current state of the process. This algorithm is
beneficial in terms of its response time.
▪ In the round robin algorithm, every process gets an equal time of execution which is
defined by the quantum time.
▪ Therefore, no process will be able to hold the CPU for a longer time period.
▪ The round-robin job scheduling algorithm is, therefore, used in a multi-user,
time-sharing or multi-tasking operating systems.
▪ As a result, it is probably the best scheduling algorithm in operating systems for
distributed terminal response time.
▪ Furthermore, the efficiency of this algorithm is totally dependent on the size of the
time quantum and the number of context switches that occur.

Advantages

▪ The decision making overhead is very low.


▪ Equal priority is given to each job within the queue, unlike other scheduling
algorithms.
▪ Hence, starvation does not occur so frequently.
Disadvantages

▪ The throughput in the round-robin algorithm is highly dependent on the quantum


length.
▪ If the quantum is less, then the process switching occurs frequently which decreases
the efficiency.
▪ If the quantum is more, the system may become unresponsive.

The RR scheduling algorithm is quite simple to implement, and the overhead in decision-making is
very low. This algorithm is good evenly distributes the terminal response time.

Note: This round robin scheduling program in C language using arrival time and an Array data
structure is compiled with GNU GCC compiler using Linux terminal on Linux Ubuntu operating
system.

Round Robin Scheduling Program in C

#include<stdio.h>

int main()
{
int i, limit, total = 0, x, counter = 0, time_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
float average_wait_time, average_turnaround_time;
printf("\nEnter Total Number of Processes:\t");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)
{
printf("\nEnter Details of Process[%d]\n", i + 1);
printf("Arrival Time:\t");
scanf("%d", &arrival_time[i]);
printf("Burst Time:\t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
printf("\nEnter Time Quantum:\t");
scanf("%d", &time_quantum);
printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
for(total = 0, i = 0; x != 0;)
{
if(temp[i] <= time_quantum && temp[i] > 0)
{
total = total + temp[i];
temp[i] = 0;
counter = 1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - time_quantum;
total = total + time_quantum;
}
if(temp[i] == 0 && counter == 1)
{
x--;
printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d", i + 1, burst_time[i], total -
arrival_time[i], total - arrival_time[i] - burst_time[i]);
wait_time = wait_time + total - arrival_time[i] - burst_time[i];
turnaround_time = turnaround_time + total - arrival_time[i];
counter = 0;
}
if(i == limit - 1)
{
i = 0;
}
else if(arrival_time[i + 1] <= total)
{
i++;
}
else
{
i = 0;
}
}
average_wait_time = wait_time * 1.0 / limit;
average_turnaround_time = turnaround_time * 1.0 / limit;
printf("\n\nAverage Waiting Time:\t%f", average_wait_time);
printf("\nAvg Turnaround Time:\t%f\n", average_turnaround_time);
return 0;
}
Output

Preemptive Priority Scheduling Algorithm C


Program

Let us learn how to implement preemptive priority scheduling algorithm in C programming with its
explanation, output, advantages, disadvantages and much more.

What is Preemptive Priority Scheduling Algorithm?

The preemptive priority scheduling algorithm is a popular operating system process management
and job scheduling algorithm.

Every job that enters the job queue is assigned a priority based on which its execution takes place.
As simple it sounds, the processes with a higher priority will be executed first and then the
processes with the lower priorities will be executed.

If there are multiple processes in the queue with the same priority, then such jobs are executed in
the order of their arrival often called as first come first served.
In this preemptive implementation of priority scheduling program in C, we consider the arrival
time of the processes.
Since this is a preemptive job scheduling algorithm, the CPU can leave the process midway. The
current state of the process will be saved by the context switch.

The system can then search for another process with a higher priority in the ready queue or
waiting queue and start its execution.

Once the CPU comes back to the previous incomplete process, the job is resumed from where it
was earlier paused.

Advantages

▪ Preemptive priority scheduling is much more efficient as compared to the


non-preemptive version.
▪ This priority job scheduling algorithm is quite simple to implement.
▪ The aging technique is implemented to reduce the starvation of lower priority
processes.
▪ The average turnaround time and waiting time is efficient.

Disadvantages

▪ Indefinite blockage of the lower priority jobs.


▪ For a system failure occurs, the unfinished lower priority jobs are removed from the
system and cannot be recovered.
Note: This implementation of preemptive priority scheduling program in C with arrival time is
compiled with GNU GCC compiler using Linux terminal on Linux Ubuntu operating system

Preemptive Priority Scheduling Algorithm in C Programming

1 #include<stdio.h>

3 struct process

4 {

5 char process_name;

6 int arrival_time, burst_time, ct, waiting_time, turnaround_time, priority;

7 int status;

8 }process_queue[10];

9
10 int limit;

11

12 void Arrival_Time_Sorting()

13 {

14 struct process temp;

15 int i, j;

16 for(i = 0; i < limit - 1; i++)

17 {

18 for(j = i + 1; j < limit; j++)

19 {

20 if(process_queue[i].arrival_time > process_queue[j].arrival_time)

21 {

22 temp = process_queue[i];

23 process_queue[i] = process_queue[j];

24 process_queue[j] = temp;

25 }

26 }

27 }

28 }

29

30 void main()

31 {

32 int i, time = 0, burst_time = 0, largest;

33 char c;

34 float wait_time = 0, turnaround_time = 0, average_waiting_time, average_turnaround_time;

35 printf("\nEnter Total Number of Processes:\t");

36 scanf("%d", &limit);

37 for(i = 0, c = 'A'; i < limit; i++, c++)

38 {

39 process_queue[i].process_name = c;

40 printf("\nEnter Details For Process[%C]:\n", process_queue[i].process_name);

41 printf("Enter Arrival Time:\t");


42 scanf("%d", &process_queue[i].arrival_time );

43 printf("Enter Burst Time:\t");

44 scanf("%d", &process_queue[i].burst_time);

45 printf("Enter Priority:\t");

46 scanf("%d", &process_queue[i].priority);

47 process_queue[i].status = 0;

48 burst_time = burst_time + process_queue[i].burst_time;

49 }

50 Arrival_Time_Sorting();

51 process_queue[9].priority = -9999;

52 printf("\nProcess Name\tArrival Time\tBurst Time\tPriority\tWaiting Time");

53 for(time = process_queue[0].arrival_time; time < burst_time;)

54 {

55 largest = 9;

56 for(i = 0; i < limit; i++)

57 {

58 if(process_queue[i].arrival_time <= time && process_queue[i].status != 1 &&


process_queue[i].priority > process_queue[largest].priority)
59
{
60
largest = i;
61
}
62
}
63
time = time + process_queue[largest].burst_time;
64
process_queue[largest].ct = time;
65
process_queue[largest].waiting_time = process_queue[largest].ct -
66 process_queue[largest].arrival_time - process_queue[largest].burst_time;

67 process_queue[largest].turnaround_time = process_queue[largest].ct -
68 process_queue[largest].arrival_time;

69 process_queue[largest].status = 1;

70 wait_time = wait_time + process_queue[largest].waiting_time;

71 turnaround_time = turnaround_time + process_queue[largest].turnaround_time;

72 printf("\n%c\t\t%d\t\t%d\t\t%d\t\t%d", process_queue[largest].process_name,
process_queue[largest].arrival_time, process_queue[largest].burst_time, process_queue[largest].priority,
73 process_queue[largest].waiting_time);
74 }

75 average_waiting_time = wait_time / limit;

76 average_turnaround_time = turnaround_time / limit;

printf("\n\nAverage waiting time:\t%f\n", average_waiting_time);

printf("Average Turnaround Time:\t%f\n", average_turnaround_time);

Output

Shortest Job First Algorithm C Program

Let us learn how to implement the shortest job first scheduling algorithm in C programming with
its explanation, output, advantages, disadvantages and much more.
What is Shortest Job First Algorithm?

The shortest job first scheduling algorithm is a very popular job scheduling algorithm in operating
systems. This algorithm is designed to overcome the shortcomings of the FCFS algorithm.

The SJF algorithm is also popularly known by the following names:

▪ Shortest Job Next algorithm


▪ Shortest Remaining Time First algorithm
▪ Shortest Process Next algorithm

Initially, the job queue contains multiple processes for execution. According to the SJF algorithm,
the processes are compared with each other and the process that has the shortest burst time
(execution time) gets executed first.

The remaining processes are also executed in the order of their burst times. If there are two or
more processes having the same burst time, the processes are executed in the order of their
arrival.

This is a non-preemptive algorithm which means that the CPU cannot leave a process in execution
and start execution of another process.

Once the CPU starts execution of a job, it has to complete it successfully and then it can move to
any other process in the job queue.

Note: This SJF non preemptive scheduling program in c with output does not consider arrival
time of the processes entering the job queue.

Advantages

▪ The response time of the processes is better.


▪ The throughput time is much better as the time taken for execution is much less.
▪ The overall performance of the system is efficient.

Disadvantages

▪ The execution time of all the processes in the job queue must be known in advance
to apply the algorithm efficiently to all the jobs.
▪ The processes with larger execution time will have a higher waiting time, and this
may lead to starvation.
Note: This shortest job first scheduling program in c language is compiled with GNU GCC compiler
using Linux terminal on Linux Ubuntu operating system.
C Program For Non Preemptive Shortest Job First Scheduling Algorithm

1 #include<stdio.h>

3 int main()

4 {

5 int temp, i, j, limit, sum = 0, position;

6 float average_wait_time, average_turnaround_time;

7 int burst_time[20], process[20], waiting_time[20], turnaround_time[20];

8 printf("\nEnter Total Number of Processes:\t");

9 scanf("%d", &limit);

10 for(i = 0; i < limit; i++)

11 {

12 printf("Enter Burst Time For Process[%d]:\t", i + 1);

13 scanf("%d", &burst_time[i]);

14 process[i] = i + 1;

15 }

16 for(i = 0; i < limit; i++)

17 {

18 position = i;

19 for(j = i + 1; j < limit; j++)

20 {

21 if(burst_time[j] < burst_time[position])

22 {

23 position = j;

24 }

25 }

26 temp = burst_time[i];

27 burst_time[i] = burst_time[position];

28 burst_time[position] = temp;

29 temp = process[i];

30 process[i] = process[position];

31 process[position] = temp;
32 }

33 waiting_time[0] = 0;

34 for(i = 1; i < limit; i++)

35 {

36 waiting_time[i] = 0;

37 for(j = 0; j < i; j++)

38 {

39 waiting_time[i] = waiting_time[i] + burst_time[j];

40 }

41 sum = sum + waiting_time[i];

42 }

43 average_wait_time = (float)sum / limit;

44 sum = 0;

45 printf("\nProcess ID\t\tBurst Time\t Waiting Time\t Turnaround Time\n");

46 for(i = 0; i < limit; i++)

47 {

48 turnaround_time[i] = burst_time[i] + waiting_time[i];

49 sum = sum + turnaround_time[i];

50 printf("\nProcess[%d]\t\t%d\t\t %d\t\t %d\n", process[i], burst_time[i], waiting_time[i],


turnaround_time[i]);
51
}
52
average_turnaround_time = (float)sum / limit;
53
printf("\nAverage Waiting Time:\t%f\n", average_wait_time);
54
printf("\nAverage Turnaround Time:\t%f\n", average_turnaround_time);
55
return 0;
56
}
Output

Preemptive Shortest Job First Algorithm C


Program

Let us learn how to implement the preemptive shortest job first scheduling algorithm in C
programming with its explanation, output, advantages, disadvantages and much more.

What is Preemptive Shortest Job Scheduling Algorithm?

According to the SJF algorithm, the jobs in the queue are compared with each other and the one
with shortest burst time gets executed first.

The remaining processes are also executed in the order of their burst times. However, there may
be scenarios where one or more processes have same execution time.

In such cases, the jobs are based on first come first serve basis or in other words, FIFO approach
is used.
This is a preemptive algorithm which means that the CPU can leave a process while under
execution, and can move to the next process in the queue.

Meanwhile, the current state of the process is saved by context switch and another job can be
processed in the meantime.
Once the CPU scheduler comes back to the previous job which was incomplete, resumes it from
where it was stopped.

The shortest job first algorithm is also popularly known by the following names:

▪ Shortest Remaining Time First algorithm


▪ Shortest Job Next algorithm
▪ Shortest Process Next algorithm
Note: This SJF preemptive scheduling program in c with output considers the arrival time of the
processes entering the job queue.

Advantages

▪ The response time is much better as compared to FCFS algorithm.


▪ Minimum average waiting time is achieved.
▪ The throughput time is good as the burst time of the processes is less.
▪ Optimum turnaround time.

Disadvantages
▪ The execution time of all the jobs in the queue must be known in advance which is
not possible in all the scenarios.
▪ The processes with larger burst time will have a high waiting time, and this may lead
to starvation.
Note: This preemptive shortest job first scheduling program in c language is compiled with GNU
GCC compiler using Linux terminal on Linux Ubuntu operating system.

C Program For Preemptive Shortest Job Scheduling Algorithm

1 #include <stdio.h>

3 int main()

4 {

5 int arrival_time[10], burst_time[10], temp[10];

6 int i, smallest, count = 0, time, limit;

7 double wait_time = 0, turnaround_time = 0, end;

8 float average_waiting_time, average_turnaround_time;

9 printf("\nEnter the Total Number of Processes:\t");

10 scanf("%d", &limit);

11 printf("\nEnter Details of %d Processes\n", limit);


12 for(i = 0; i < limit; i++)

13 {

14 printf("\nEnter Arrival Time:\t");

15 scanf("%d", &arrival_time[i]);

16 printf("Enter Burst Time:\t");

17 scanf("%d", &burst_time[i]);

18 temp[i] = burst_time[i];

19 }

20 burst_time[9] = 9999;

21 for(time = 0; count != limit; time++)

22 {

23 smallest = 9;

24 for(i = 0; i < limit; i++)

25 {

26 if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] > 0)

27 {

28 smallest = i;

29 }

30 }

31 burst_time[smallest]--;

32 if(burst_time[smallest] == 0)

33 {

34 count++;

35 end = time + 1;

36 wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];

37 turnaround_time = turnaround_time + end - arrival_time[smallest];

38 }

39 }

40 average_waiting_time = wait_time / limit;

41 average_turnaround_time = turnaround_time / limit;

42 printf("\n\nAverage Waiting Time:\t%lf\n", average_waiting_time);

43 printf("Average Turnaround Time:\t%lf\n", average_turnaround_time);


44 return 0;

45 }

Priority Scheduling Algorithm C Program

Let us learn how to implement priority scheduling algorithm in C programming with its
explanation, output, advantages, disadvantages and much more.

What is Non-Preemptive Priority Scheduling Algorithm?

The priority scheduling algorithm is one of the most common algorithms for scheduling jobs in
batch systems.

Every process is assigned a number which denotes the priority, and based on this priority the
processes are executed. Therefore, the process having the highest priority (1) is executed first and
then the priority 2, 3 and so on.
There can be some scenarios where more two or more processes may have the same priority. In
this case, the processes are executed based on First In First Out order or in other words, First
Come First Serve.
We do not consider the arrival time of the jobs in this non-preemptive priority scheduling
algorithm. This means that unless a job gets completely executed, the CPU won’t leave the current
job before it completes its execution.

Only once the process gets out of the job queue after successful execution, the CPU is allowed to
process another job from the queue.

Advantages

▪ This algorithm is very simple to implement.


▪ The aging technique is implemented to reduce the starvation of lower priority
processes.

Disadvantages

▪ Starvation or indefinite blockage of the lower priority processes.


▪ Since this is a non-preemptive implementation, the waiting time is comparatively
higher.
▪ The average turnaround time is higher as compared to the preemptive priority
scheduling algorithm.
▪ If a system failure occurs, all the unfinished lower priority jobs get vanished from the
system.
Note: This implementation of non-preemptive priority scheduling program in C without arrival
time is compiled with GNU GCC compiler using Linux terminal on Linux Ubuntu operating system.

C Program To Implement Non-Preemptive Priority Scheduling Algorithm

1 #include<stdio.h>

3 int main()

4 {

5 int burst_time[20], process[20], waiting_time[20], turnaround_time[20], priority[20];

6 int i, j, limit, sum = 0, position, temp;

7 float average_wait_time, average_turnaround_time;

8 printf("Enter Total Number of Processes:\t");

9 scanf("%d", &limit);

10 printf("\nEnter Burst Time and Priority For %d Processes\n", limit);

11 for(i = 0; i < limit; i++)


12 {

13 printf("\nProcess[%d]\n", i + 1);

14 printf("Process Burst Time:\t");

15 scanf("%d", &burst_time[i]);

16 printf("Process Priority:\t");

17 scanf("%d", &priority[i]);

18 process[i] = i + 1;

19 }

20 for(i = 0; i < limit; i++)

21 {

22 position = i;

23 for(j = i + 1; j < limit; j++)

24 {

25 if(priority[j] < priority[position])

26 {

27 position = j;

28 }

29 }

30 temp = priority[i];

31 priority[i] = priority[position];

32 priority[position] = temp;

33 temp = burst_time[i];

34 burst_time[i] = burst_time[position];

35 burst_time[position] = temp;

36 temp = process[i];

37 process[i] = process[position];

38 process[position] = temp;

39 }

40 waiting_time[0] = 0;

41 for(i = 1; i < limit; i++)

42 {

43 waiting_time[i] = 0;
44 for(j = 0; j < i; j++)

45 {

46 waiting_time[i] = waiting_time[i] + burst_time[j];

47 }

48 sum = sum + waiting_time[i];

49 }

50 average_wait_time = sum / limit;

51 sum = 0;

52 printf("\nProcess ID\t\tBurst Time\t Waiting Time\t Turnaround Time\n");

53 for(i = 0; i < limit; i++)

54 {

55 turnaround_time[i] = burst_time[i] + waiting_time[i];

56 sum = sum + turnaround_time[i];

57 printf("\nProcess[%d]\t\t%d\t\t %d\t\t %d\n", process[i], burst_time[i], waiting_time[i],


turnaround_time[i]);
58
}
59
average_turnaround_time = sum / limit;
60
printf("\nAverage Waiting Time:\t%f", average_wait_time);
61
printf("\nAverage Turnaround Time:\t%f\n", average_turnaround_time);
62
return 0;
63
}

Output
Preemptive Priority Scheduling Algorithm C
Program

Let us learn how to implement preemptive priority scheduling algorithm in C programming with its
explanation, output, advantages, disadvantages and much more.

What is Preemptive Priority Scheduling Algorithm?

The preemptive priority scheduling algorithm is a popular operating system process management
and job scheduling algorithm.

Every job that enters the job queue is assigned a priority based on which its execution takes place.
As simple it sounds, the processes with a higher priority will be executed first and then the
processes with the lower priorities will be executed.
If there are multiple processes in the queue with the same priority, then such jobs are executed in
the order of their arrival often called as first come first served.
In this preemptive implementation of priority scheduling program in C, we consider the arrival
time of the processes.
Since this is a preemptive job scheduling algorithm, the CPU can leave the process midway. The
current state of the process will be saved by the context switch.

The system can then search for another process with a higher priority in the ready queue or
waiting queue and start its execution.

Once the CPU comes back to the previous incomplete process, the job is resumed from where it
was earlier paused.

Advantages

▪ Preemptive priority scheduling is much more efficient as compared to the


non-preemptive version.
▪ This priority job scheduling algorithm is quite simple to implement.
▪ The aging technique is implemented to reduce the starvation of lower priority
processes.
▪ The average turnaround time and waiting time is efficient.

Disadvantages

▪ Indefinite blockage of the lower priority jobs.


▪ For a system failure occurs, the unfinished lower priority jobs are removed from the
system and cannot be recovered.
Note: This implementation of preemptive priority scheduling program in C with arrival time is
compiled with GNU GCC compiler using Linux terminal on Linux Ubuntu operating system.

Preemptive Priority Scheduling Algorithm in C Programming

1 #include<stdio.h>

3 struct process

4 {

5 char process_name;

6 int arrival_time, burst_time, ct, waiting_time, turnaround_time, priority;

7 int status;

8 }process_queue[10];

10 int limit;
11

12 void Arrival_Time_Sorting()

13 {

14 struct process temp;

15 int i, j;

16 for(i = 0; i < limit - 1; i++)

17 {

18 for(j = i + 1; j < limit; j++)

19 {

20 if(process_queue[i].arrival_time > process_queue[j].arrival_time)

21 {

22 temp = process_queue[i];

23 process_queue[i] = process_queue[j];

24 process_queue[j] = temp;

25 }

26 }

27 }

28 }

29

30 void main()

31 {

32 int i, time = 0, burst_time = 0, largest;

33 char c;

34 float wait_time = 0, turnaround_time = 0, average_waiting_time, average_turnaround_time;

35 printf("\nEnter Total Number of Processes:\t");

36 scanf("%d", &limit);

37 for(i = 0, c = 'A'; i < limit; i++, c++)

38 {

39 process_queue[i].process_name = c;

40 printf("\nEnter Details For Process[%C]:\n", process_queue[i].process_name);

41 printf("Enter Arrival Time:\t");

42 scanf("%d", &process_queue[i].arrival_time );
43 printf("Enter Burst Time:\t");

44 scanf("%d", &process_queue[i].burst_time);

45 printf("Enter Priority:\t");

46 scanf("%d", &process_queue[i].priority);

47 process_queue[i].status = 0;

48 burst_time = burst_time + process_queue[i].burst_time;

49 }

50 Arrival_Time_Sorting();

51 process_queue[9].priority = -9999;

52 printf("\nProcess Name\tArrival Time\tBurst Time\tPriority\tWaiting Time");

53 for(time = process_queue[0].arrival_time; time < burst_time;)

54 {

55 largest = 9;

56 for(i = 0; i < limit; i++)

57 {

58 if(process_queue[i].arrival_time <= time && process_queue[i].status != 1 &&


process_queue[i].priority > process_queue[largest].priority)
59
{
60
largest = i;
61
}
62
}
63
time = time + process_queue[largest].burst_time;
64
process_queue[largest].ct = time;
65
process_queue[largest].waiting_time = process_queue[largest].ct -
66 process_queue[largest].arrival_time - process_queue[largest].burst_time;

67 process_queue[largest].turnaround_time = process_queue[largest].ct -
68 process_queue[largest].arrival_time;

69 process_queue[largest].status = 1;

70 wait_time = wait_time + process_queue[largest].waiting_time;

71 turnaround_time = turnaround_time + process_queue[largest].turnaround_time;

72 printf("\n%c\t\t%d\t\t%d\t\t%d\t\t%d", process_queue[largest].process_name,
process_queue[largest].arrival_time, process_queue[largest].burst_time, process_queue[largest].priority,
73 process_queue[largest].waiting_time);

74 }
75 average_waiting_time = wait_time / limit;

76 average_turnaround_time = turnaround_time / limit;

printf("\n\nAverage waiting time:\t%f\n", average_waiting_time);

printf("Average Turnaround Time:\t%f\n", average_turnaround_time);

Output
Lab 6

PROGRAM TO SIMULATE CONTIGIOUS MEMORY ALLOCATION TECHNIQUES: WORST FIT, BEST FIT, & FIRST
FIT

Write a C program to simulate the FIRST-FIT contiguous memory allocation technique

FIRST-FIT:

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp; bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i];
getch();
}

INPUT
Enter the number of blocks: 3 Enter the number of files: 2
Enter the size of the blocks:- Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the files:- File 1: 1
File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
1 1 1 5 4
2 4 3 7 3

2.Write a C program to simulate the BEST FIT contiguous memory allocation


technique

BEST-FIT
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme – Best Fit");
printf("\nEnter the number of blocks:"); scanf("%d",&nb);
printf("Enter the number of files:"); scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);

printf("Enter the size of the files :-\n");


for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
INPUT
Enter the number of blocks: 3 Enter the number of files: 2

Enter the size of the blocks:- Block 1: 5


Block 2: 2
Block 3: 7

Enter the size of the files:- File 1: 1


File 2: 4

OUTPUT
File No File Size Block No Block Size Fragment
1 1 2 2 1
2 4 1 5 1

3.Write a C program to simulate the WORST-FIT contiguous memory allocation technique

WORST-FIT
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();

printf("\n\tMemory Management Scheme - Worst Fit");


printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{

for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;

}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}

ff[i]=j;
highest=temp;
}

printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

INPUT
Enter the number of blocks: 3 Enter the number of files: 2

Enter the size of the blocks:- Block 1: 5


Block 2: 2
Block 3: 7

Enter the size of the files:- File 1: 1


File 2: 4

OUTPUT
File No File Size Block No Block Size Fragment
1 1 3 7 6
2 4 1 5 1

Lab 7

PROGRAM TO SIMULATE PAGE REPLACEMENT ALGORITHMS: FIFO, LRU, & LFU

Write a C program to simulate LRU page replacement algorithm

LRU PAGE REPLACEMENT ALGORITHM


#include<stdio.h>
#include<conio.h>
main()
{
int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0, next=1;
clrscr();
printf("Enter the length of reference string -- ");
scanf("%d",&n);
printf("Enter the reference string -- ");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter the number of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)

{
count[i]=0;
m[i]=-1;
}
printf("\nThe Page Replacement process is -- \n");
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}

}
if(flag[i]==0)
{

if(i<f)
{ m[i]=rs[i];
count[i]=next;
next++;
}
else
{ min=0;
for(j=1;j<f;j++)
if(count[min] > count[j])
min=j;

m[min]=rs[i];
count[min]=next;
next++;
}
pf++;
}

for(j=0;j<f;j++)
printf("%d\t", m[j]);
if(flag[i]==0)
printf("PF No. -- %d" , pf);
printf("\n");
}
printf("\nThe number of page faults using LRU are %d",pf);
getch();
}

INPUT
Enter the length of reference string -- 20
Enter the reference string -- 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Enter the number of frames -- 3

OUTPUT
The Page Replacement process is --
7 -1 -1 PF No. – 1
7 0 -1 PF No. – 2
7 0 1 PF No. – 3
2 0 1 PF No. – 4
201
2 0 3 PF No. – 5
203
4 0 3 PF No. – 6
4 0 2 PF No. – 7
4 3 2 PF No. – 8
0 3 2 PF No. – 9
032
032
1 3 2 PF No. – 10
132
1 0 2 PF No. – 11
102
1 0 7 PF No. – 12
107
107
The number of page faults using LRU are 12

Write a C program to simulate LFU page replacement algorithm


LFU PAGE REPLACEMENT ALGORITHM
#include<stdio.h>
#include<conio.h>
main()
{
int rs[50], i, j, k, m, f, cntr[20], a[20], min, pf=0;
clrscr();
printf("\nEnter number of page references -- ");
scanf("%d",&m);
printf("\nEnter the reference string -- ");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0; a[i]=-1;
}
printf(“\nThe Page Replacement Process is – \n“);
for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;

if(j==f)
{ min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i]; cntr[min]=1;
pf++;
}
printf("\n");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)

}
printf(“\tPF No. %d”,pf);}
printf("\n\n Total number of page faults -- %d",pf);
getch();
}

INPUT
Enter number of page references -- 10
Enter the reference string -- 1 2 3 4 5 2 5 2 5 1 4 3
Enter the available no. of frames -- 3

OUTPUT
The Page Replacement Process is –

1 -1 -1 PF No. 1
1 2 -1 PF No. 2
1 2 3 PF No. 3
4 2 3 PF No. 4
5 2 3 PF No. 5
5 23
5 23
5 2 1 PF No. 6
5 2 4 PF No. 7
5 2 3 PF No. 8

Total number of page faults -- 8

Write a C program to simulate first page replacement algorithm

FIFO PAGE REPLACEMENT ALGORITHM


#include<stdio.h>
#include<conio.h>
main()
{
int i, j, k, f, pf=0, count=0, rs[25], m[10], n;
clrscr();
printf("\n Enter the length of reference string -- ");
scanf("%d",&n);
printf("\n Enter the reference string -- ");
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
printf("\n Enter no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;

printf("\n The Page Replacement Process is -- \n");


for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
if(m[k]==rs[i])
break;

}
if(k==f)
{
m[count++]=rs[i];
pf++;

for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\tPF No. %d",pf);
printf("\n");
if(count==f)
count=0;
}
printf("\n The number of Page Faults using FIFO are %d",pf);
getch();

INPUT
Enter the length of reference string – 20
Enter the reference string -- 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Enter no. of frames -- 3
OUTPUT
The Page Replacement Process is –
7 -1 -1 PF No. 1
7 0 -1 PF No. 2
7 0 1 PF No. 3
2 0 1 PF No. 4
201
2 3 1 PF No. 5
2 3 0 PF No. 6
4 3 0 PF No. 7
4 2 0 PF No. 8
4 2 3 PF No. 9
0 2 3 PF No. 10
023
023
0 1 3 PF No. 11
0 1 2 PF No. 12
012
012
7 1 2 PF No. 13
7 0 2 PF No. 14
7 0 1 PF No. 15
The number of Page Faults using FIFO are 15

LAB 8

“PROGRAM TO SIMULATE DISK SCHEDULING ALGORITHMS: FCFS, SCAN, & C-SCAN

First Come First Serve Algorithm C Program


Let us learn how to implement first come first serve algorithm in C programming with its
explanation, output, advantages, disadvantages and much more.

What is First Come First Serve Disk Scheduling Algorithm?

The first come first serve algorithm is commonly abbreviated as FCFS algorithm. It primarily works
on the First In First Out (FIFO) principle.
The incoming requests or jobs in the system queue are executed based on first come first
served basis. This is a non-preemptive scheduling algorithm.

Therefore, once the CPU is allocated to a particular job, the job keeps on executing till it gets
completed. The CPU cannot leave the current job before it gets completed. So, the CPU cannot
move to another job in the queue.

The FCFS algorithm is usually represented using Gantt’s Chart on paper. However, the FCFS
scheduling algorithm is not so efficient when it comes to performance optimization.

It is not optimized for time-sharing systems. The average waiting time for the first come first serve
scheduling algorithm is highly dependent on the burst time of the jobs.

Advantages

▪ Simple and easy to implement


▪ Every process/job gets executed completely
▪ Lower possibilities of starvation

Disadvantages

▪ Poor performance due to high average waiting time


▪ There is no option for pre-emption of a job.
▪ Higher average turnaround time
▪ In-efficient for time-sharing systems
Note: This FCFS Algorithm C program is compiled with GNU GCC compiler using Linux terminal on
Linux Ubuntu operating system.

First Come First Serve CPU Scheduling Algorithm C Program

1 #include<stdio.h>

3 int main()

4 {

5 float burst_time[30], waiting_time[30], turnaround_time[30];

6 float average_waiting_time = 0.0, average_turnaround_time = 0.0;

7 int count, j, total_process;

8 printf("Enter The Number of Processes To Execute:\t");

9 scanf("%d", &total_process);

10 printf("\nEnter The Burst Time of Processes:\n\n");

11 for(count = 0; count < total_process; count++)


12 {

13 printf("Process [%d]:", count + 1);

14 scanf("%f", &burst_time[count]);

15 }

16 waiting_time[0] = 0;

17 for(count = 1; count < total_process; count++)

18 {

19 waiting_time[count] = 0;

20 for(j = 0; j < count; j++)

21 {

22 waiting_time[count] = waiting_time[count] + burst_time[j];

23 }

24 }

25 printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time\n");

26 for(count = 0; count < total_process; count++)

27 {

28 turnaround_time[count] = burst_time[count] + waiting_time[count];

29 average_waiting_time = average_waiting_time + waiting_time[count];

30 average_turnaround_time = average_turnaround_time + turnaround_time[count];

31 printf("\nProcess [%d]\t\t%.2f\t\t%.2f\t\t%.2f", count + 1, burst_time[count], waiting_time[count],


turnaround_time[count]);
32
}
33
printf("\n");
34
average_waiting_time = average_waiting_time / count;
35
average_turnaround_time = average_turnaround_time / count;
36
printf("\nAverage Waiting Time = %f", average_waiting_time);
37
printf("\nAverage Turnaround Time = %f", average_turnaround_time);
38
printf("\n");
39
return 0;
40
}
Output

SCAN Disk Scheduling Algorithm C Program

Let us learn how to implement SCAN disk scheduling algorithm in C programming with its
explanation, output, advantages, disadvantages and much more.

What Is Scan Disk Scheduling Algorithm?

The SCAN algorithm is a disk scheduling algorithm that helps in determining the motion of a disk’s
arm and head in executing the read and write requests.

In the SCAN scheduling algorithm, the disk arm moves in a single direction and executes all the
jobs coming in its way.

Once the disk arm reaches the end of that direction, it reverses the direction of scanning and
executes all of the jobs arriving in its path.
The SCAN scheduling algorithm is also commonly known as Elevator algorithm, and this
algorithm has a very high throughput. It also reduces the response time variance compared to the
SSTF algorithm.

The elevator algorithm is performed by moving the read/write head back-and-forth to the
innermost and outermost track.

There are multiple enhancements performed on the elevator algorithm which is depicted by the
following algorithms:

▪ Circular SCAN algorithm


▪ LOOK algorithm
▪ Circular LOOK algorithm

Advantages

▪ Lower response time variance


▪ High throughput
▪ Average response time

Disadvantages

▪ There are chances of starvation of processes in some scenarios.


Note: This SCAN disk program in C is compiled with GNU GCC compiler using CodeLite IDE on
Microsoft Windows 10 operating system.

C Program For SCAN Disk Scheduling Algorithm

#include<stdio.h>
#include<conio.h>

void scan_algorithm(int left[], int right[], int count, int limit)


{
int arr[20];
int x = count - 1, y = count + 1, c = 0, d = 0, j;
while(x > -1)
{
printf("\nX:\t%d", x);
printf("\nLeft[X]:\t%d", left[x]);
arr[d] = left[x];
x--;
d++;
}
arr[d] = 0;
while(y < limit + 1)
{
arr[y] = right[c];
c++;
y++;
}
printf("\nScanning Order:\n");
for(j = 0; j < limit + 1; j++)
{
printf("\n%d", arr[j]);
}
}

void division(int elements[], int limit, int disk_head)


{
int count = 0, p, q, m, x;
int left[20], right[20];
for(count = 0; count < limit; count++)
{
if(elements[count] > disk_head)
{
printf("\nBreak Position:\t%d\n", elements[count]);
break;
}
}
printf("\nValue:\t%d\n", count);
q = 1;
p = 0;
m = limit;
left[0] = elements[0];
printf("\nLeft:\t%d", left[0]);
while(q < count)
{
printf("\nElement[l] value:\t%d" , elements[q]);
left[q] = elements[q];
printf("\nLeft:\t%d", left[q]);
q++;
printf("\nl:\t%d", q);
}
x = count;
while(x < m)
{
right[p] = elements[x];
printf("\nRight:\t%d", right[p]);
printf("\nElement:\t%d", elements[x]);
p++;
x++;
}
scan_algorithm(left, right, count, limit);
}

void sorting(int elements[], int limit)


{
int location, count, j, temp, small;
for(count = 0; count < limit - 1; count++)
{
small = elements[count];
location = count;
for(j = count + 1; j < limit; j++)
{
if(small > elements[j])
{
small = elements[j];
location = j;
}
}
temp = elements[location];
elements[location] = elements[count];
elements[count] = temp;
}
}

int main()
{
int count, disk_head, elements[20], limit;
printf("Enter total number of locations:\t");
scanf("%d", &limit);
printf("\nEnter position of disk head:\t");
scanf("%d", &disk_head);
printf("\nEnter elements of disk head queue\n");
for(count = 0; count < limit; count++)
{
printf("Element[%d]:\t", count + 1);
scanf("%d", &elements[count]);
}
sorting(elements, limit);
division(elements, limit, disk_head);
getch();
return 0;
}
OUTPUT

C SCAN Disk Scheduling Algorithm C Program

Let us learn how to implement Circular SCAN or C SCAN disk scheduling algorithm in C
programming with its explanation, output, advantages, disadvantages and much more.
What is Circular Scan Disk Scheduling Algorithm?

The C SCAN algorithm is a disk scheduling algorithm that helps in determining the motion of a
disk’s arm and head in executing the read and write requests.

The circular SCAN algorithm is a variation or an advanced version of the SCAN algorithm. This
algorithm is also known as Circular Elevator Algorithm.

This algorithm ensures that the jobs are serviced only in a single direction. disk head from one end
of the disk to the other end of the disk.

While moving from one end to another end, the disk head serviced the requests along the way.

As soon as the disk head reached the other end, it immediately returns to the beginning of the
disk without servicing any requests while on the path to return to the starting position.

This large jump from one rear end of the disk to the other end is not considered as a head
movement as cylinders are treated as a circular list.

Again, the disk head starts moving in the same direction and services the remaining jobs in the job
queue.

As you can notice that while returning a certain amount of time is wasted but this results in better
overall performance for all the head positions.

Advantages

▪ Better optimization of Disk Input/Output


▪ Provides more uniform waiting time as compared to SCAN algorithm

Disadvantages

▪ There is a possibility of job starvation in some scenarios.


Note: This C SCAN disk scheduling program in C is compiled with GNU GCC compiler using
CodeLite IDE on Microsoft Windows 10 operating system.

C Program For C SCAN Disk Scheduling Algorithm

#include<stdio.h>
#include<stdlib.h>

int main()
{
int queue1[30], queue2[30], queue3[30];
int limit, disk_head, count = 0, j, seek_time = 0, range, diff;
int t1, t2 = 0, t3 = 0;
float avg_seek_time;
printf("Maximum Range of Disk:\t");
scanf("%d", &range);
printf("Initial Head Position:\t");
scanf("%d", &disk_head);
printf("Queue Request Size:\t");
scanf("%d", &limit);
printf("Disk Queue Element Positions:\n");
while(count < limit)
{
scanf("%d", &t1);
if(t1 >= disk_head)
{
queue1[t2] = t1;
t2++;
}
else
{
queue2[t3] = t1;
t3++;
}
count++;
}
count = 0;
while(count < t2 - 1)
{
j = count + 1;
while(j < t2)
{
if(queue1[count] > queue1[j])
{
t1 = queue1[count];
queue1[count] = queue1[j];
queue1[j] = t1;
}
j++;
}
count++;
}
count = 0;
while(count < t3 - 1)
{
j = count + 1;
while(j < t3)
{
if(queue2[count] > queue2[j])
{
t1 = queue2[count];
queue2[count] = queue2[j];
queue2[j] = t1;
}
j++;
}
count++;
}
count = 1;
j = 0;
while(j < t2)
{
queue3[count] = queue1[j];
queue3[count] = range;
queue3[count + 1] = 0;
count++;
j++;
}
count = t2 + 3;
j = 0;
while(j < t3)
{
queue3[count] = queue2[j];
queue3[0] = disk_head;
count++;
j++;
}
for(j = 0; j <= limit + 1; j++)
{
diff = abs(queue3[j + 1] - queue3[j]);
seek_time = seek_time + diff;
printf("\nDisk Head:\t%d -> %d [Seek Time: %d]\n", queue3[j], queue3[j + 1],
diff);
}
printf("\nTotal Seek Time:\t%d\n", seek_time);
avg_seek_time = seek_time / (float)limit;
printf("\nAverage Seek Time:\t%f\n", avg_seek_time);
return 0;
}

OutPut

You might also like