Os
Os
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(S){
while(S<=0); // busy waiting
S--;
}
signal(S){
S++;
}
● 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
//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.
do{
wait(full);
wait(mutex);
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”
Before beginning with the C program implementation, let us first understand the conceptual
theory of the Round Robin Scheduling Algorithm.
Advantages
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.
#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
Let us learn how to implement preemptive priority scheduling algorithm in C programming with its
explanation, output, advantages, disadvantages and much more.
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
Disadvantages
1 #include<stdio.h>
3 struct process
4 {
5 char process_name;
7 int status;
8 }process_queue[10];
9
10 int limit;
11
12 void Arrival_Time_Sorting()
13 {
15 int i, j;
17 {
19 {
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 {
33 char c;
36 scanf("%d", &limit);
38 {
39 process_queue[i].process_name = c;
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;
49 }
50 Arrival_Time_Sorting();
51 process_queue[9].priority = -9999;
54 {
55 largest = 9;
57 {
67 process_queue[largest].turnaround_time = process_queue[largest].ct -
68 process_queue[largest].arrival_time;
69 process_queue[largest].status = 1;
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 }
Output
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.
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
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 {
9 scanf("%d", &limit);
11 {
13 scanf("%d", &burst_time[i]);
14 process[i] = i + 1;
15 }
17 {
18 position = i;
20 {
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;
35 {
36 waiting_time[i] = 0;
38 {
40 }
42 }
44 sum = 0;
47 {
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.
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:
Advantages
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.
1 #include <stdio.h>
3 int main()
4 {
10 scanf("%d", &limit);
13 {
15 scanf("%d", &arrival_time[i]);
17 scanf("%d", &burst_time[i]);
18 temp[i] = burst_time[i];
19 }
20 burst_time[9] = 9999;
22 {
23 smallest = 9;
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;
38 }
39 }
45 }
Let us learn how to implement priority scheduling algorithm in C programming with its
explanation, output, advantages, disadvantages and much more.
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
Disadvantages
1 #include<stdio.h>
3 int main()
4 {
9 scanf("%d", &limit);
13 printf("\nProcess[%d]\n", i + 1);
15 scanf("%d", &burst_time[i]);
16 printf("Process Priority:\t");
17 scanf("%d", &priority[i]);
18 process[i] = i + 1;
19 }
21 {
22 position = i;
24 {
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;
42 {
43 waiting_time[i] = 0;
44 for(j = 0; j < i; j++)
45 {
47 }
49 }
51 sum = 0;
54 {
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.
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
Disadvantages
1 #include<stdio.h>
3 struct process
4 {
5 char process_name;
7 int status;
8 }process_queue[10];
10 int limit;
11
12 void Arrival_Time_Sorting()
13 {
15 int i, j;
17 {
19 {
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 {
33 char c;
36 scanf("%d", &limit);
38 {
39 process_queue[i].process_name = c;
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;
49 }
50 Arrival_Time_Sorting();
51 process_queue[9].priority = -9999;
54 {
55 largest = 9;
57 {
67 process_queue[largest].turnaround_time = process_queue[largest].ct -
68 process_queue[largest].arrival_time;
69 process_queue[largest].status = 1;
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;
Output
Lab 6
PROGRAM TO SIMULATE CONTIGIOUS MEMORY ALLOCATION TECHNIQUES: WORST FIT, BEST FIT, & FIRST
FIT
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
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]);
OUTPUT
File No File Size Block No Block Size Fragment
1 1 2 2 1
2 4 1 5 1
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();
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
OUTPUT
File No File Size Block No Block Size Fragment
1 1 3 7 6
2 4 1 5 1
Lab 7
{
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
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
}
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
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
Disadvantages
1 #include<stdio.h>
3 int main()
4 {
9 scanf("%d", &total_process);
14 scanf("%f", &burst_time[count]);
15 }
16 waiting_time[0] = 0;
18 {
19 waiting_time[count] = 0;
21 {
23 }
24 }
27 {
Let us learn how to implement SCAN disk scheduling algorithm in C programming with its
explanation, output, advantages, disadvantages and much more.
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:
Advantages
Disadvantages
#include<stdio.h>
#include<conio.h>
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
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
Disadvantages
#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