Lab Report of Operating System BCA 4th semester
1 Introduction of Process scheduling
Process
scheduling is a fundamental concept in operating systems that deals with the
efficient and orderly execution of multiple processes running concurrently on a
computer system. In a multitasking operating system, such as Windows, macOS, or
Linux, there are typically many processes competing for the CPU's (Central
Processing Unit) attention. The process scheduler is responsible for
determining which process should run next and allocating CPU time to each
process in a fair and efficient manner.
The primary goals of
process scheduling are:
- Fairness: Ensure that each process
gets a fair share of the CPU time, preventing any single process from
monopolizing the CPU and starving others.
- Efficiency: Maximize CPU
utilization and throughput by keeping the CPU busy with productive tasks
as much as possible.
- Responsiveness: Prioritize
interactive processes, like user interface applications, to respond
quickly to user inputs and maintain a smooth user experience.
- Throughput: Maximize the number of
processes completed per unit of time, achieving better overall system
performance.
1.1
Write algorithm of FCFS, SJS, RR
1.1.1
FCFS
Algorithm
The
algorithm for the FCFS Scheduling program in C is as follows:
- At first,
the process of execution algorithm starts
- Then, we
declare the size of an array
- Then we get
the number of processes that need to insert into the program
- Getting the
value
- Then the
first process starts with its initial position and the other processes are
in a waiting queue.
- The total
number of burst times is calculated.
- The value
is displayed
- At last, the process stops
1.1.2 SJF Algorithm
- Sort all the processes according to
the arrival time.
- Then select that process that has
minimum arrival time and minimum Burst time.
- After completion of the process
make a pool of processes that arrives afterward till the completion of the
previous process and select that process among the pool which is having
minimum Burst time.
1.1.3
RR Algorithm
- All the
processes are organized in the ready queue based on the arrival time. To
execute the CPU processes, a queue based on FIFO is used.
- Push the
first process from ready queue for the task to be executed for the
allotted time.
- The
process runs for the allocated time even if the process doesn't reach
completion, still it is stopped by the process in queue until the arrival
time for the next process is reached.
- In a
similar manner, another process is selected by the ready queue to execute
its task.
- The above steps are repeated until all processes are finishe
1.2
WAP to Demonstrate following programs.
1.2.1
Program to generate FCFS Algorithm
#include<stdio.h>
int main(){
int
n,bt[30],wait_t[30],turn_ar_t[30],av_wt_t=0,avturn_ar_t=0,i,j;
printf("Please enter the total number
of processes(maximum 30):");
scanf("%d",&n);
printf("\nEnter The Process Burst
Timen");
for(i=0;i<n;i++) {
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wait_t[0]=0;
for(i=1;i<n;i++){
wait_t[i]=0;
for(j=0;j<i;j++)
wait_t[i]+=bt[j];
}
printf("\nProcess\t\tBurst
Time\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++){
turn_ar_t[i]=bt[i]+wait_t[i];
av_wt_t+=wait_t[i];
avturn_ar_t+=turn_ar_t[i];
printf("\nP[%d]\t\t%d\t\t\t%d\t\t\t\t%d",i+1,bt[i],wait_t[i],turn_ar_t[i]);
}
av_wt_t/=i;
avturn_ar_t/=i; // average calculation is done here
printf("\nAverage Waiting
Time:%d",av_wt_t);
printf("\nAverage Turnaround
Time:%d",avturn_ar_t);
return 0;
}
Output
:
1.2.2 Program to generate SJF Algorithm
#include <stdio.h>
int main()
{
int
A[100][4];
int i, j,
n, total = 0, index, temp;
float
avg_wt, avg_tat;
printf("Enter number of process: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");
for (i =
0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}
for (i =
0; i < n; i++) {
index
= i;
for (j
= i + 1; j < n; j++)
if
(A[j][1] < A[index][1])
index = j;
temp =
A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp =
A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] =
0;
for (i =
1; i < n; i++) {
A[i][2] = 0;
for (j
= 0; j < i; j++)
A[i][2] += A[j][1];
total
+= A[i][2];
}
avg_wt =
(float)total / n;
total = 0;
printf("P BT WT
TAT\n");
for (i =
0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total
+= A[i][3];
printf("P%d %d %d
%d\n", A[i][0],
A[i][1], A[i][2], A[i][3]);
}
avg_tat =
(float)total / n;
printf("Average Waiting Time= %f", avg_wt);
printf("\nAverage Turnaround Time= %f", avg_tat);
}
Output
:
1.2.3 Program to generate RR algorithm
#include<stdio.h>
int main(){
int
cnt,j,n,t,remain,flag=0,tq;
int
wt=0,tat=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(cnt=0;cnt<n;cnt++){
printf("Enter Arrival Time and Burst Time for Process Process
Number %d :",cnt+1);
scanf("%d",&at[cnt]);
scanf("%d",&bt[cnt]);
rt[cnt]=bt[cnt];
}
printf("Enter Time Quantum:\t");
scanf("%d",&tq);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(t=0,cnt=0;remain!=0;){
if(rt[cnt]<=tq && rt[cnt]>0){
t+=rt[cnt];
rt[cnt]=0;
flag=1;
}
else
if(rt[cnt]>0){
rt[cnt]-=tq;
t+=tq;
}
if(rt[cnt]==0 && flag==1){
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",cnt+1,t-at[cnt],t-at[cnt]-bt[cnt]);
wt+=t-at[cnt]-bt[cnt];
tat+=t-at[cnt];
flag=0;
}
if(cnt==n-1)
cnt=0;
else
if(at[cnt+1]<=t)
cnt++;
else
cnt=0;
}
printf("\nAverage Waiting Time= %f\n",wt*1.0/n);
printf("Avg Turnaround Time = %f",tat*1.0/n);
return 0;
}
Output:
2 Introduction of Process
A
process refers to an independent, self-contained unit of execution within an
operating system. It is a fundamental concept that enables the execution of
tasks, programs, and applications on a computer. Processes are at the heart of
multitasking and multiprocessing, allowing a computer to handle multiple
operations simultaneously, making it efficient and responsive.
Key
aspects of a process include:
- Process ID (PID
- Memory Space
- Context Switching
- States
- Creation and Termination
- Inter-Process Communication (IPC)
2.1
Inter-Process Communication
Processes
may need to exchange data or synchronize their actions. IPC mechanisms enable
communication and coordination between processes, allowing them to work
together or share resources when needed.
2.2
Define term Deadlock, PCB, Thread and Process
State
2.2.1 Deadlock
Deadlock
is a specific state that can occur in a concurrent computing system, where two
or more processes are unable to proceed further because each is waiting for the
other(s) to release a resource or complete an action. Essentially, deadlock
creates a standstill in the system, where no progress can be made, and the processes
involved become stuck in a loop of waiting indefinitely.
Deadlock
arises when four conditions, known as the "deadlock conditions," are
simultaneously met:
- Mutual Exclusion
- Hold and Wait
- No Preemption
- Circular Wait
2.2.2 PCB
PCB stands for "Process Control Block." It is a data structure used
by operating systems to manage and store information about each individual
process running in the system. The PCB serves as the central repository of
process-related information that the operating system needs to keep track of
the various processes and manage their execution effectively.
The
information stored in PCB typically includes:
- Process ID (PID), Process State
- Program Counter (PC).
- CPU Registers, Memory Pointers
- CPU Scheduling Information
- Process Priority
- Open Files
- Accounting Information
- Parent-Child Relationship
- Inter-Process Communication (IPC)
Data
2.2.3 Thread
A thread is the smallest unit of execution within a process. It is also known as a "lightweight process" because it shares resources with other threads within the same process, unlike separate processes that have their own memory space and resources.
Threads allow a program to perform multiple tasks concurrently, taking advantage of modern multi-core processors and achieving better performance and responsiveness.
2.2.4 Process State
Process state, also known as process status, refers
to the current condition or stage in which a process exists during its
execution within an operating system.
The common process states in most operating systems
include:
·
New, Ready, Running, Blocked (Waiting),
Terminated (Exit) and stopped.
2.3
WAP a program to demonstrate
2.3.1 Program to show creation of new process
#include
<sys/types.h>
#include
<sys/types.h>
#include
<stdio.h>
#include
<unistd.h>
#include
<sys/wait.h>
int
main(int argc, char *argv[]) {
/* fork a child process */
pid_t pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr,
"Fork Failed");
return
1;
}
else if (pid == 0) { /* child process */
printf("I'm
the child \n"); /* you can execute some commands here */
}
else { /* parent process */
/*
parent will wait for the child to complete */
wait(NULL);
/*
When the child is ended, then the parent will continue to execute its code */
printf("Child Complete \n");
}
}
Output :
3 Introduction to Memory Management
Memory
management is a critical aspect of computer systems and operating systems that
deals with the organization and utilization of a computer's memory resources.
In modern computing environments, memory management plays a central role in
ensuring efficient and secure allocation, tracking, and optimization of memory
usage by various processes and applications.
The
primary goals of memory management are as follows:
1. Allocation
2. Deallocation/Freeing
3. Protection
4. Sharing
5. Optimization
3.1 Write algorithm of page replacement using different technique.
3.1.1 FIFO Algorithm
- Begin
turning the pages.
- Declare
the size now relative to Page's length.
- Check
the page to memory for replacement needs.
- Check
whether switching from the old page to the new page in memory is necessary
in a similar manner.
- Create
a queue to hold all of the pages now.
- Add
require page memory to the waiting list.
- Check
for page defects and bad replacements.
- Request
that no processes be introduced.
- Declare
the values.
- Stop
3.1.2 LRU Algorithm
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection
8. Display the value
9. Stop the process
3.1.3 OPR Page Replacement Algorithm
1. Start program
2. Read Number of page and frames
3. Read each page value
4. Search for page in the frames
5. If not available allocate free frames
6. If no frames is free replace the page with the page that is lastly used
7. Print page number of page faults
8. Stop process
3.1.4 Worst Fit Algorithm
1. Input memory block with a size.
2. Input process with size.
3. Initialize
by selecting each process to find the maximum block size that can be assigned
to the current process.
4. If
the condition does not fulfill, they leave the process.
5. If the condition is not fulfilled, then leave
the process and check for the next process.
6. Stop.
3.1.5 Best Fit Algorithm
1. Input memory blocks and processes with sizes.
2. Initialize all memory blocks as free.
3. Start by picking each process and find the minimum block size that can be assigned to current process i.e., find min(bockSize[1], blockSize[2],.....blockSize[n]) > processSize[current], if found then assign it to the current process.
4. If not then leave that process and keep checking the further processes.
3.1.6 First Fit Algorithm
1. Start with all available memory as one big block.
2. When a new process needs memory, look for the first available block that is large enough to hold it.
3. If you find a block that fits the process exactly, allocate it to the process.
4. If the block is larger than the process, split it into two parts: one for the process and the other for remaining free memory.
5. Repeat the process for each new request.
6. When a process finishes, mark its memory block as free.
7. Merge contiguous free memory blocks to prevent fragmentation.
8. If there's no suitable block for a new process, either wait or terminate the process, depending on the system's policy.
3.2 Define different Term
3.2.1
Page
In operating systems, a page is a fixed-sized block of memory used for data storage and management. It's like a small, equal-sized section of a notebook where information can be written or read. Pages are used in memory management to efficiently store and retrieve data from RAM, making the computer system work more smoothly.
3.2.2
Fragment
In operating systems, a fragment refers to small, unused gaps or leftover spaces in memory. It's like having some empty spaces between different pieces of information in a notebook, making it harder to find a continuous blank area to write new data. Fragments can occur in both RAM and disk storage, and they can lead to inefficiency in memory usage and slower performance.
3.2.3
Page Table
In operating systems, a page table is like a map that helps the computer find where each part of a process is stored in memory. It keeps track of which pages of a program are in RAM and their corresponding locations. When the CPU needs to access specific data, it uses the page table to quickly locate the correct memory address. This way, page tables make the process of accessing and managing memory more efficient.
3.2.4
Segment
In operating systems, a segment is like a distinct, self-contained section of a program or data. It's similar to having separate compartments in a drawer, where each compartment holds a specific part of the program or information. Segmentation allows programs to be divided into logical units, making them easier to manage and share resources efficiently. Each segment can have its own size and attributes, such as read or write permissions, which helps in better memory management and protection.
3.2.5 Thrashing
In operating systems, thrashing refers to a situation where the system spends more time swapping data between RAM and disk than actually executing useful tasks. It's like a computer being stuck in a never-ending loop of moving information back and forth between two locations without making any real progress in its work. Thrashing occurs when the system is overloaded with too many processes competing for limited memory resources. As a result, the performance of the system becomes extremely slow and inefficient.
3.2.6 Physical Address
In operating systems, a physical address is like the actual location or address of data in the computer's physical memory. It's similar to a house address that tells you the exact location of a building. When the CPU wants to read or write data, it uses the physical address to access the specific location in RAM where that data is stored. Physical addresses are essential for proper memory management and data retrieval in the computer system.
3.2.7
Logical Address
In
operating systems, a logical address is like a virtual location or address used
by a program to access data. It's similar to a postal code that helps identify
a general area, but it doesn't correspond directly to a physical location. When
a program needs to access data, it uses logical addresses, and the operating
system translates these logical addresses into actual physical addresses in RAM
using techniques like page tables. Logical addresses provide a layer of abstraction,
making it easier to manage memory and protect processes from interfering with
each other.
3.3
Write a program.
3.3.1
FIFO Page Replacement Algorithm
#include
<stdio.h>
int
main()
{
int
referenceString[10], pageFaults = 0, m, n, s, pages, frames;
printf("\nEnter
the number of Pages:\t");
scanf("%d",
&pages);
printf("\nEnter
reference string values:\n");
for(
m = 0; m < pages; m++){
printf("Value No. [%d]:\t", m +
1);
scanf("%d",
&referenceString[m]);
}
printf("\n
What are the total number of frames:\t");
{
scanf("%d", &frames);
}
int
temp[frames];
for(m = 0; m < frames; m++){
temp[m] = -1;
}
for(m
= 0; m < pages; m++){
s = 0;
for(n = 0; n < frames; n++){
if(referenceString[m] == temp[n]){
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s
== 0)){
temp[m] = referenceString[m];
}
else if(s == 0){
temp[(pageFaults - 1) % frames] =
referenceString[m];
}
printf("\n");
for(n = 0; n < frames; n++){
printf("%d\t", temp[n]);
}
}
printf("\nTotal
Page Faults:\t%d\n", pageFaults);
return
0;
}
Output:
3.3.2
LRU Page Replacement Algorithm
#include<stdio.h>
main()
{
int
q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter
no of pages:");
scanf("%d",&n);
printf("Enter
the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter
no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe
no of page faults is %d",c);
}
Output:
3.3.3
OPR Page Replacement Algorithm
#include<stdio.h>
int
main()
{
int
n,pg[30],fr[10];
int
count[10],i,j,k,fault,f,flag,temp,current,c,dist,max,m,cnt,p,x;
fault=0;
dist=0;
k=0;
printf("Enter
the total no pages:\t");
scanf("%d",&n);
printf("Enter
the sequence:");
for(i=0;i<n;i++)
scanf("%d",&pg[i]);
printf("\nEnter
frame size:");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
fr[i]=-1;
}
for(i=0;i<n;i++)
{
flag=0;
temp=pg[i];
for(j=0;j<f;j++)
{
if(temp==fr[j])
{
flag=1;
break;
}
}
if((flag==0)&&(k<f))
{
fault++;
fr[k]=temp;
k++;
}
else
if((flag==0)&&(k==f))
{
fault++;
for(cnt=0;cnt<f;cnt++)
{
current=fr[cnt];
for(c=i;c<n;c++){
if(current!=pg[c])
count[cnt]++;
else
break;
}
}
max=0;
for(m=0;m<f;m++){
if(count[m]>max){
max=count[m];
p=m;
}
}
fr[p]=temp;
}
printf("\npage %d
frame\t",pg[i]);
for(x=0;x<f;x++){
printf("%d\t",fr[x]);
}
}
printf("\nTotal
number of faults=%d",fault);
return
0;
}
Output:
3.3.4
Worst Fit Memory Management Algorithm
#include<stdio.h>
#define
max 25
int
main(){
int
frag[max],b[max],f[max],i,j,nb,nf,temp;
static
int bf[max],ff[max];
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]);
return
0;
}
Output:
3.3.5
Best Fit Memory Management Algorithm
#include<stdio.h>
#define
max 25
int
main(){
int
frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static
int bf[max],ff[max];
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]); Ouput:
}
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]);
return
0;
}
3.3.6
First Fit Memory Management Algorithm
#include<stdio.h>
#define
max 25
int
main(){
int
frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static
int bf[max],ff[max];
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++){ Output:
if(bf[j]!=1){
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;
}
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]);
return
0;
}
4
Introduction to Disk Scheduling
Disk
scheduling is a process in the operating system that determines the order in
which read and write requests to the disk are serviced. When multiple processes
or users request data from the disk, disk scheduling algorithms help decide
which request should be handled first.
The
main objective of disk scheduling is to reduce the seek time, which is the time
taken by the disk arm to move to the desired track. By selecting an efficient
request order, disk scheduling algorithms aim to minimize the time it takes to
access data from the disk and improve overall system performance.
Different
disk scheduling algorithms exist, such as FCFS (First-Come-First-Serve), SSTF
(Shortest Seek Time First), SCAN, C-SCAN, LOOK, C-LOOK, and more.
Each
algorithm has its own advantages and trade-offs, depending on the specific
workload and access patterns of the system.
The
choice of the disk scheduling algorithm can significantly impact the overall
disk I/O performance and the response time of processes that rely on disk
access.
In
summary, disk scheduling is the process of arranging disk read and write
requests to optimize disk access and improve the efficiency of the computer
system.
4.1
Write algorithm of
4.1.1
FCFS
Algorithm of FCFS
- Start with a queue that holds all the pending disk
requests in the order they arrive.
- The head of the disk starts at a particular position
(track) on the disk.
- Initialize the total seek time to zero, as there are
no seeks initially.
- While there are pending disk requests in the queue,
do the following:
a. Dequeue the next request from the queue (the first
request in the queue).
b. Calculate the absolute difference between the current head
position and the requested track position (seek distance).
c. Add the seek distance to the total seek time.
d. Move the disk head to the requested track.
e. Service the request by reading or writing data.
- Repeat step 4 until all requests in the queue are
serviced.
- After servicing all requests, calculate the average
seek time by dividing the total seek time by the number of requests.
4.1.2
SCAN
Algorithm of SCAN
- Start with
a queue that holds all the pending disk requests.
- The head of
the disk starts at a particular position (track) on the disk.
- Initialize
the total seek time to zero, as there are no seeks initially.
- Determine
the direction of the disk movement. For example, if the head is at track
50 and the next request is at track 40, the direction is toward lower
tracks (left). If the next request is at track 60, the direction is toward
higher tracks (right).
- While there
are pending disk requests in the queue, do the following:
a.
Service all requests in the current direction (either left or right) until no
more requests are in that direction.
b.
Calculate the absolute difference between the current head position and the
requested track position (seek distance) for each serviced request.
c.
Add the seek distances to the total seek time.
d.
Move the disk head to the last serviced track in the current direction.
e.
If there are still pending requests in the opposite direction, reverse the
direction of disk movement.
- Repeat step
5 until all requests in the queue are serviced.
- After
servicing all requests, calculate the average seek time by dividing the
total seek time by the number of requests.
4.1.3 C-SCAN
Algorithm for C-SCAN
- Start
with a queue that holds all the pending disk requests.
- The
head of the disk starts at a particular position (track) on the disk.
- Initialize
the total seek time to zero, as there are no seeks initially.
- Determine
the direction of the disk movement. For example, if the head is at track
50 and the next request is at track 40, the direction is toward lower
tracks (left). If the next request is at track 60, the direction is toward
higher tracks (right).
- While
there are pending disk requests in the queue, do the following:
a.
Service all requests in the current direction (either left or right) until no
more requests are in that direction.
b.
Calculate the absolute difference between the current head position and the
requested track position (seek distance) for each serviced request.
c.
Add the seek distances to the total seek time.
d.
Move the disk head to the last serviced track in the current direction.
e.
If there are still pending requests in the opposite direction, move the head
immediately to the first track in that direction, without servicing any
requests in between.
- Repeat
step 5 until all requests in the queue are serviced.
- After
servicing all requests, calculate the average seek time by dividing the
total seek time by the number of requests.
4.2
Write a program to demonstrate.
4.2.1 FCFS disk scheduling algorithm
#include<stdio.h>
#include<stdlib.h>
int
main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial;
printf("Enter the number of
Requests\n");
scanf("%d",&n);
printf("Enter the Requests
sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head
position\n");
scanf("%d",&initial);
// logic for FCFS disk scheduling
for(i=0;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
printf("Total head moment is
%d",TotalHeadMoment);
return 0;
}
Output:
4.2.2 SCAN disk scheduling algorithm
#include<stdio.h>
#include<stdlib.h>
int
main()
{
int
RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of
Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head
position\n");
scanf("%d",&initial);
printf("Enter total disk
size\n");
scanf("%d",&size);
printf("Enter the head movement
direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for Scan disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
//
last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
initial = size-1;
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
//
last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
initial =0;
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}
Output:
4.2.3 C-SCAN disk scheduling algorithm
#include<stdio.h>
#include<stdlib.h>
int
main()
{
int
RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests
sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head
position\n");
scanf("%d",&initial);
printf("Enter total disk
size\n");
scanf("%d",&size);
printf("Enter the head movement
direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for C-Scan disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for( j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
//
last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
/*movement max to min disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial=0;
for( i=0;i<index;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
//
last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
/*movement min to max disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial =size-1;
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is
%d",TotalHeadMoment);
return 0;
}
Output:
<write the conclusion at last>
Don't forget to share your comment with us, also follows us to get more interesting article on different subject.
Comments
Post a Comment