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

OS Lab Manual 2023-24

Uploaded by

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

OS Lab Manual 2023-24

Uploaded by

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

2023-24

H.K.E. Society’s
H
Sir M.. Visvesvaraya College
e of Engineering
Department. of Computer Science & Engineering
(Affiliated to VTU - Belagavi, Approved by AICTE – New Delhi, Accredited by NAAC)
Yeramarus Camp, Raichur – 584135

OPERATING SYSTEMS LABORATORY


MANUAL (BCS303) (IPCC)
(CBCS Scheme)

Compiled by: Prof. Renukadevi


H.K.E. Society’s
Sir
ir M. Visvesvaraya College of Engineering
Department of Computer Science & Engineering
(Affiliated to VTU - Belagavi, Approved by AICTE – New Delhi, Accredited by NAAC)
Yeramarus camp, Raichur – 584135

VISION

The Department of Computer Science & Engineering


Nurtures young engineers with technical knowledge
and to be responsible in societal
ocietal life with leadership
and ethical qualities for life
life-long learning.

MISSION
 To provide young computing aspirants with
best practices in teaching
teaching-learning
learning with
highly experienced faculty
 To team up with industries and provide
experience on latest tools to excel in
promising technologies.
 To prepare students with required
fundamentals practical exposure, social and
professional morals
Programme Outcomes (POs)
PO1: Apply knowledge of computing fundamentals, computing
specialization, mathematics and domain knowledge to provide IT
solutions.
PO2: Identify, analyze and solve IT problems using fundamental
principles of mathematics and computing sciences.
PO3: Design, Develop and evaluate software solutions to meet societal
and environmental concerns.
PO4: Conduct investigations of complex problems using research
based knowledge and methods to provide valid conclusions.
PO5: Select and apply appropriate techniques and modern tools for
complex computing activities.
PO6: Understand professional ethics, cyber regulations and
responsibilities.
PO7: Involve in life-long learning for continual development as an IT
professional.
PO8: Apply and demonstrate computing and management principles
to manage projects in multidisciplinary environments by involving in
different roles
PO9: Comprehend& write effective reports and make quality
presentations.
PO10: Understand and assess the impact of IT solutions on socio-
environmental Issues.
PO11: Work collaboratively as a member or leader in
multidisciplinary teams.
PO12: Identify potential business opportunities and innovate to
create value to the society and seize that opportunity.
CONTENTS
List of problems for which student should develop program and execute in the Laboratory.
Implement all the programs in “C ” Programming Language and Linux OS.

Exp. No Title Of the Experiment Page No.

Develop a c program to implement the Process system calls (fork ( ),


1 1–7
exec( ), wait( ), create process, terminate process)

Simulate the following CPU scheduling algorithms to find turnaround


2 8 – 16
time and waiting time a) FCFS b) SJF c) Round Robin d) Priority.

Develop a C program to simulate producer-consumer problem using


3 17 – 18
semaphores.
Develop a C program which demonstrates interprocess communication
4 between a reader process and a writer process. Use mkfifo, open, read, 19 – 21
write and close APIs in your program.
Develop a C program to simulate Bankers Algorithm for DeadLock
5 22 - 24
Avoidance.

Develop a C program to simulate the following contiguous memory


6 25 – 31
allocation Techniques: a) Worst fit b) Best fit c) First fit.

Develop a C program to simulate page replacement algorithms:


7 32 – 37
a) FIFO b) LRU

Simulate following File Organization Techniques:


8 38 – 44
a) Single level directory b) Two level directory

9 Develop a C program to simulate the Linked file allocation strategies. 45 – 46

10 Develop a C program to simulate SCAN disk scheduling algorithm. 47 - 49

Laboratory Outcomes: The student should be able to:

CO1: Explain the structure and functionality of operating system


CO2: Apply appropriate CPU scheduling algorithms for the given problem
CO3: Analyse the various techniques for process synchronization and deadlock handling
CO4: Apply the various techniques for memory management
CO5: Explain file and secondary storage management strategies.
CO6: Describe the need for information protection mechanisms
CIE for the practical component of the IPCC:

 15 marks for the conduction of the experiment and preparation of laboratory record, and 10
marks for the test to be conducted after the completion of all the laboratory sessions.

 On completion of every experiment/program in the laboratory, the students shall be


evaluated including viva-voce and marks shall be awarded on the same day.

 The CIE marks awarded in the case of the Practical component shall be based on the
continuous evaluation of the laboratory report. Each experiment report can be evaluated for
10 marks. Marks of all experiments write-ups are added and scaled down to 15 marks.

 The laboratory test (duration 02/03 hours) after completion of all the experiments shall be
conducted for 50 marks and scaled down to 10 marks.

 Scaled-down marks of write-up evaluations and tests added will be CIE marks for the
laboratory component of IPCC for 25 marks.

 The student has to secure 40% of 25 marks to qualify in the CIE of the practical component of
the IPCC.

● Change of experiment is allowed only once and marks allotted for procedure to be
made zero of the changed part only.
OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 1:

Aim: Develop a c program to implement the Process system calls.

Description:
i. Fork( ): fork( ) is used to create a new process known as a child process which
runs concurrently with the process that made the fork( ) call.
The child process is an exact copy of parent process it has its own unique
process ID and its parents ID is set to the ID of the parent process. In the parent
process fork returns the process ID of the newly created child process. In the
child process fork returns a zero.

ii. Wait( ): the primary function of the wait( ) is for a parent process to wait() for
its child’s process to compete execution. When a parent process calls a wait it
gets blocked until one of its child process exits or signal is received.
Upon the successful completion of the child process wait( ) returns the process
ID of the child. If there is an error it returns -1.

iii. Exec( ): exec replaces the current process image with the new process image.
This is typically used in conjunction with the fork( ). A process would fork( ) a
child and the child process then uses exec to run a new program.
On success, exec( ) does not return to the original program, instead the new
programs main function is called. If there is an error it returns -1.

iv. Exit( ): it is used to end up process explicitly when a process calls a exit( ) it
signals the OS to release all the resources associated with that process and
terminate it. Before termination exit( ) ensures the standard IO buffers are
flushed, files opened by the process are closed and temporary files are deleted.

Dept of CSE, SMVCE Page 1


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 1a: Fork( )

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
int main(int argc, char **argv)
{
pid_t pid;
pid = fork();
if(pid==0)
{
printf("It is the child process and pid is %d\n",getpid());
exit(0);
}
else if(pid> 0)
{
printf("It is the parent process and pid is %d\n",getpid());
}
else
{
printf("Error while forking\n");
exit(EXIT_FAILURE);
}
return 0;
}

OUTPUT:
[arjunshanka@localhost ~]$ cc pg1a.c
[arjunshanka@localhost ~]$ ./a.out
It is the child process and pid is 3324
It is the parent process and pid is 3323

Dept of CSE, SMVCE Page 2


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 1b:EXEC( )
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
main(void)
{
pid_t pid = 0;
int status;
pid = fork( );
if (pid == 0)
{
printf("I am the child.");
execl("/bin/ls", "ls", "-l", "/home/arjunshanka/", (char *) 0);
perror("In exec( ): ");
}
if (pid> 0)
{
printf("I am the parent, and the child is %d.\n", pid);
pid = wait(&status);
printf("End of process %d: ", pid);
if (WIFEXITED(status))
{
printf("The process ended with exit(%d).\n", WEXITSTATUS(status));
}
if (WIFSIGNALED(status))
{
printf("The process ended with kill -%d.\n", WTERMSIG(status));
}
}
if (pid< 0)
{
perror("In fork():");
}
exit(0);
}

Dept of CSE, SMVCE Page 3


OPERATING SYSTEMS LAB MANUAL (BCS303)

OUTPUT:
[arjunshanka@localhost ~]$ cc pg1b.c
[arjunshanka@localhost ~]$ ./a.out
total 2032
-rw-rw-r-- 1 arjunshankaarjunshanka 292 2012-05-26 17:07 1a1.l
-rw-rw-r-- 1 arjunshankaarjunshanka 294 2022-07-04 14:58 1a.l
..
..
..
rwxr-xr-x 2 arjunshankaarjunshanka 4096 2012-02-15 03:01 Templates
-rw-rw-r-- 1 arjunshankaarjunshanka 810 2023-02-20 16:24 test2.c

Dept of CSE, SMVCE Page 4


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 1c: wait( )

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
int main(int argc, char **argv)
{
pid_t pid;
pid = fork( );
if(pid==0)
{
printf("It is the child process and pid is %d\n",getpid( ));
int i=0;
for(i=0;i<8;i++)
{
printf("%d\n",i);
}
exit(0);
}
else if(pid>0)
{
printf("It is the parent process and pid is %d\n", getpid( ));
int status;
wait(&status);
printf("Child is repeated\n");
}
else
{
printf("Error in forking..\n");

Dept of CSE, SMVCE Page 5


OPERATING SYSTEMS LAB MANUAL (BCS303)

exit(EXIT_FAILURE);
}
return 0;
}

OUTPUT:
[arjunshanka@localhost ~]$ cc pg1c.c
[arjunshanka@localhost ~]$ ./a.out
It is the child process and pid is 3380
0 1 2 3 4 5 6 7
It is the parent process and pid is 3379
Child is repeated

Dept of CSE, SMVCE Page 6


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 1d: exit( )

#include <stdio.h>
#include <stdlib.h>
void exitfunc( )
{
printf("Called cleanup function - exitfunc( )\n");
return;
}
int main( )
{
atexit(exitfunc);
printf("Hello, World!\n");
exit(0);
}

OUTPUT:
[arjunshanka@localhost ~]$ cc pg1d.c
[arjunshanka@localhost ~]$ ./a.out
Hello, World!
Called cleanup function - exitfunc( )

Dept of CSE, SMVCE Page 7


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 2:
AIM: Simulate the following CPU scheduling algorithms to find turnaround time and
waiting time

DESCRIPTION: To calculate the average waiting time using the FCFS algorithm first
the waiting time of the first process is kept zero and the waiting time of the second
process is the burst time of the first process and the waiting time of the third process
is the sum of the burst times of the first and the second process and so on. After
calculating all the waiting times the average waiting time is calculated as the average
of all waiting time.

Program 2a: FCFS


#include<stdio.h>
main( )
{
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
printf("\nEnter the number of processes: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("\n Enter Burst Time for Process %d:", i);
scanf("%d", &bt[i]);
}
wt[0]=wtavg=0;
tat[0]=tatavg=bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0; i<n; i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\n Average Waiting Time -- %f", wtavg/n);
printf("\n Average Turnaround Time -- %f", tatavg/n);
}

Dept of CSE, SMVCE Page 8


OPERATING SYSTEMS LAB MANUAL (BCS303)

OUTPUT:
[arjunshanka@localhost ~]$ cc pg2a.c
[arjunshanka@localhost ~]$ ./a.out
Enter the number of processes: 3
Enter Burst Time for Process 0:24
Enter Burst Time for Process 1:3
Enter Burst Time for Process 2:3
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time -- 17.000000
Average Turnaround Time -- 27.000000

Dept of CSE, SMVCE Page 9


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 2b: Shortest Job First (SJF)


DESCRIPTION: To calculate the average waiting time in the shortest job first
algorithm the sorting of the process based on their burst time in ascending order then
calculate the waiting time of each process as the sum of the bursting times of all the
process previous or before to that process.

#include<stdio.h>
main( )
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp;
float wtavg, tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];

Dept of CSE, SMVCE Page 10


OPERATING SYSTEMS LAB MANUAL (BCS303)

for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
}

OUTPUT:
[arjunshanka@localhost ~]$ cc pg2b.c
[arjunshanka@localhost ~]$ ./a.out
Enter the number of processes -- 4
Enter Burst Time for Process 0 -- 6
Enter Burst Time for Process 1 -- 8
Enter Burst Time for Process 2 -- 7
Enter Burst Time for Process 3 -- 3
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24
Average Waiting Time -- 7.000000

Dept of CSE, SMVCE Page 11


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 2c: Round Robbin

DESCRIPTON: To aim is to calculate the average waiting time. There will be a


time slice, each process should be executed within that time-slice and if not it will
go to the waiting state so first check whether the burst time is less than the time-
slice. If it is less than it assign the waiting time to the sum of the total times. If it is
greater than the burst-time then subtract the time slot from the actual burst time
and increment it by time-slot and the loop continues until all the processes are
completed.

#include<stdio.h>
main( )
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
printf("Enter the no of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t)
{
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else

Dept of CSE, SMVCE Page 12


OPERATING SYSTEMS LAB MANUAL (BCS303)

{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++)
{
wa[i]=tat[i]-ct[i];
att+=tat[i];
awt+=wa[i];}
printf("\nThe Average Turnaround time is -- %f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);
printf("\nPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
}

OUTPUT:
[arjunshanka@localhost ~]$ cc pg2c.c
[arjunshanka@localhost ~]$ ./a.out

Enter the no of processes -- 4


Enter Burst Time for process 1 -- 6
Enter Burst Time for process 2 -- 8
Enter Burst Time for process 3 -- 7
Enter Burst Time for process 4 -- 3
Enter the size of time slice -- 3
The Average Turnaround time is -- 18.500000
The Average Waiting time is -- 12.500000

PROCESS BURST TIME WAITING TIME TURNAROUND TIME


1 6 9 15
2 8 15 23
3 7 17 24
4 3 9 12

Dept of CSE, SMVCE Page 13


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 2d: Priority

DESCRIPTION: To calculate the average waiting time in the priority algorithm,


sort the burst times according to their priorities and then calculate the average
waiting time of the processes. The waiting time of each process is obtained by
summing up the burst times of all the previous processes.

#include<stdio.h>
main( )
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ",i);
scanf("%d %d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(pri[i] >pri[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];

Dept of CSE, SMVCE Page 14


OPERATING SYSTEMS LAB MANUAL (BCS303)

pri[k]=temp;
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND
TIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is --- %f",tatavg/n);
}

OUTPUT:
[arjunshanka@localhost ~]$ cc pg2d.c
[arjunshanka@localhost ~]$ ./a.out

Enter the number of processes --- 5


Enter the Burst Time & Priority of Process 0 --- 10 3
Enter the Burst Time & Priority of Process 1 --- 1 1
Enter the Burst Time & Priority of Process 2 --- 2 4
Enter the Burst Time & Priority of Process 3 --- 1 5
Enter the Burst Time & Priority of Process 4 --- 5 2

Dept of CSE, SMVCE Page 15


OPERATING SYSTEMS LAB MANUAL (BCS303)

PROCESS PRIORITY BURST TIME WAITING TIME TURNAROUND TIME


1 1 1 0 1
4 2 5 1 6
0 3 10 6 16
2 4 2 16 18
3 5 1 18 19

Average Waiting Time is --- 8.200000


Average Turnaround Time is --- 12.000000

Dept of CSE, SMVCE Page 16


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 3:
AIM: Develop a C program to simulate producer-consumer problem using
semaphores.

DESCRIPTION: Producer consumer problem is a synchronization problem. There is a


fixed size buffer where the producer produces items and that is consumed by a
consumer process. One solution to the produce consumer problem uses shared
memory. To allow producer and consumer processes to run concurrently, there must
be available a buffer of items that can be filled by the producer and emptied by the
consumer. This buffer will reside in a region of memory that is shared by the producer
and consumer processes. The producer and consumer must be synchronized, so that
the consumer does not try to consume an item that has not yet been produced.

#include<stdio.h>
void main( )
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0;
out = 0;
bufsize = 10;
while(choice!=3)
{
printf("\n1.Producer\t 2.Consumer\t 3.Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf("\nBuffer is Full");
else
{
printf("\nEnter the value: ");
scanf("%d", &produce);

Dept of CSE, SMVCE Page 17


OPERATING SYSTEMS LAB MANUAL (BCS303)

buffer[in] = produce;
in = (in+1)%bufsize;
}
break;
case 2: if(in == out)
printf("\nBuffer is Empty");
else
{
consume = buffer[out];
printf("\nThe consumed value is %d", consume);
out = (out+1)%bufsize;
}
break;
}
}
}
OUTPUT:
[arjunshanka@localhost ~] cc pg3.c
[arjunshanka@localhost ~]$ ./a.out
1.Producer 2.Consumer 3.Exit
Enter your choice: 2
Buffer is Empty
1.Producer 2.Consumer 3.Exit
Enter your choice: 1
Enter the value: 100
1.Producer 2.Consumer 3.Exit
Enter your choice: 2
The consumed value is 100
1.Producer 2.Consumer 3.Exit
Enter your choice: 3

Dept of CSE, SMVCE Page 18


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 4:
AIM: Develop a C program which demonstrates inter process communication between
a reader process and a writer process. Use mkfifo, open, read, write and close APIs in
your program.

DESCRIPTION: The reader-writer problem is a classic synchronization problem in


operating systems where multiple processes require access to a shared resource. In
this problem, some processes may only read the resource while others may write to it.
The goal is to ensure that multiple reader processes can access the resource
simultaneously, but only one writer process can access the resource at a time to avoid
data inconsistency.

Writer process

#include<stdio.h>

#include<fcntl.h>

#include<sys/stat.h>

#include<sys/types.h>

#include <unistd.h>

int main( )

int fd;

char buf[1024];

/* create the FIFO (named pipe) */

char *myfifo = "/tmp/myfifo";

mkfifo(myfifo, 0666);

printf("Run Reader process to read the FIFO File\n");

fd = open(myfifo, O_WRONLY);

write(fd,"Hi", sizeof("Hi"));

Dept of CSE, SMVCE Page 19


OPERATING SYSTEMS LAB MANUAL (BCS303)

/* write "Hi" to the FIFO */

close(fd);

unlink(myfifo);

/* remove the FIFO */

return 0;

Reader process

#include<stdio.h>

#include<fcntl.h>

#include<sys/stat.h>

#include<sys/types.h>

#include <unistd.h>

#define MAX_BUF 1024

int main()

int fd;

/* A temp FIFO file is not created in reader */

char *myfifo = "/tmp/myfifo";

char buf[MAX_BUF];

/* open, read, and display the message from the FIFO */

fd = open(myfifo, O_RDONLY);

read(fd, buf, MAX_BUF);

printf("Writer: %s\n", buf);

close(fd);

Dept of CSE, SMVCE Page 20


OPERATING SYSTEMS LAB MANUAL (BCS303)

return 0;

OUTPUT:

Note: 1. Open two terminals for Reader & Writer processes.

2. Run Writer process.

3. Run Reader process

[arjunshanka@localhost ~]$ cc pg4writer.c

[arjunshanka@localhost ~]$ ./a.out

Reader is reading message from writer

[arjunshanka@localhost ~]$ cc pg4reader.c

[arjunshanka@localhost ~]$ ./a.out

Writer: Hi

Dept of CSE, SMVCE Page 21


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 5:
AIM: Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.

DESCRIPTION: Deadlock is a situation where in two or more competing actions are


waiting for the other to finish, and thus neither ever does. When a new process enters
a system, it must declare the maximum number of instances of each resource type it
needed. This number may exceed the total number of resources in the system. When
the user request a set of resources, the system must determine whether the allocation
of each resources will leave the system in safe state. If it will the resources are
allocation; otherwise the process must wait until some other process release the
resources.

include<stdio.h>
#include<string.h>
void main( )
{
int alloc[10][10],max[10][10];
int avail[10],work[10],total[10];
int i,j,k,n,need[10][10];
int m;
int count=0,c=0;
char finish[10];
printf("Enter the no. of processes and resources:");
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
finish[i]='n';
printf("Enter the claim matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&max[i][j]);
printf("Enter the allocation matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&alloc[i][j]);
printf("Resource vector:");
for(i=0;i<m;i++)
scanf("%d",&total[i]);
for(i=0;i<m;i++)
avail[i]=0;

Dept of CSE, SMVCE Page 22


OPERATING SYSTEMS LAB MANUAL (BCS303)

for(i=0;i<n;i++)
for(j=0;j<m;j++)
avail[j]+=alloc[i][j];
for(i=0;i<m;i++)
work[i]=avail[i];
for(j=0;j<m;j++)
work[j]=total[j]-work[j];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
need[i][j]=max[i][j]-alloc[i][j];
A:
for(i=0;i<n;i++)
{
c=0;
for(j=0;j<m;j++)
if((need[i][j]<=work[j])&&(finish[i]=='n')) c++;
if(c==m)
{
printf("All the resources can be allocated to Process %d", i+1);
printf("\n\nAvailable resources are:");
for(k=0;k<m;k++)
{
work[k]+=alloc[i][k];
printf("%4d",work[k]);
}
printf("\n");
finish[i]='y';
printf("\nProcess %d executed?:%c \n",i+1,finish[i]);
count++;
}
}
if(count!=n) goto A;
else
printf("\n System is in safe mode");
printf("\n The given state is safe state");
}

Dept of CSE, SMVCE Page 23


OPERATING SYSTEMS LAB MANUAL (BCS303)

OUTPUT:
[arjunshanka@localhost ~]$ cc pg5.c
[arjunshanka@localhost ~]$ ./a.out

Enter the no. of processes and resources: 4 3


Enter the claim matrix:
322
613
314
422
Enter the allocation matrix:
100
612
211
002
Resource vector: 9 3 6
All the resources can be allocated to Process 2
Available resources are: 6 2 3
Process 2 executed?: y
All the resources can be allocated to Process 3
Available resources are: 8 3 4
Process 3 executed?: y
All the resources can be allocated to Process 4
Available resources are: 8 3 6
Process 4 executed?: y
All the resources can be allocated to Process 1
Available resources are: 9 3 6
Process 1 executed?:y
System is in safe mode

Dept of CSE, SMVCE Page 24


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 6:
AIM: Develop a C program to simulate the following contiguous memory allocation
Techniques.

DESCRIPTION: One of the simplest methods for memory allocation is to divide


memory into several fixed-sized partitions. Each partition may contain exactly one
process. In this multiple-partition method, when a partition is free, a process is
selected from the input queue and is loaded into the free partition. When the process
terminates, the partition becomes available for another process. The operating system
keeps a table indicating which parts of memory are available and which are occupied.
Finally, when a process arrives and needs memory, a memory section large enough for
this process is provided. When it is time to load or swap a process into main memory,
and if there is more than one free block of memory of sufficient size, then the
operating system must decide which free block to allocate. Best-fit strategy chooses
the block that is closest in size to the request. First-fit chooses the first available block
that is large enough. Worst-fit chooses the largest available block.

Program 6a: First Bit


#include<stdio.h>
#define max 25
void main( )
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
staticint 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]);
}

Dept of CSE, SMVCE Page 25


OPERATING SYSTEMS LAB MANUAL (BCS303)

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("\n File_no: \t File_size :\t Block_no:\t Block_size:\t Fragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d\n",i,f[i],ff[i],b[ff[i]],frag[i]);
}

Dept of CSE, SMVCE Page 26


OPERATING SYSTEMS LAB MANUAL (BCS303)

OUTPUT:
[arjunshanka@localhost ~]$ cc pg6.c
[arjunshanka@localhost ~]$ ./a.out

Memory Management Scheme - First Fit


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

File_no: File_size : Block_no: Block_size: Fragement


1 1 1 5 4

2 4 3 7 3

Dept of CSE, SMVCE Page 27


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 6b: Best Fit

#include<stdio.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];
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

Dept of CSE, SMVCE Page 28


OPERATING SYSTEMS LAB MANUAL (BCS303)

bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\t File Size\t Block No\t Block Size\t Fragment");
for(i=1;i<=nf&&ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d\n",i,f[i],ff[i],b[ff[i]],frag[i]);
}

OUTPUT:
[arjunshanka@localhost ~]$ cc pg6b.c
[arjunshanka@localhost ~]$ ./a.out

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

FileNo File Size Block No Block Size Fragment


1 1 2 2 1

2 4 1 5 1

Dept of CSE, SMVCE Page 29


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 6c: Worst Fit


#include<stdio.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];
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;
}
}
}

Dept of CSE, SMVCE Page 30


OPERATING SYSTEMS LAB MANUAL (BCS303)

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\n",i,f[i],ff[i],b[ff[i]],frag[i]);
}

OUTPUT:
[arjunshanka@localhost ~]$ cc pg6c.c
[arjunshanka@localhost ~]$ ./a.out

Memory Management Scheme - Worst Fit


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

File_no: File_size: Block_no: Block_size: Fragement


1 1 3 7 6

2 4 1 5 1

Dept of CSE, SMVCE Page 31


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 7:
AIM: Develop a C program to simulate page replacement algorithms.

DESCRIPTION: Page replacement algorithms are an important part of virtual memory


management and it helps the OS to decide which memory page can be moved out
making space for the currently needed page. However, the ultimate objective of all
page replacement algorithms is to reduce the number of page faults.

FIFO-This is the simplest page replacement algorithm. In this algorithm, the operating
system keeps track of all pages in the memory in a queue, the oldest page is in the
front of the queue. When a page needs to be replaced page in the front of the queue is
selected for removal.

LRU-In this algorithm page will be replaced which is least recently used.

Program 7a: FIFO


#include<stdio.h>
int fr[3];
void main( )
{
void display( );
int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};
int flag1=0,flag2=0,pf=0,frsize=3,top=0;
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0;
flag2=0;
for(i=0;i<12;i++)
{
if(fr[i]==page[j])

Dept of CSE, SMVCE Page 32


OPERATING SYSTEMS LAB MANUAL (BCS303)

{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<frsize;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
fr[top]=page[j];
top++;
pf++;
if(top>=frsize)
top=0;
}
display( );
}
printf("\n Number of page faults : %d\n ",pf+frsize);
}

Dept of CSE, SMVCE Page 33


OPERATING SYSTEMS LAB MANUAL (BCS303)

void display( )
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("%d\t",fr[i]);
}

OUTPUT:
[arjunshanka@localhost ~]$ cc pg7a.c
[arjunshanka@localhost ~]$ ./a.out
2 -1 -1
2 3 -1
2 3 -1
2 3 1
5 3 1
5 2 1
5 2 4
5 2 4
3 2 4
3 2 4
3 5 4
3 5 2
Number of page faults: 9

Dept of CSE, SMVCE Page 34


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 7b: LRU


#include<stdio.h>
int fr[3];
void main( )
{
void display( );
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int index,k,l,flag1=0,flag2=0,pf=0,frsize=3;
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0;flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<3;i++)
{
if(fr[i]==-1)
{

Dept of CSE, SMVCE Page 35


OPERATING SYSTEMS LAB MANUAL (BCS303)

fr[i]=p[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<3;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<3;i++)
{
if(fr[i]==p[k])
fs[i]=1;
}
}
for(i=0;i<3;i++)
{
if(fs[i]==0)
index=i;
}
fr[index]=p[j];
pf++;
}
display( );
}
printf("\n no of page faults :%d",pf+frsize);
}

Dept of CSE, SMVCE Page 36


OPERATING SYSTEMS LAB MANUAL (BCS303)

void display( )
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("\t%d",fr[i]);
}

OUTPUT:
[arjunshanka@localhost ~]$ cc pg7b.c
[arjunshanka@localhost ~]$ ./a.out
2 -1 -1
2 3 -1
2 3 -1
2 3 1
2 5 1
2 5 1
2 5 4
2 5 4
3 5 4
3 5 2
3 5 2
3 5 2
No of page faults: 7

Dept of CSE, SMVCE Page 37


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 8:
AIM: Simulate following File Organization Techniques.

DESCRIPTION: The directory structure is the organization of files into a hierarchy of


folders. In a single-level directory system, all the files are placed in one directory.
There is a root directory which has all files. It has a simple architecture and there are
no sub directories. Advantage of single level directory system is that it is easy to find a
file in the directory.

Program 8a: Single level directory

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main( )
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory--");
scanf("%s", dir.dname);
while(1)
{
printf(“\n\n 1. Create File\t 2. Delete File\t 3. Search File \n 4. Display Files\t5.
Exit\n“);
printf(“Enter your choice – “);
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the name of the file -- ");
scanf("%s", dir.fname[dir.fcnt]);
dir.fcnt++; break;
case 2:printf("\nEnter the name of the file -- ");
scanf("%s",f);

Dept of CSE, SMVCE Page 38


OPERATING SYSTEMS LAB MANUAL (BCS303)

for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3:printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4:if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
break;
}
default: exit(0);
}
}

Dept of CSE, SMVCE Page 39


OPERATING SYSTEMS LAB MANUAL (BCS303)

}
OUTPUT:
[arjunshanka@localhost ~]$ ./a.out
Enter name of directory-- CSE
1. Create File 2. Delete File3. Search File 4. Display Files 5. Exit
Enter your choice -- 1
Enter the name of the file -- cse
1. Create File 2. Delete File3. Search File 4. Display Files 5. Exit
Enter your choice -- 1
Enter the name of the file -- ise
1. Create File 2. Delete File3. Search File 4. Display Files 5. Exit
Enter your choice -- 4
The Files are -- cse ise
1. Create File 2. Delete File3. Search File 4. Display Files 5. Exit
Enter your choice -- 3
Enter the name of the file -- cse
File cse is found
1. Create File 2. Delete File3. Search File 4. Display Files 5. Exit
Enter your choice -- 3
Enter the name of the file -- ISE
File ISE not found
1. Create File 2. Delete File3. Search File 4. Display Files 5. Exit
Enter your choice -- 2
Enter the name of the file -- ise
File ise is deleted
1. Create File 2. Delete File3. Search File
4. Display Files 5. Exit
Enter your choice – 5

Dept of CSE, SMVCE Page 40


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 8b:

Description: In the two-level directory system, each user has own user file directory
(UFD). The system maintains a master block that has one entry for each user. This
master block contains the addresses of the directory of the users. When a user job
starts or a user logs in, the system's master file directory (MFD) is searched. When a
user refers to a particular file, only his own UFD is searched.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct
{
chardname[10],fname[10][10];
intfcnt;
}dir[10];
void main( )
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t5. Display\t6. Exit\t Enter your choice --");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
scanf("%s",dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file --");

Dept of CSE, SMVCE Page 41


OPERATING SYSTEMS LAB MANUAL (BCS303)

scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++; printf("File created");
}
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f); for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{

Dept of CSE, SMVCE Page 42


OPERATING SYSTEMS LAB MANUAL (BCS303)

printf("File %s is found ",f);


goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
}

OUTPUT:
[arjunshanka@localhost ~]$ cc pg8a.c
[arjunshanka@localhost ~]$ ./a.out
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice – 1
Enter name of directory -- CSE
Directory created
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice --2
Enter name of the directory -- CSE
Enter name of the file-- SLN

Dept of CSE, SMVCE Page 43


OPERATING SYSTEMS LAB MANUAL (BCS303)

File created
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 5
Directory Files
CSE SLN
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice –1
Enter name of directory -- ISE
Directory created
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 2
Enter name of the directory – ISE
Enter name of the file --SLNCE
File created
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 5
Directory Files
CSE SLN
ISE SLNCE
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 3
Enter name of the directory -- CSE
Enter name of the file -- SLN
File SLN is deleted
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 5
Directory Files
CSE
ISE SLNCE
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter name of the directory -- ISE
Enter the name of the file -- SLN
File SLN not found

Dept of CSE, SMVCE Page 44


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 9:
AIM: Develop a C program to simulate the Linked file allocation strategies.

DESCRIPTION: In the chained method file allocation table contains a field which
points to starting block of memory. From it for each bloc a pointer is kept to next
successive block. Hence, there is no external fragmentation.

#include<stdio.h>
#include<stdlib.h>
main( )
{
int f[50],p,i,j,k,a,st,len,n,c;
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks that are already allocated");
scanf("%d",&p);
printf("\nEnter the blocks no.s that are already allocated");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:printf("Enter the starting index block & length");
scanf("%d%d",&st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("\n %d->file is already allocated",j);
k++;
}

Dept of CSE, SMVCE Page 45


OPERATING SYSTEMS LAB MANUAL (BCS303)

}
}
else {
printf("\n If u want to enter one more file? (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
}
else exit(0);
}

OUTPUT:
[arjunshanka@localhost ~]$ cc pg9.c

[arjunshanka@localhost ~]$ ./a.out

Enter how many blocks that are already allocated: 2

Enter the blocks no.s that are already allocated: 5 6

Enter the starting index block & length1: 8

1->1

2->1

3->1

4->1

5->file is already allocated

6->file is already allocated

7->1

8->1

9->1

10->1

If u want to enter one more file? (yes-1/no-0): 0

Dept of CSE, SMVCE Page 46


OPERATING SYSTEMS LAB MANUAL (BCS303)

Program 10:
AIM: Develop a C program to simulate SCAN disk scheduling algorithm.

DESCRIPTION: In the SCAN algorithm, the disk arm starts at one end, and moves
towards the other end, servicing requests as it reaches each cylinder, until it gets to
the other end of the disk. At the other end, the direction of head movement is reversed,
and servicing continues. The head continuously scans back and forth across the disk.

#include <stdio.h>
int request[50];
int SIZE;
int head;
int uptrack;
int downtrack;
struct max{
int up;
int down;
} kate[50];
void sort(int n)
{
int i, j;
for (i = 0; i < n - 1; i++){
for (j = 0; j < n - i - 1; j++){
if (request[j] > request[j + 1]){
int temp = request[j];
request[j] = request[j + 1];
request[j + 1] = temp;
}
}
}
j = 0;
i = 0;
while (request[i] != head)
{
kate[j].down = request[i];
j++;
i++;
}
downtrack = j;

Dept of CSE, SMVCE Page 47


OPERATING SYSTEMS LAB MANUAL (BCS303)

i++;
j = 0;
while (i < n) {
kate[j].up = request[i];
j++;
i++;
}
uptrack = j;
}
void scan(int n){
int i;
printf("SEEK SEQUENCE = ");
sort(n);
for (i = downtrack - 1; i > 0; i--){
printf("%d ", head);
head = kate[i].down;
}
for (i = 0; i <uptrack - 1; i++){
printf("%d ", head);
head = kate[i].up;
}
}
int main( ){
int n, i;
printf("ENTER THE DISK SIZE :\n");
scanf("%d", &SIZE);
printf("ENTER THE NO OF REQUEST SEQUENCE :\n");
scanf("%d", &n);
printf("ENTER THE REQUEST SEQUENCE :\n");
for (i = 0; i < n; i++)
scanf("%d", &request[i]);
printf("ENTER THE CURRENT HEAD :\n");
scanf("%d", &head);
request[n] = head;
request[n + 1] = SIZE - 1;
request[n + 2] = 0;
scan(n + 3);
}

Dept of CSE, SMVCE Page 48


OPERATING SYSTEMS LAB MANUAL (BCS303)

OUTPUT:

[arjunshanka@localhost ~]$ cc pgs10.c

[arjunshanka@localhost ~]$ ./a.out

ENTER THE DISK SIZE: 190

ENTER THE NO OF REQUEST SEQUENCE: 7

ENTER THE REQUEST SEQUENCE:

170

150

135

10

20

ENTER THE CURRENT HEAD: 50

SEEK SEQUENCE= 50 20 10 5 4 135 150 170

Dept of CSE, SMVCE Page 49

You might also like