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

OS LAB Manual

Lab manual

Uploaded by

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

OS LAB Manual

Lab manual

Uploaded by

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

LABORATORY MANUAL

OPERATING SYSTEMS (2005PC62)


II YEAR I SEM

Prepared By

Dr.Y.Geetha Reddy Mrs. K.Ramya Sree Mrs.D.Manasa Spandhana


Associate Professor Assistant Professor Assistant Professor

Mr. Karunakar Mrs. Naseera Mr.Ranadheer


Assistant Professor Assistant Professor Assistant Professor

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

MALLAREDDY ENGINEERING COLLEGE FOR WOMEN


(Autonomous Institution-UGC, Govt. of India)
Accredited by NBA & NAAC with ‘A+’ Grade, NIRF Ranking by MHRD, Govt
of India Department of Computer Science& Engineering.
2024-2025
COURSE NAME: OPERATING SYSTEMS
LAB COURSE CODE: 2005PC62

COURSE OBJECTIVES
 To understand the functionalities of various layers of OSI model.
 To explain the difference between Hardware, Software, Operating Systems, Programs
and Files.
 Identify the purpose of different Software Applications

COURSE OUTCOMES
At the end of the course the students are able to:

CO1 : Ability to implement inter process communication between two processes.

CO2 : Ability to design and solve synchronization problems.


CO3 : Ability to simulate and implement operating systems concepts such as scheduling,
Deadlock management, file management and memory management

COURSE OUTCOME’S MAPPING WITH PROGRAM


OUTCOME’S

Program
Outcomes
PO1 Engineering knowledge: Apply the knowledge of Mathematics, Science, Engineering
Fundamentals,and an Engineering specialization to the solution of complex engineering
problems.
PO2 Problem analysis: Identify, Formulate, Review Research Literature, and Analyze
Complex Engineering Problems Reaching substantiated conclusions using first
principles of Mathematics, Natural Sciences, and Engineering Sciences.

PO3 Design/development of solutions: Design Solutions for Complex Engineering


problems and design system components or processes that meet the specified needs
with appropriate consideration for the public health and safety, and the cultural,
societal, and environmental considerations.

PO4 Conduct investigations of complex problems: Use Research-Based Knowledge and


Research
methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.
PO5 Modern tool usage: Create, Select, and Apply appropriate techniques, resources,
and modern engineering and IT tools including prediction and modeling to complex
engineeringactivities with an understanding of thelimitations.
PO6 The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the professional engineering practice.
PO7 Environment and sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the
knowledge of, and need for sustainable development.

PO8 Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.

PO9 Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.

PO10 Communication: Communicate effectively on complex engineering activities with the


engineering community and with society at large, such as, being able to comprehend
and
write effective reports and design documentation, make effective presentations, and
give and receive clear instructions.
PO11 Project management and finance: Demonstrate knowledge and understanding of
the engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in multidisciplinary
environments.
PO12 Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change.
COURSE OUTCOME’S MAPPING WITH PROGRAM OUTCOME’S

2005PC62 PO 1 PO 2 PO 3 PO 4 PO 5 PO 6 PO 7 PO 8 PO 9 PO 10 PO 11 PO 12

CO 1 ✔ ✔ ✔ ✔

CO 2 ✔
✔ ✔ ✔

CO 3 ✔ ✔ ✔ ✔
INDEX
Division of Page
Exp. No. List of Experiments
Experiments No.
CPU Scheduling Simulate the following CPU Scheduling Algorithms
Week 1 Algorithms a)Round Robin b)SJF c)FCFS d)Priority 1-10
File Allocation Simulate all the File Allocation Strategies
Week 2 Strategies a)Sequential b)Indexed c)Linked 11-16
Write a C program to simulate the MVT and
Week 3 Memory MFT Memory Management Techniques. 17-20
Management Write a C program to simulate the following
Week 4 Techniques Contiguous Memory Allocation Techniques 21-29
a)Worst fit b)Best fit c)First fit
Simulate all File Organization Techniques
Week 5 File Organization a) Single level directory b) Two level directory c) 30-47
technique Hierarchical d)DAG
Deadlock Write a C program to simulate Bankers
Week 6 Management Algorithm for the purpose of Deadlock Avoidance. 48-53
Techniques
Deadlock Write a C program to simulate Bankers
Week 7 Management Algorithm for the purpose of Deadlock Prevention. 54-59
Techniques
Write a C program to simulate disk scheduling
Disk Scheduling Algorithms 60-66
Week 8 Techniques a)FCFS b)SCAN c)C-SCAN
Write a C program to simulate Page Replacement
Week 9 Page Replacement Algorithms 67-74
Techniques a) FIFO b)LRU c)LFU
Memory Write a C program to simulate Paging Technique of
Week 10 Management Memory Management. 75-77
Techniques
Write a C program to simulate Producer- Consumer
Week 11 problem using semaphore. 78-79
Process
Synchronization Write a C program to simulate the concept of
Week 12 Dining –philosophers problem using semaphore. 80-84
Beyond the
Syllabus
Week 13 Write a C program to simulate Rate-monotonic scheduling 85-86
Week 14 Write C programs to simulate the OPTIMAL page replacement
Algorithms 87-89
Week 15 Write a C program to copy the contents of one file to another using
90
system calls.
Applications
Week 16 Applications of Operating System 91
Project
Week 17 Write C program to simulate the Implementation of Access Matrix. 92-93
Week 18 Write C programs to simulate the following techniques of Memory
Management using Segmentation 94-96
Operating Systems Lab Department of CSE

EXPERIMENT 1
OBJECTIVE
Simulate the following CPU scheduling algorithms
a)Round Robin b)SJF c)FCFS d)Priority
DESCRIPTION
Assume all the processes arrive at the same time.
a) ROUND ROBIN CPU SCHEDULING ALGORITHM
For round robin scheduling algorithm, read the number of processes/jobs in the system,
their CPU burst times, and the size of the time slice. Time slices are assigned to each
process in equal portions and in circular order, handling all processes execution. This
allows every process to get an equal chance. Calculate the waiting time and turnaround
time of each of the processes accordingly.
Aim: Write a C program to implement the various process scheduling mechanisms such
as Round Robin Scheduling.
Algorithm for RR
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue and time quantum (or) time
slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
Step 4: Calculate the no. of time slices for each process where No. of time slice for
process(n) = burst time process(n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q,
Calculate
(a) Waiting time for process(n) = waiting time of process(n-1)+ burst time of process(n-
1 ) + the time difference in getting the CPU from process(n-1)
(b) Turn around time for process(n) = waiting time of process(n) + burst time of
process(n)+ the time difference in getting CPU from process(n).
Step 7: Calculate
(a) Average waiting time = Total waiting Time / Number of process
(b) Average Turnaround time = Total Turnaround Time / Number of process
Step 8: Stop the process

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 1
Operating Systems Lab Department of CSE

SOURCE CODE:
#include<stdio.h>
void 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
{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++)
{
wa[i]=tat[i]-ct[i];

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 2
Operating Systems Lab Department of CSE

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("\n\tPROCESS\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]);
}

INPUT:
Enter the no of processes – 3
Enter Burst Time for process 1 – 24
Enter Burst Time for process 2 –3
Enter Burst Time for process 3 –3

Enter the size of time slice – 3

OUTPUT:
The Average Turnaround time is – 15.666667
The Average Waiting time is -- 5.666667

PROCESS BURST TIME WAITING TIME TURNAROUND


TIME
1 24 6 30
2 3 4 7
3 3 7 10

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 3
Operating Systems Lab Department of CSE

b) SJF CPU SCHEDULING ALGORITHM


For SJF scheduling algorithm, read the number of processes/jobs in the system, their
CPU burst times. Arrange all the jobs in order with respect to their burst times. There
may be two jobs in queue with the same execution time, and then FCFS approach is to
be performed. Each process will be executed according to the length of its burst time.
Then calculate the waiting time and turnaround time of each of the processes
accordingly.
Algorithm for SJF
Step 1: Declare the array size.
Step 2: Get the number of elements to be inserted.
Step 3: Select the process which have shortest burst will execute first.
Step 4: If two process have same burst length then FCFS scheduling algorithm used.
Step 5: Make the average waiting the length of next process.
Step 6: Start with the first process from it’s selection as above and let other process to be
in queue.
Step 7: Calculate the total number of burst time.
Step 8: Display the values.
SOURCE CODE:
#include<stdio.h>
void 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;

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 4
Operating Systems Lab Department of CSE

temp=p[i];
p[i]=p[k];
p[k]=temp;
}
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("\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);
}

INPUT
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

OUTPUT
PROCESS BURST TIME WAITING TIME TURNAROUND
TIME
P0 6 3 9
P1 8 16 24
P2 7 9 16
P3 3 0 3
Average Waiting Time --7.000000
Average Turnaround Time --13.000000

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 5
Operating Systems Lab Department of CSE

c) FCFS CPU SCHEDULING ALGORITHM


For FCFS scheduling algorithm, read the number of processes/jobs in the system, their
CPU burst times. The scheduling is performed on the basis of arrival time of the
processes irrespective of their other parameters. Each process will be executed
according to its arrival time. Calculate the waiting time and turnaround time of each of
the processes accordingly.

Algorithm for FCFS


Step 1: Create the number of process.
Step 2: Get the ID and Service time for each process.
Step 3: Initially, Waiting time of first process is zero and Total time for the first process
is the starting time of that process.
Step 4: Calculate the Total time and Processing time for the remaining processes.
Step 5: Waiting time of one process is the Total time of the previous process.
Step 6: Total time of process is calculated by adding Waiting time and Service time.
Step 7: Total waiting time is calculated by adding the waiting time for lack process.
Step 8: Total turn around time is calculated by adding all total time of each process.
Step 9: Calculate Average waiting time by dividing the total waiting time by total
number of process.
Step 10: Calculate Average turn around time by dividing the total time by the number of
process.
Step 11: Display the result.

SOURCE CODE:
#include<stdio.h>
void 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("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 6
Operating Systems Lab Department of CSE

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("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
}

INPUT

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

OUTPUT
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

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 7
Operating Systems Lab Department of CSE

d) PRIORITY CPU SCHEDULING ALGORITHM


For priority scheduling algorithm, read the number of processes/jobs in the system, their
CPU burst times, and the priorities. Arrange all the jobs in order with respect to their
priorities. There may be two jobs in queue with the same priority, and then FCFS
approach is to be performed. Each process will be executed according to its priority.
Calculate the waiting time and turnaround time of each of the processes accordingly.

Algorithm for Priority


STEP 1: Declare the array size.
STEP 2: Get the number of elements to be inserted.
STEP 3: Get the priority for each process and value
STEP 4: Start with the higher priority process from it’s initial position let other process
to be queue.
STEP 5: Calculate the total number of burst time.
STEP 6: Display the values

SOURCE CODE:
#include<stdio.h>
void main( )
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10]; int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10]; clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name,arrivaltime,execution time & priority:");
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 8
Operating Systems Lab Department of CSE

temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
{
st[i]=at[i];wt[i]=st[i]-at[i];ft[i]=st[i]+et[i];ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];wt[i]=st[i]-at[i];ft[i]=st[i]+et[i];ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 9
Operating Systems Lab Department of CSE

INPUT
Enter the number of processes: 3
Enter the Process Name, Arrival Time, execution time & priority: p1 2 3 1
Enter the Process Name, Arrival Time, execution time & priority: p2 4 5 2
Enter the Process Name, Arrival Time, execution time & priority: p3 5 6 3
OUTPUT
PROCESS ARRIVAL TIME EXECUTION TIME PRIORITY A TIME

P1 2 3 1 3

P2 4 5 2 16

P3 5 6 3 11

Average Waiting Time: 2.0000


Average Turn Around Time: 6.6667

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 10
Operating Systems Lab Department of CSE

EXPERIMENT 2
OBJECTIVE
Simulate all the file Allocation strategies
a)Sequential b)Indexed c)Linked

a) SEQUENTIAL FILE ALLOCATION


In this file organization, the records of the file are stored one after another both
physically and logically. That is, record with sequence number 16 is located just after the
15th record. A record of a sequential file can only be accessed by reading all the
previous records.

AIM: Write a C Program to implement Sequential File Allocation method.

ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size for each process.
Step 4: First fit algorithm searches all the entire memory block until a hole which is big
enough is encountered. It allocates that memory block for the requesting process.
Step 5: Best-fit algorithm searches the memory blocks for the smallest hole which can
be allocated to requesting process and allocates if.
Step 6: Worst fit algorithm searches the memory blocks for the largest hole and allocates
it to the process.
Step 7: Analyses all the three memory management techniques and display the best
algorithm which utilizes the memory resources effectively and efficiently.
Step 8: Stop the program.

SOURCE CODE:
#include<stdio.h>
void main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 11
Operating Systems Lab Department of CSE

scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
}

INPUT
Enter no.of files: 2
Enter no. of blocks occupied by file1 : 5
Enter the starting block of file1 : 0
Enter no. of blocks occupied by file2 : 3
Enter the starting block of file2 : 2

OUTPUT
Filename Start block length
1 0 5
2 2 3
Enter file number: 1
File name is:1 length is:5 2 blocks occupied : 0 1 2 3 4

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 12
Operating Systems Lab Department of CSE

b) INDEXED FILE ALLOCATION

Indexed file allocation strategy brings all the pointers together into one location: an
index block. Each file has its own index block, which is an array of disk-block
addresses. The ith entry in the index block points to the ith block of the file. The directory
contains the address of the index block. To find and read the ith block, the pointer in the
ith index-block entry is used.

AIM: Write a C Program to implement Indexed File Allocation method.

Algorithm:
Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any consumer. If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required
Step 11: Terminate the process.

SOURCE CODE:
#include<stdio.h>
void main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 13
Operating Systems Lab Department of CSE

scanf("%d",&b[i][j]);
}
printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}
printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
}

INPUT AND OUTPUT:


Enter no. of files:2
Enter starting block and size of file1: 3 2
Enter blocks occupied by file1: 3
enter blocks of file1: 2
32
Enter starting block and size of file2: 2 2
Enter blocks occupied by file2: 2
enter blocks of file2: 2 2
File index length
1 3 3
2 2 2
Enter file name: 2
file name is: 2
Index is: 2
Block occupied are: 2 2

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 14
Operating Systems Lab Department of CSE

c) LINKED FILE ALLOCATION

With linked allocation, each file is a linked list of disk blocks; the disk blocks may be
scattered anywhere on the disk. The directory contains a pointer to the first and last
blocks of the file. Each block contains a pointer to the next block.
AIM: Write a C Program to implement Linked File Allocation method.

ALGORITHM:
Step 1: Create a queue to hold all pages in memory
Step 2: When the page is required replace the page at the head of the queue
Step 3: Now the new page is inserted at the tail of the queue
Step 4: Create a stack
Step 5: When the page fault occurs replace page present at the bottom of the stack
Step 6: Stop the allocation.
SOURCE CODE:
#include<stdio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 15
Operating Systems Lab Department of CSE

scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
}
INPUT:
Enter no. of files: 2
Enter file name: venkat
Enter starting block: 20
Enter no.of blocks: 6
Enter block numbers: 4
12 15 45 32 25
Enter file name: rajesh
Enter starting block: 12
Enter no.of blocks: 5
Enter block numbers: 6 5 4 3 2
OUTPUT:
File start size block
venkat 20 6 4--->12--->15--->45--->32--->25
rajesh 12 5 6--->5--->4--->3--->2

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 16
Operating Systems Lab Department of CSE

EXPERIMENT-3
OBJECTIVE

Write a C program to simulate the MVT and MFT memory management


techniques.

MULTIPROGRAM VARIABLE TASK(MVT)


MVT stands for multiprogramming with variable number of tasks. Multiprogramming is
a technique to execute number of programs simultaneously by a single processor. This is
one of the memory management techniques. To eliminate the same of the problems with
fixed partitions, an approach known as dynamic partitioning developed. In this
technique, partitions are created dynamically, so that each process is loaded into
partition of exactly the same size at that process. This scheme suffering from external
fragmentation.

AIM: Write a program to implement Dynamic allocation of memories in MVT.

ALGORITHM:
Step1: start the process.
Step2: Declare variables.
Step3: Enter total memory size.
Step4: Allocate memory for os.
Step5: allocate total memory to the pages.
Step6: Display the wastage of memory.
Step7: Stop the process.

SOURCE CODE:
#include<stdio.h>
void main()
{
int i,m,n,tot,s[20];
printf("Enter total memory size:");
scanf("%d",&tot);
printf("Enter no. of pages:");
scanf("%d",&n);
printf("Enter memory for OS:");
scanf("%d",&m);

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 17
Operating Systems Lab Department of CSE

for(i=0;i<n;i++)
{
printf("Enter size of page%d:",i+1);
scanf("%d",&s[i]);
}
tot=tot-m;
for(i=0;i<n;i++)
{
if(tot>=s[i])
{
printf("Allocate page %d\n",i+1);
tot=tot-s[i];
}
else
printf("process p%d is blocked\n",i+1);
}
printf("External Fragmentation is=%d",tot);
}

INPUT:
Enter total memory size : 50
Enter no.of pages : 4
Enter memory for OS : 10
Enter size of page : 10
Enter size of page : 9
Enter size of page : 9
Enter size of page : 10
OUTPUT:
External Fragmentation is = 2

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 18
Operating Systems Lab Department of CSE

MULTIPROGRAM FIXED TASK(MFT)

MFT stands for multiprogramming with fixed no of tasks.MFT is the one of the memory
management technique. In this technique, main memory is divided into no of static
partitions at the system generated time. A process may be loaded into a partition of equal
or greater size. The partition sizes are depending on o.s. in this memory management
scheme the o.s occupies the low memory, and the rest of the main memory is available
for user space. This scheme suffers from internal as well as external fragmentation

AIM: Write a program to implement Dynamic allocation of memories in MFT.

Algorithm:
Step1: start the process.
Step2: Declare variables.
Step3: Enter total memory size.
Step4: Allocate memory for os.
Step5: allocate total memory to the pages.
Step6: Display the wastage of memory.
Step7: Stop the process.

SOURCE CODE:
#include<stdio.h>
void main()
{
int ms,i,ps[20],n,size,p[20],s,intr=0;
printf("Enter size of memory:");
scanf("%d",&ms);
printf("Enter memory for OS:");
scanf("%d",&s);
ms-=s;
printf("Enter no.of partitions to be divided:");
scanf("%d",&n);
size=ms/n;
for(i=0;i<n;i++)
{
printf("Enter process and process size");
scanf("%d%d",&p[i],&ps[i]);

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 19
Operating Systems Lab Department of CSE

if(ps[i]<=size)
{
intr=intr+size-ps[i];
printf("process%d is allocated\n",p[i]);
}
else
printf("process%d is blocked",p[i]);
}
printf("total fragmentation is %d",intr);
}

INPUT:
Enter total memory size: 50
Enter memory for OS : 10
Enter no. of partitions to be divided: 4
Enter process and process size: 1 10
Process 1 is allocated.
Enter process and process size: 2 9
Process 2 is allocated.
Enter process and process size: 3 9
Process 3 is allocated.
Enter process and process size: 4 8
Process 4 is allocated.

OUTPUT:
Internal Fragmentation is = 4

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 20
Operating Systems Lab Department of CSE

EXPERIMENT 4
OBJECTIVE
Write a C program to simulate the following contiguous memory allocation techniques
a)Worst fit b)Best fit c)First fit

a) WORST FIT
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.
Algorithm:
Step 1- Input memory blocks and processes with sizes.
Step 2- Initialize all memory blocks as free.
Step 3- Start by picking each process and find the maximum block size that can be
assigned to current process i.e., find max(bockSize[1], blockSize[2], ...... blockSize[n]) >
processSize[current], if found then assign it to the current process.
Step 4- If not then leave that process and keep checking the further processes.

SOURCE CODE:
#include<stdio.h>
int main()
{
int fragments[10], blocks[10], files[10];
int m, n, number_of_blocks, number_of_files, temp, top = 0;
static int block_arr[10], file_arr[10];
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d",&number_of_blocks);
printf("Enter the Total Number of Files:\t");
scanf("%d",&number_of_files);
printf("\nEnter the Size of the Blocks:\n");

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 21
Operating Systems Lab Department of CSE

for(m = 0; m < number_of_blocks; m++)


{
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &blocks[m]);
}
printf("Enter the Size of the Files:\n");
for(m = 0; m < number_of_files; m++)
{
printf("File No.[%d]:\t", m + 1);
scanf("%d", &files[m]);
}
for(m = 0; m < number_of_files; m++)
{
for(n = 0; n < number_of_blocks; n++)
{
if(block_arr[n] != 1)
{
temp = blocks[n] – files[m];

if(temp >= 0)
{
if(top < temp)
{
file_arr[m] = n;
top = temp;
}
}
}
fragments[m] = top;
block_arr[file_arr[m]] = 1;
top = 0;
}
}
printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment");

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 22
Operating Systems Lab Department of CSE

for(m = 0; m < number_of_files; m++)


{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m],
blocks[file_arr[m]], fragments[m]);
}
printf("\n");
return 0;
}

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

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 23
Operating Systems Lab Department of CSE

b )BEST FIT

Best-fit strategy chooses the block that is closest in size to the request.

Algorithm:

Step 1- Input memory blocks and processes with sizes.


Step 2- Initialize all memory blocks as free.
Step 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.
Step 4- If not then leave that process and keep checking the further processes.

SOURCE CODE:
#include<stdio.h>
int main()
{
int fragments[10], block[10], file[10], m, n, number_of_blocks, number_of_files,
temp, top = 0;
static int block_arr[10], file_arr[10];
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d", &number_of_blocks);
printf("\nEnter the Total Number of Files:\t");
scanf("%d", &number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m < number_of_blocks; m++)
{
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &block[m]);
}
printf("Enter the Size of the Files:\n");
for(m = 0; m < number_of_files; m++)
{
printf("File No.[%d]:\t", m + 1);
scanf("%d", &file[m]);
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 24
Operating Systems Lab Department of CSE

for(m = 0; m < number_of_files; m++)


{
for(n = 0; n < number_of_blocks; n++)
{
if(block_arr[n] != 1)
{
temp = block[n] – file[m];
if(temp >= 0)
{
if(lowest > temp)
{
file_arr[m] = n;
lowest = temp;
}
}
}
fragments[m] = lowest;
block_arr[file_arr[m]] = 1;
lowest = 10000;
}
}
printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment");
for(m = 0; m < number_of_files && file_arr[m] != 0; m++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",m,file[m],file_arr[m],block[file_arr[m]],fragment
s[m];
}
printf("\n");
return 0;
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 25
Operating Systems Lab Department of CSE

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

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

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 26
Operating Systems Lab Department of CSE

c) FIRST-FIT
First-fit chooses the first available block that is large enough.

Algorithm
Step 1- Input memory blocks with size and processes with size.
Step 2- Initialize all memory blocks as free.
Step 3- Start by picking each process and check if it can be assigned to current block.
Step 4- If size-of-process <= size-of-block if yes then assign and check for next process.
Step 5- If not then keep checking the further blocks.

SOURCE CODE:
#include<stdio.h>
int main()
{
static int block_arr[10], file_arr[10];
int fragments[10], blocks[10], files[10];
int m, n, number_of_blocks, number_of_files, temp;
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d", &number_of_blocks);
printf("Enter the Total Number of Files:\t");
scanf("%d", &number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m < number_of_blocks; m++)
{
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &blocks[m]);
}
printf("Enter the Size of the Files:\n");
for(m = 0; m < number_of_files; m++)
{
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &blocks[m]);
}
printf("Enter the Size of the Files:\n");
for(m = 0; m < number_of_files; m++)
{
printf("File No.[%d]:\t", m + 1);

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 27
Operating Systems Lab Department of CSE

scanf("%d", &files[m]);
}
for(m = 0; m < number_of_files; m++)
{
for(n = 0; n < number_of_blocks; n++)
{
if(block_arr[n] != 1)
{
temp = blocks[n] - files[m];
if(temp >= 0)
{
file_arr[m] = n;
break;
}
}
}
fragments[m] = temp;
block_arr[file_arr[m]] = 1;
}
printf("\nFile Number\tBlock Number\tFile Size\tBlock Size\tFragment");
for(m = 0; m < number_of_files; m++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, file_arr[m], files[m], blocks[file_arr[m]],
fragments[m]);
}
printf("\n");
return 0;
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 28
Operating Systems Lab Department of CSE

INPUT
Enter the Total Number of Blocks:5
Enter the Total Number of Files:4

Enter the Size of the Blocks:


Block No.[1]:5
Block No.[2]:4
Block No.[3]:3
Block No.[4]:6
Block No.[5]:7
Enter the Size of the Files:
File No.[1]:1
File No.[2]: 3
File No.[3]: 5
File No.[4]: 3

OUTPUT
File Number Block Number File Size Block Size Fragment
0 0 1 5 4
1 1 3 4 1
2 3 5 6 1
3 2 3 3 0

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 29
Operating Systems Lab Department of CSE

EXPERIMENT 5
OBJECTIVE
Simulate all File Organisation Techniques
a) Single level directory b) Two level directory c) Hierarchical d)DAG

a) SINGLE LEVEL DIRECTORY


Single Level Directories: It is the simplest of all directory structures, in this the directory
system having only one directory, it consisting of the all files. Sometimes it is said to be
root directory. The following dig. Shows single level directory that contains four files
(A, B, C, D).
Aim:To write a C program to implement File Organization concept using the technique
Single level directory.

ALGORITHM:
Step 1: Start the Program
Step 2: Initialize values gd=DETECT,gm,count,i,j,mid,cir_x.
Step 3: Initialize graph function
Step 4: Set back ground color with setbkcolor();
Step 5: Read number of files in variable count.
Step 6: check i<count; mid=640/count;
Step 7: Stop the execution

SOURCE CODE:
#include<stdio.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);

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 30
Operating Systems Lab Department of CSE

while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n4. Display Files\t5. Exit\nEnter
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);
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;
}
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 31
Operating Systems Lab Department of CSE

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);
}
}
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 32
Operating Systems Lab Department of CSE

OUTPUT
Enter name of directory -- CSE
1. Create File
2. Delete File
3. Search File
4. Display Files
5. Exit

Enter your choice – 1


Enter the name of the file – A

1. Create File
2. Delete File
3. Search File
4. Display Files
5. Exit

Enter your choice – 1


Enter the name of the file -- B

1. Create File
2. Delete File
3. Search File
4. Display Files
5. Exit

Enter your choice – 1


Enter the name of the file -- C

1. Create File
2. Delete File
3. Search File
4. Display Files
5. Exit

Enter your choice – 4


The Files are -- A B C

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 33
Operating Systems Lab Department of CSE

1. Create File
2. Delete File
3. Search File
4. Display Files
5. Exit
Enter your choice – 3
Enter the name of the file – ABC
File ABC not found
1. Create File
2. Delete File
3. Search File
4. Display Files
5. Exit

Enter your choice – 2


Enter the name of the file – B
File B is deleted

1. Create File
2. Delete File
3. Search File
4. Display Files
5. Exit

Enter your choice – 5

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 34
Operating Systems Lab Department of CSE

b) TWO LEVEL DIRECTORY ORGANIZATION

Two Level Directory: The problem in single level directory is different users may be
accidentally using the same names for their files. To avoid this problem, each user need
a private directory. In this way names chosen by one user don’t interface with names
chosen by a different user. The following dig 2-level directory

Aim:To write a C program to implement File Organization concept using the technique
two level directory.

Algorithm:
Step 1: Start the Program
Step 2: Initialize structure elements
Step 3: Start main function
Step 4: Set variables gd =DETECT, gm;
Step 5: Create structure using create (&root,0,”null”,0,639,320);
Step 6: initgraph(&gd,&gm,”c:\tc\bgi”);
Step 7: Stop the execution

SOURCE CODE:
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
clrscr();
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);

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 35
Operating Systems Lab Department of CSE

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 -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
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--;

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 36
Operating Systems Lab Department of CSE

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)
{
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
{

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 37
Operating Systems Lab Department of CSE

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);
}
}
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 38
Operating Systems Lab Department of CSE

OUTPUT
1. Create Directory
2. Create File
3. Delete File
4. Search File
5. Display
6. Exit

Enter your choice -- 1


Enter name of directory -- DIR1
Directory created
1. Create Directory
2. Create File
3. Delete File
4. Search File
5. Display
6. Exit

Enter your choice -- 1


Enter name of directory -- DIR2
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 – DIR1
Enter name of the file -- A1
File created

1. Create Directory
2. Create File
3. Delete File

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 39
Operating Systems Lab Department of CSE

4. Search File
5. Display
6. Exit

Enter your choice – 2


Enter name of the directory – DIR1
Enter name of the file-- A2
File 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 – DIR2
Enter name of the file-- B1
File created
1. Create Directory
2. Create File
3. Delete File
4. Search File
5. Display
6. Exit

Enter your choice – 5


Directory Files
DIR1 A1 A2
DIR2 B1

1. Create Directory
2. Create File
3. Delete File
4. Search File
5. Display

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 40
Operating Systems Lab Department of CSE

6. Exit

Enter your choice – 4


Enter name of the directory – DIR
Directory not found

Enter your choice – 3


Enter name of the directory – DIR1
Enter name of the file -- A2
File A2 is deleted
1. Create Directory
2. Create File
3. Delete File
4. Search File
5. Display
6. Exit
Enter your choice – 6

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 41
Operating Systems Lab Department of CSE

C) HIERARCHICAL DIRECTORY ORGANIZATION


The two level directories eliminate name conflicts among users but it is not satisfactory
for users but it is not satisfactory for users with a large no of files. To avoid this, create
the subdirectory and load the same type of the files into the subdirectory. So, in this
method each can have as many directories are needed.

Aim:To write a C program to implement File Organization concept using the technique
hierarchical level directory.

Algorithm:
Step 1: Start the Program
Step 2: Define structure and declare structure variables
Step 3: start main and declare variables
Step 4: Check a directory treestructure
Step 5: Display the directory tree in graphical mood
Step 6: Stop the execution

SOURCE CODE:
#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x, y, ftype, lx, rx, nc, level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
display(root);

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 42
Operating Systems Lab Department of CSE

getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i, gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s) : ",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 for file :");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name); scanf("%d",&(*root)>nc);
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)>link[i]),lev+1,(*root)>name,lx+gap*i,lx+gap*i+gap,
lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 43
Operating Systems Lab Department of CSE

{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root !=NULL)
{
for(i=0;i<root->nc;i++)
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root>y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
display(root->link[i]);
}
}

INPUT AND OUTPUT :


Enter Name of dir/file(under root): ROOT
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for ROOT): 2

Enter Name of dir/file(under ROOT): USER1


Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for USER1): 1

Enter Name of dir/file(under USER1): SUBDIR1


Enter 1 for Dir/2 for File: 1

No of subdirectories/files(for SUBDIR1): 2
Enter Name of dir/file(under USER1): JAVA
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for JAVA): 0

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 44
Operating Systems Lab Department of CSE

Enter Name of dir/file(under SUBDIR1): VB


Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for VB): 0

Enter Name of dir/file(under ROOT): USER2


Enter 1 for Dir/2 for File: 1

No of subdirectories/files(for USER2): 2
Enter Name of dir/file(under ROOT): A
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under USER2): SUBDIR2
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for SUBDIR2): 2
Enter Name of dir/file(under SUBDIR2): PPL
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for PPL): 2
Enter Name of dir/file(under PPL): B
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under PPL): C
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under SUBDIR): AI
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for AI): 2
Enter Name of dir/file(under AI): D
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under AI): E
Enter 1 for Dir/2 for File: 2

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 45
Operating Systems Lab Department of CSE

d) DAG
The tree structured directory system doesn't allow the same file to exist in multiple
directories therefore sharing is major concern in tree structured directory system. We can
provide sharing by making the directory an acyclic graph. In this system, two or more
directory entry can point to the same file or sub directory. That file or sub directory is
shared between the two directory entries.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MIN_PER_RANK 1
#define MAX_PER_RANK 5
#define MIN_RANKS 3
#define MAX_RANKS 5
#define PERCENT 30
void main()
{
int i,j,k,nodes=0;
srand(time(NULL));
int ranks=MIN_RANKS+(rand()%(MAX_RANKS-MIN_RANKS+1));
printf("DIRECTED ACYCLIC GRAPH\n");
for(i=1;i<ranks;i++)
{
int new_nodes=MIN_PER_RANK+(rand()%(MAX_PER_RANK-
MIN_PER_RANK+1));
for(j=0;j<nodes;j++)
for(k=0;k<new_nodes;k++)
if((rand()%100)<PERCENT)
printf("%d->%d;\n",j,k+nodes);
nodes+=new_nodes;
}
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 46
Operating Systems Lab Department of CSE

OUTPUT:
DIRECTED ACYCLIC GRAPH

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 47
Operating Systems Lab Department of CSE

EXPERIMENT-6

DEADLOCK AVOIDANCE
In a multiprogramming environment, several processes may compete for a finite number
of resources. A process requests resources; if the resources are not available at that time,
the process enters a waiting state. Sometimes, a waiting process is never again able to
change state, because the resources it has requested are held by other waiting processes.
This situation is called a deadlock. Deadlock avoidance is one of the techniques for
handling deadlocks. This approach requires that the operating system be given in
advance additional information concerning which resources a process will request and
use during its lifetime. With this additional knowledge, it can decide for each request
whether or not the process should wait. To decide whether the current request can be
satisfied or must be delayed, the system must consider the resources currently available,
the resources currently allocated to each process, and the future requests and releases of
each process. Banker’s algorithm is a deadlock avoidance algorithm that is applicable to
a system with multiple instances of each resource type.

Aim:To write a C program to implement bankers algorithm for dead lock avoidance

Algorithm:
Step 1: Start the Program
Step 2: Get the values of resources and processes.
Step 3: Get the avail value.
Step 4: After allocation find the need value.
Step 5: Check whether its possible to allocate. If possible it is safe state
Step 6: If the new request comes then check that the system is in safety or not if we
allow the
request.
Step 7: Stop the execution

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 48
Operating Systems Lab Department of CSE

SOURCE CODE:
#include<stdio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];
int fl;
int i, j, k, p, b, n, r, g, cnt=0, id, newr;
int avail[10],seq[10];
printf("Enter number of processes -- ");
scanf("%d",&n);
printf("Enter number of resources -- ");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("Enter details for P%d",i);
printf("\nEnter allocation\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
printf("Enter Max\t\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
printf("\nEnter Available Resources\t -- \t");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
printf("\nEnter New Request Details -- ");
printf("\nEnter pid \t -- \t");
scanf("%d",&id);
printf("Enter Request for Resources \t -- \t");

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 49
Operating Systems Lab Department of CSE

for(i=0;i<r;i++)
{
scanf("%d",&newr);
f[id].all[i] += newr;
avail[i]=avail[i] - newr;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
}
}
cnt=0;
fl=0;
while(cnt!=n)
{
g=0;
for(j=0;j<n;j++)
{
if(f[j].flag==0)
{
b=0;
for(p=0;p<r;p++)
{
if(avail[p]>=f[j].need[p])
b=b+1;
else
b=b-1;
}
if(b==r)
{
printf("\nP%d is visited",j);
seq[fl++]=j;
f[j].flag=1;

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 50
Operating Systems Lab Department of CSE

for(k=0;k<r;k++)
avail[k]=avail[k]+f[j].all[k];
cnt=cnt+1;
printf("(");
for(k=0;k<r;k++)
printf("%3d",avail[k]);
printf(")");
g=1;
}
}
}
if(g==0)
{
printf("\n REQUEST NOT GRANTED -- DEADLOCK OCCURRED");
printf("\n SYSTEM IS IN UNSAFE STATE");
goto y;
}
}
printf("\nSYSTEM IS IN SAFE STATE");
printf("\nThe Safe Sequence is -- (");
for(i=0;i<fl;i++)
printf("P%d ",seq[i]);
printf(")");
y: printf("\nProcess\t\tAllocation\t\tMax\t\t\tNeed\n");
for(i=0;i<n;i++)
{
printf("P%d\t",i);
for(j=0;j<r;j++)
printf("%6d",f[i].all[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].max[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].need[j]);
printf("\n");
}
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 51
Operating Systems Lab Department of CSE

INPUT
Enter number of processes -- 5
Enter number of resources -- 3
Enter details for P0
Enter allocation – 0 1 0
Enter Max -- 7 5 3

Enter details for P1


Enter allocation -- 2 0 0
Enter Max ---- 3 2 2

Enter details for P2


Enter allocation -- 3 0 2
Enter Max -- 9 0 2

Enter details for P3


Enter allocation -- 2 1 1
Enter Max -- 2 2 2

Enter details for P4


Enter allocation -- 0 0 2
Enter Max -- 4 3 3

Enter Available Resources -- 3 3 2


Enter New Request Details --
Enter pid-- 1
Enter Request for Resources – 1 0 2

OUTPUT
P1 is visited ( 5 3 2)
P3 is visited ( 7 4 3)
P4 is visited ( 7 4 5)
P0 is visited ( 7 5 5)
P2 is visited ( 10 5 7)
SYSTEM IS IN SAFE STATE
The Safe Sequence is -- (P1 P3 P4 P0 P2 )

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 52
Operating Systems Lab Department of CSE

Process Allocation Max Need


P0 0 1 0 7 5 3 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 53
Operating Systems Lab Department of CSE

EXPERIMENT 7
DEADLOCK PREVENTION
Deadlocks can be prevented by preventing at least one of the four required conditions:
 Mutual Exclusion
 Shared resources such as read-only files do not lead to deadlocks.
 Hold and Wait
 To prevent this condition processes must be prevented from holding one or more
resources while simultaneously waiting for one or more others.
 No Preemption
 Preemption of process resource allocations can prevent this condition of deadlocks,
when it is possible.
 Circular Wait
One way to avoid circular wait is to number all resources, and to require that processes
request resources only in strictly increasing ( or decreasing ) order.

Algorithm:
Step 1: Start the program.
Step 2: Get the values of resources and processes.
Step 3: Get the avail value.
Step 4: After allocation find the need value.
Step 5: Check whether its possible to allocate.
Step 6: If it is possible then the system is in safe state.
Step 7: Else system is not in safety state.
Step 8: If the new request comes then check that the system is in safety.
Step 9: or not if we allow the request.
Step 10: stop the program.

SOURCE CODE:
#include< stdio.h>
void main()
{
int allocated[15][15], max[15][15], need[15][15], avail[15], tres[15], work[15],flag[15];
int pno,rno,i,j,prc,count,t,total;
count=0;
printf("\n Enter number of process:");
scanf("%d",&pno);

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 54
Operating Systems Lab Department of CSE

printf("\n Enter number of resources:");


scanf("%d",&rno);
for(i=1;i< =pno;i++)
{
flag[i]=0;
}
printf("\n Enter total numbers of each resources:");
for(i=1;i<= rno;i++)
scanf("%d",&tres[i]);
printf("\n Enter Max resources for each process:");
for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i);
for(j=1;j<= rno;j++)
scanf("%d",&max[i][j]);
}
printf("\n Enter allocated resources for each process:");
for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i);
for(j=1;j<= rno;j++)
scanf("%d",&allocated[i][j]);
}
printf("\n available resources:\n");
for(j=1;j<= rno;j++)
{
avail[j]=0;
total=0;
for(i=1;i<= pno;i++)
{
total+=allocated[i][j];
}
avail[j]=tres[j]-total;
work[j]=avail[j];
printf(" %d \t",work[j]);
}
do

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 55
Operating Systems Lab Department of CSE

{
for(i=1;i<= pno;i++)
{
for(j=1;j<= rno;j++)
{
need[i][j]=max[i][j]-allocated[i][j];
}
}
printf("\n Allocated matrixMax need");
for(i=1;i<= pno;i++)
{
printf("\n");
for(j=1;j<= rno;j++)
{
printf("%4d",allocated[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",max[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",need[i][j]);
}
}
prc=0;

for(i=1;i<= pno;i++)
{
if(flag[i]==0)
{
prc=i;
for(j=1;j<= rno;j++)
{
if(work[j]< need[i][j])

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 56
Operating Systems Lab Department of CSE

{
prc=0;
break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\n Process %d completed",i);
count++;
printf("\n Available matrix:");
for(j=1;j<= rno;j++)
{
work[j]+=allocated[prc][j];
allocated[prc][j]=0;
max[prc][j]=0;
flag[prc]=1;
printf(“%d",work[j]);
}
}
}while(count!=pno&&prc!=0);
if(count==pno)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in an unsafe state!!");
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 57
Operating Systems Lab Department of CSE

INPUT AND OUTPUT


Enter number of process : 5
Enter number of resources : 3
Enter total numbers of each resources: 10 5 7
Enter Max resources for each process:
for process 1: 7 5 3
for process 2: 3 2 2
for process 3: 9 0 2
for process 4: 2 2 2
for process 5: 4 3 3

Enter allocated resources for each process:


for process 1: 0 1 0
for process 2: 3 0 2
for process 3: 3 0 2
for process 4: 2 1 1
for process 5: 0 0 2

available resources: 2 3 0

Allocated matrix Max need


0 1 0 | 7 5 3 | 7 4 3
3 0 2 | 3 2 2 | 0 2 0
3 0 2 | 9 0 2 | 6 0 0
2 1 1 | 2 2 2 | 0 1 1
0 0 2 | 4 3 3 | 4 3 1
Process 2 completed
Available matrix: 5 3 2
Allocated matrix Max need
0 1 0 | 7 5 3| 7 4 3
0 0 0 | 0 0 0| 0 0 0
3 0 2 | 9 0 2| 6 0 0
2 1 1 | 2 2 2| 0 1 1
0 0 2 | 4 3 3| 4 3 1

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 58
Operating Systems Lab Department of CSE

Process 4 completed

Available matrix: 7 4 3
Allocated matrix Max need
0 1 0 | 7 5 3| 7 4 3
0 0 0 | 0 0 0| 0 0 0
3 0 2 | 9 0 2| 6 0 0
0 0 0 | 0 0 0| 0 0 0
0 0 2 | 4 3 3| 4 3 1
Process 1 completed

Available matrix: 7 5 3

Allocated matrix Max need


0 0 0 | 0 0 0| 0 0 0
0 0 0 | 0 0 0| 0 0 0
3 0 2 | 9 0 2| 6 0 0
0 0 0 | 0 0 0| 0 0 0
0 0 2 | 4 3 3| 4 3 1

Process 3 completed

Available matrix: 10 5 5

Allocated matrix Max need


0 0 0 | 0 0 0| 0 0 0
0 0 0 | 0 0 0| 0 0 0
0 0 0 | 0 0 0| 0 0 0
0 0 0 | 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 5 completed

Available matrix: 10 5 7
The system is in a safe state!!

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 59
Operating Systems Lab Department of CSE

EXPERIMENT 8
DISK SCHEDULING
OBJECTIVE
Write a C program to simulate disk scheduling algorithms
a) FCFS b) SCAN c) C-SCAN

a) FCFS

One of the responsibilities of the operating system is to use the hardware efficiently. For
the disk drives, meeting this responsibility entails having fast access time and large disk
bandwidth. Both the access time and the bandwidth can be improved by managing the
order in which disk I/O requests are serviced which is called as disk scheduling. The
simplest form of disk scheduling is, of course, the first-come, first-served (FCFS)
algorithm. This algorithm is intrinsically fair, but it generally does not provide the fastest
service.

Aim: Write a C program to simulate disk scheduling algorithms using First –come,First-
served(FCFS).

SOURCE CODE:
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
scanf("%d",&queue[i]);
printf("Enter the initial head position\n");
scanf("%d",&head);
queue[0]=head;

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 60
Operating Systems Lab Department of CSE

for(j=0;j<=n-1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek%d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}

INPUT:
Enter the max range of disk
200
Enter the size of queue request
8
Enter the queue of disk positions to be read
90 120 35 122 38 128 65 68
Enter the initial head position
50

OUTPUT
Disk head moves from 50 to 90 with seek 40
Disk head moves from 90 to 120 with seek 30
Disk head moves from 120 to 35 with seek 85
Disk head moves from 35 to 122 with seek 87
Disk head moves from 122 to 38 with seek 84
Disk head moves from 38 to 128 with seek 90
Disk head moves from 128 to 65 with seek 63
Disk head moves from 65 to 68 with seek 3
Total seek time is 482
Average seek time is 60.250000

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 61
Operating Systems Lab Department of CSE

b) SCAN
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.
Aim: Write a C program to simulate disk scheduling algorithms using SCAN.
SOURCE CODE:

#include<stdio.h>
int main()
{
int i,j,sum=0,n;
int d[20];
int disk; //loc of head
int temp,max;
int dloc; //loc of disk in array
printf("enter number of location\t");
scanf("%d",&n);
printf("enter position of head\t");
scanf("%d",&disk);
printf("enter elements of disk queue\n");
for(i=0;i<n;i++)
{
scanf("%d",&d[i]);
}
d[n]=disk;
n=n+1;
for(i=0;i<n;i++) // sorting disk locations
{
for(j=i;j<n;j++)
{
if(d[i]>d[j])
{
temp=d[i];
d[i]=d[j];
d[j]=temp;
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 62
Operating Systems Lab Department of CSE

}
}
max=d[n];
for(i=0;i<n;i++) // to find loc of disc in array
{
if(disk==d[i])
{
dloc=i;
break;
}
}
for(i=dloc;i>=0;i--)
{
printf("%d -->",d[i]);
}
printf("0 -->");
for(i=dloc+1;i<n;i++)
{
printf("%d-->",d[i]);
}
sum=disk+max;
printf("\nmovement of total cylinders %d",sum);
return 0;
}

INPUT:
Enter number of location 8
Enter position of head 53
Enter elements of disk queue
98 183 37 122 14 124 65 67
OUTPUT
53 -->37 -->14 -->0 -->65-->67-->98-->122-->124-->183-->
movement of total cylinders 53

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 63
Operating Systems Lab Department of CSE

c) C-SCAN

C-SCAN is a variant of SCAN designed to provide a more uniform wait time. Like
SCAN, C-SCAN moves the head from one end of the disk to the other, servicing
requests along the way. When the head reaches the other end, however, it immediately
returns to the beginning of the disk without servicing any requests on the return trip

Aim: Write a C program to simulate disk scheduling algorithms using C-SCAN.

SOURCE CODE:
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);

if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 64
Operating Systems Lab Department of CSE

for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n", queue[j], queue[j+1], diff);
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 65
Operating Systems Lab Department of CSE

printf("Total seek time is %d\n",seek);


avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}

INPUT:
Enter the max range of disk
200
Enter the initial head position
50
Enter the size of queue request
8
Enter the queue of disk positions to be read
90 120 35 122 38 128 65 68
OUTPUT
Disk head moves from 50 to 65 with seek 15
Disk head moves from 65 to 68 with seek 3
Disk head moves from 68 to 90 with seek 22
Disk head moves from 90 to 120 with seek 30
Disk head moves from 120 to 122 with seek 2
Disk head moves from 122 to 128 with seek 6
Disk head moves from 128 to 200 with seek 72
Disk head moves from 200 to 0 with seek 200
Disk head moves from 0 to 35 with seek 35
Disk head moves from 35 to 38 with seek 3
Total seek time is 388
Average seek time is 48.500000

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 66
Operating Systems Lab Department of CSE

EXPERIMENT 9
PAGE REPLACEMENT
OBJECTIVE
Write a C program to simulate page replacement algorithms
a) FIFO b) LRU c) LFU

a) FIFO
Page replacement is basic to demand paging. It completes the separation between logical
memory and physical memory. With this mechanism, an enormous virtual memory can
be provided for programmers on a smaller physical memory. There are many different page-
replacement algorithms. Every operating system probably has its own replacement scheme.
A FIFO replacement algorithm associates with each page the time when that page was
brought into memory. When a page must be replaced, the oldest page is chosen.
Aim: Write a C program to simulate page replacement algorithms using FIFO

Algorithm:
Step 1: Start the Program
Step 2: Read number of pages n
Step 3: Read number of pages no
Step 4: Read page numbers into an array a[i]
Step 5: Initialize aval[i]=o, to check page hit
Step 6: Print the results.
Step 7: Stop the Process.

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

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 67
Operating Systems Lab Department of CSE

for(i=0;i<f;i++)
m[i]=-1;
printf("\n The Page Replacement Process is -- \n");
for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
if(m[k]==rs[i])
break;
}
if(k==f)
{
m[count++]=rs[i];
pf++;
}

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

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 68
Operating Systems Lab Department of CSE

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
2-0-1
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
0-2-3
0-2-3
0-1-3 PF No. 11
0-1-2 PF No. 12
0-1-2
0-1-2
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

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 69
Operating Systems Lab Department of CSE

b) LRU (Least Recently Used)


If the recent past is used as an approximation of the near future, then the page that has
not been used for the longest period of time can be replaced. This approach is the Least
Recently Used (LRU) algorithm. LRU replacement associates with each page the time
of that page's last use. When a page must be replaced, LRU chooses the page that has
not been used for the longest period of time.
Aim: Write a C program to simulate page replacement algorithms using LRU
Algorithm:
Step 1: Start the Program
Step 2: Declare the size.
Step 3: Get the number of pages to be issued.
Step 4: Declare counter and stack
Step 5: Select the LRU page by counter value
Step 6: Stack them according the selection
Step 7: Stop the execution
SOURCE CODE:
#include<stdio.h>
main()
{
int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0, next=1;
printf("Enter the length of reference string -- ");
scanf("%d",&n);
printf("Enter the reference string -- ");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter the number of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
m[i]=-1;
}
printf("\nThe Page Replacement process is -- \n");
for(i=0;i<n;i++)
{

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 70
Operating Systems Lab Department of CSE

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);
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 71
Operating Systems Lab Department of CSE

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
2-0-1
2-0-3 PF No. -- 5
2-0-3
4-0-3 PF No. -- 6
4-0-2 PF No. -- 7
4-3-2 PF No. -- 8
0-3-2 PF No. -- 9
0-3-2
0-3-2
1-3-2 PF No. -- 10
1-3-2
1-0-2 PF No. -- 11
1-0-2
1-0-7 PF No. -- 12
1-0-7
1-0-7
The number of page faults using LRU are 12

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 72
Operating Systems Lab Department of CSE

c) LFU(Least frequently used)


Least frequently used (LFU) page-replacement algorithm requires that the page with the
smallest count be replaced. The reason for this selection is that an actively used page
should have a large reference count.
Aim: Write a C program to simulate page replacement algorithms using LFU
SOURCE CODE:
#include<stdio.h>
main()
{
int rs[50], i, j, k, m, f, cntr[20], a[20], min, pf=0;
printf("\nEnter number of page references -- ");
scanf("%d",&m);
printf("\nEnter the reference string -- ");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0;
a[i]=-1;
}
printf("\nThe Page Replacement Process is – \n");

for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;
}
if(j==f)
{
min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 73
Operating Systems Lab Department of CSE

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);
}

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-1 PF No. 1
1-2-1-1 PF No. 2
1-2-3-1 PF No. 3
1-2-3-4 PF No. 4
5-2-3-4 PF No. 5
5-2-3-4 PF No. 5
5-2-3-4 PF No. 5
5-2-3-4 PF No. 5
5-2-3-4 PF No. 5
5-2-1-4 PF No. 6
Total number of page faults – 6

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 74
Operating Systems Lab Department of CSE

EXPERIMENT 10
PAGING
OBJECTIVE
Write a C program to simulate paging technique of memory management.

DESCRIPTION
In computer operating systems, paging is one of the memory management schemes by
which a computer stores and retrieves data from the secondary storage for use in main
memory. In the paging memory-management scheme, the operating system retrieves
data from secondary storage in same-size blocks called pages. Paging is a memory-
management scheme that permits the physical address space a process to be
noncontiguous. The basic method for implementing paging involves breaking physical
memory into fixed-sized blocks called frames and breaking logical memory into blocks
of the same size called pages. When a process is to be executed, its pages are loaded into
any available memory frames from their source.

Aim: Write a C program to simulate paging technique of memory management.

Algorithm:
Step 1: Read all the necessary input from the keyboard.
Step 2: Pages – Logical memory is broken into fixed- sized blocks.
Step 3: Frames – physical memory is broken into fixed – sized blocks
Step 4: Calculate the physical address using the following Physical address=(frame
number * Frame size)+ offset
Step 5: Display the physical address.
Step 6: Stop the execution

SOURCE CODE:

#include<stdio.h>
main()
{
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;
int s[10], fno[10][20];
printf("\nEnter the memory size -- ");
scanf("%d",&ms);
printf("\nEnter the page size -- ");

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 75
Operating Systems Lab Department of CSE

scanf("%d",&ps);
nop = ms/ps;
printf("\nThe no. of pages available in memory are -- %d ",nop);
printf("\nEnter number of processes -- ");
scanf("%d",&np);
rempages = nop;
for(i=1;i<=np;i++)
{
printf("\nEnter no. of pages required for p[%d]-- ",i);
scanf("%d",&s[i]);
if(s[i] >rempages)
{
printf("\nMemory is Full");
break;
}
rempages = rempages - s[i];
printf("\nEnter pagetable for p[%d] --- ",i);
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
}
printf("\nEnter Logical Address to find Physical Address ");
printf("\nEnter process no. and pagenumber and offset -- ");
scanf("%d %d %d",&x,&y, &offset);
if(x>np || y>=s[i] || offset>=ps)
printf("\nInvalid Process or Page Number or offset");
else
{
pa=fno[x][y]*ps+offset;
printf("\nThe Physical Address is -- %d",pa);
}
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 76
Operating Systems Lab Department of CSE

INPUT:
Enter the memory size -- 1000
Enter the page size -- 100
The no. of pages available in memory are -- 10
Enter number of processes -- 3
Enter no. of pages required for p[1] -- 4
Enter pagetable for p[1] --- 8 6 9 5

Enter no. of pages required for p[2] -- 5


Enter pagetable for p[2] --- 14573
Enter no. of pages required for p[3]-- 5

OUTPUT:
Memory is Full
Enter Logical Address to find Physical Address
Enter process no. and pagenumber and offset -- 2 3 60
The Physical Address is – 760

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 77
Operating Systems Lab Department of CSE

EXPERIMENT 11
PRODUCER-CONSUMER PROBLEM
OBJECTIVE
Write a C program to simulate producer-consumer problem using semaphores.
DESCRIPTION
Producer-consumer problem, is a common paradigm for cooperating processes. A
producer process produces information that is consumed by a consumer process. One
solution to the producer-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. A producer
can produce one item while the consumer is consuming another item. The producer and
consumer must be synchronized, so that the consumer does not try to consume an item
that has not yet been produced.
Aim: Write a C program to simulate producer-consumer problem using semaphores.
Algorithm:
Step 1: Start
Step 2: Define the maximum buffer size.
Step 3:Enter the number of producers and consumers.
Step 4: The producer produces the job and put it in the buffer.
Step 5:The consumer takes the job from the buffer.
Step 6:If the buffer is full the producer goes to sleep.
Step 7:If the buffer is empty then consumer goes to sleep.
Step 8:Stop
SOURCE CODE:
#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. Produce \t 2. Consume \t3. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 78
Operating Systems Lab Department of CSE

switch(choice)

{
case 1: if((in+1)%bufsize==out)
printf("\nBuffer is Full");
else
{
printf("\nEnter the value: ");
scanf("%d", &produce);
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:
1. Produce 2. Consume 3. Exit
Enter your choice: 2
Buffer is Empty
1. Produce 2. Consume 3. Exit
Enter your choice: 1
Enter the value: 100
1. Produce 2. Consume 3. Exit
Enter your choice: 2
The consumed value is 100
1. Produce 2. Consume 3. Exit
Enter your choice: 3

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 79
Operating Systems Lab Department of CSE

EXPERIMENT 12
Dining-Philosophers problem
OBJECTIVE
Write a C program to simulate the concept of Dining-Philosophers problem.

DESCRIPTION

The dining-philosophers problem is considered a classic synchronization problem


because it is an example of a large class of concurrency-control problems. It is a simple
representation of the need to allocate several resources among several processes in a
deadlock-free and starvation-free manner. Consider five philosophers who spend their
lives thinking and eating. The philosophers share a circular table surrounded by five
chairs, each belonging to one philosopher. In the center of the table is a bowl of rice, and
the table is laid with five single chopsticks. When a philosopher thinks, she does not
interact with her colleagues. From time to time, a philosopher gets hungry and tries to
pick up the two chopsticks that are closest to her (the chopsticks that are between her
and her left and right neighbors). A philosopher may pick up only one chopstick at a
time. Obviously, she cam1ot pick up a chopstick that is already in the hand of a
neighbor. When a hungry philosopher has both her chopsticks at the same time, she eats
without releasing her chopsticks. When she is finished eating, she puts down both of her
chopsticks and starts thinking again. The dining-philosophers problem may lead to a
deadlock situation and hence some rules have to be framed to avoid the occurrence of
deadlock.
Aim: Write a C program to simulate the concept of Dining-Philosophers problem.

Algorithm:
Step 1: Define the number of philosophers
Step 2: Declare one thread per philosopher
Step 3: Declare one semaphore (represent chopsticks) per philosopher
Step 4:When a philosopher is hungry
1. See if chopsticks on both sides are free
2. Acquire both chopsticks or
. 3. eat
4. restore the chopsticks
5.If chopsticks aren’t free
Step 5:Wait till they are available

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 80
Operating Systems Lab Department of CSE

SOURCE CODE:

#include<stdio.h>
int tph, philname[20], status[20], howhung, hu[20], cho;
main()
{
int i;
printf("\n\nDINING PHILOSOPHER PROBLEM");
printf("\nEnter the total no. of philosophers: ");
scanf("%d",&tph);
for(i=0;i<tph;i++)
{
philname[i] = (i+1);
status[i]=1;
}
printf("How many are hungry : ");
scanf("%d", &howhung);
if(howhung==tph)
{
printf("\nAll are hungry..\nDead lock stage will occur");
printf("\nExiting..");
}
else
{
for(i=0;i<howhung;i++)
{
printf("Enter philosopher %d position: ",(i+1));
scanf("%d", &hu[i]);
status[hu[i]]=2;
}
do
{
printf("1.One can eat at a time\t2.Two can eat at a time\t3.Exit\nEnter your choice:");
scanf("%d", &cho);

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 81
Operating Systems Lab Department of CSE

switch(cho)
{
case 1: one();
break;
case 2: two();
break;
case 3: exit(0);
default: printf("\nInvalid option..");
}
}while(1);
}
}
one()
{
int pos=0, x, i;
printf("\nAllow one philosopher to eat at any time\n");
for(i=0;i<howhung; i++, pos++)
{
printf("\nP %d is granted to eat", philname[hu[pos]]);
for(x=pos;x<howhung;x++)
printf("\nP %d is waiting", philname[hu[x]]);
}}
two()
{
int i, j, s=0, t, r, x;

printf("\n Allow two philosophers to eat at same time\n");


for(i=0;i<howhung;i++)
{
for(j=i+1;j<howhung;j++)
{
if(abs(hu[i]-hu[j])>=1&& abs(hu[i]-hu[j])!=4)
{
printf("\n\ncombination %d \n", (s+1));
t=hu[i];
r=hu[j];
s++;

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 82
Operating Systems Lab Department of CSE

printf("\nP %d and P %d are granted to eat", philname[hu[i]],


philname[hu[j]]);
for(x=0;x<howhung;x++)
{
if((hu[x]!=t)&&(hu[x]!=r))
printf("\nP %d is waiting", philname[hu[x]]);
}
}
}

INPUT:
DINING PHILOSOPHER PROBLEM
Enter the total no. of philosophers: 5
How many are hungry : 3
Enter philosopher 1 position: 2
Enter philosopher 2 position: 4
Enter philosopher 3 position: 5

OUTPUT:
1.One can eat at a time
2.Two can eat at a time
3.Exit
Enter your choice: 1

Allow one philosopher to eat at any time


P 3 is granted to eat
P 3 is waiting
P 5 is waiting
P 0 is waiting
P 5 is granted to eat
P 5 is waiting
P 0 is waiting
P 0 is granted to eat
P 0 is waiting
1.One can eat at a time 2.Two can eat at a time 3.Exit

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 83
Operating Systems Lab Department of CSE

Enter your choice: 2


Allow two philosophers to eat at same time
combination 1
P 3 and P 5 are granted to eat
P 0 is waiting
combination 2
P 3 and P 0 are granted to eat
P 5 is waiting
combination 3
P 5 and P 0 are granted to eat
P 3 is waiting
1. One can eat at a time 2.Two can eat at a time 3.Exit
Enter your choice: 3

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 84
Operating Systems Lab Department of CSE

BEYOND THE SYLLABUS


Aim: Write a C program to implement Rate-monotonic scheduling

Source Code:
#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

int n;

float e[20],p[20];

int i;

float ut,u,x,y;

clrscr();

printf("\n Enter Number of Processes :: ");

scanf("%d",&n);

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

clrscr();

printf("\n Enter Execution Time for P%d ::",(i+1));

scanf("%f",&e[i]);

printf("\n Enter Period for P%d ::",(i+1));

scanf("%f",&p[i]);

}//calculate the utilization

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

x=e[i]/p[i];

ut+=x;

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 85
Operating Systems Lab Department of CSE

//calculate value of U

y=(float)n;

y=y*((pow(2.0,1/y))-1);

u=y;

clrscr();

if(ut<u)

printf("\n As %f < %f ,",ut,u);

printf("\n The System is surely Schedulable");

else

printf("\n Not Sure......");

getch();

INPUT AND OUTPUT:

Enter the no of process: 3


Enter the Execution Time for P1 :: 3
Enter Time Period for P1 :: 20
Enter the Execution Time for P2 :: 2
Enter Time Period for P2 :: 5
Enter the Execution Time for P3 :: 2
Enter Time Period for P3 :: 10
As 0.75000 < 0.779763
The system is surely schedulable .

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 86
Operating Systems Lab Department of CSE

AIM: Write C programs to simulate the OPTIMAL page replacement


algorithms

Algorithm:
1. If referred page is already present, increment hit count.
2. If not present, find if a page that is never referenced in future. If such a page
exists, replace this page with new page. If no such page exists, find a page that is
referenced farthest in future. Replace this page with new page.

Source Code:
#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3,
i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter page reference string: ");
for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i){
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i){
flag1 = flag2 = 0;

for(j = 0; j < no_of_frames; ++j){


if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 87
Operating Systems Lab Department of CSE

if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}

if(flag2 == 0){
flag3 =0;
for(j = 0; j < no_of_frames; ++j){
temp[j] = -1;
for(k = i + 1; k < no_of_pages; ++k){
if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}}
for(j = 0; j < no_of_frames; ++j){
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j){
if(temp[j] > max){
max = temp[j];
pos = j;
}}
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 88
Operating Systems Lab Department of CSE

frames[pos] = pages[i];
faults++;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}

INPUT :
Enter number of frames: 3
Enter number of pages: 10
Enter page reference string: 2 3 4 2 1 3 7 5 4 3
OUTPUT:
2 -1 -1
2 3 -1
234
234
134
134
734
534
534
534

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 89
Operating Systems Lab Department of CSE

AIM: Write a C program to copy the contents of one file to another


using system calls.

Source Code:
#include<unistd.h> #include<stdio.h> #include<sys/types.h> #include<fcntl.h>
#include<stdlib.h> #include<string.h>
int main(int args,char *ar[])
{
char *source=ar[1]; char *dest="def.txt";
char *buf=(char *)malloc(sizeof(char)*120); int fd1,fd2;
fd1=open(source,O_CREAT,0744); fd2=open(dest,O_CREAT,0744);
while(read(fd1,buf,120)!=-1)
{
printf("%s",buf);
//printf("Processing\n"); write(fd2,buf,120);
}
printf("Process Done"); close(fd1);
close(fd2);
}
OUTPUT:
$main
Process Done

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 90
Operating Systems Lab Department of CSE

APPLICATION

APPLICATIONS OF OPERATING SYSTEM


Real time operating systems can be used in any event driven system. The key here is
systems cannot consume too much time handling resources. So as you might see them
used in various embedded systems, typically those with sensors that need to execute
tasks whenever a certain variable changes. If a system needs to be:
1. always on
2. event driven
3. providing quick output
Windows embedded CE is a popular rtOS. If it is a practical application you are looking
for then the latest trend is Internet of Things(IoT). Any IoT device is basically running
an rtOS.
Real time operating system are known for -
1.Deterministic- they execute functions in fixed amount of time
2.Correctness- time at which result produced
3.Predictability- all constraints related to timing meet

Application based on classification of real time operating system -

*SOFT RTOS - performance degraded but not destroyed by failure to meet response
time
constraints. Used in Multimedia, interactive video games

*FIRM RTOS - missing more than few deadline, may lead to catastrophe. In robot weed
killer

*Hard RTOS- failure to meet single deadlines may lead to catastrophic failure In aircraft
control system, aviation, nuclear power systems, chemical plants ,life support system.

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 91
Operating Systems Lab Department of CSE

PROJECT 1
AIM: Write C program to simulate the Implementation of Access
Matrix.

Source Code:
#include <stdio.h> #define ROWS 4
#define COLS 3
void array_of_arrays_ver(int arr[][COLS]); /* prototype */ void ptr_to_array_ver(int
(*arr)[COLS]); /* prototype */
int main ()
{
// declare 4x3 array
int matrix[ROWS][COLS] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}};
printf("Printing Array Elements by Array of Arrays Version Function: \n");
array_of_arrays_ver(matrix);
printf("Printing Array Elements by Pointer to Array Version Function: \n");
ptr_to_array_ver(matrix);
return 0;
}
void array_of_arrays_ver(int arr[][COLS])
{
int i, j;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("%d\t", arr[i][j]);

}
printf("\n");
}
}
void ptr_to_array_ver(int (*arr)[COLS])
{
int i, j;

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 92
Operating Systems Lab Department of CSE

for (i = 0; i < ROWS; i++)


{
for (j = 0; j < COLS; j++)
{
printf("%d\t", (*arr)[j]);
}
arr++; printf("\n");
}
}

OUTPUT
Printing Array Elements by Array of Arrays Version Function:

1 2 3
4 5 6
7 8 9
10 11 12

Printing Array Elements by Pointer to Array Version Function:

1 2 3
4 5 6
7 8 9
10 11 12

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 93
Operating Systems Lab Department of CSE

AIM : Write C programs to simulate the following techniques of


memory Management using Segmentation

Source Code:
#include<stdio.h>
struct list
{
int seg;
int base;
int limit;
struct list *next;
} *p;
void insert(struct list *q,int base,int limit,int seg)
{
if(p==NULL)
{
p=malloc(sizeof(Struct list));
p->limit=limit;
p->base=base;
p->seg=seg;
p- >next=NULL;
}
else
{
while(q->next!=NULL)
{
Q=q->next;
Printf(“yes”)
}
q- >next=malloc(sizeof(Struct list));
q->next ->limit=limit;
q->next ->base=base;
q->next ->seg=seg;
q->next ->next=NULL;
}}
}
int find(struct list *q,int seg)
{
while(q->seg!=seg)
{
q=q->next;
}
return q->limit;

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 94
Operating Systems Lab Department of CSE

}
int search(struct list *q,int seg)
{
while(q->seg!=seg)
{
q=q->next;
}
return q->base;
}
main()
{
p=NULL;
int seg,offset,limit,base,c,s,physical;
printf(“Enter segment table/n”);
printf(“Enter -1 as segment value for termination\n”);
do
{
printf(“Enter segment number”);
scanf(“%d”,&seg);
if(seg!=-1)
{
printf(“Enter base value:”);
scanf(“%d”,&base);
printf(“Enter value for limit:”);
scanf(“%d”,&limit);
insert(p,base,lmit,seg);
}}
while(seg!=-1)
printf(“Enter offset:”);
scanf(“%d”,&offset);
printf(“Enter bsegmentation number:”);
scanf(“%d”,&seg);
c=find(p,seg);
s=search(p,seg);
if(offset<c)
{
physical=s+offset;
printf(“Address in physical memory %d\n”,physical);
}
else
{
printf(“error”);
}
}

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 95
Operating Systems Lab Department of CSE

OUTPUT:
$ cc seg.c
$ ./a.out

Enter segment table


Enter -1 as segmentation value for termination
Enter segment number:1
Enter base value:2000
Enter value for limit:100
Enter segment number:2
Enter base value:2500
Enter value for limit:100
Enter segmentation number:-1
Enter offset:90
Enter segment number:2
Address in physical memory 2590

./a.out
Enter segment table
Enter -1 as segmentation value for termination
Enter segment number:1
Enter base value:2000
Enter value for limit:100
Enter segment number:2
Enter base value:2500
Enter value for limit:100
Enter segmentation number:-1
Enter offset:90
Enter segment number:1
Address in physical memory 2090

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN(AUTONOMOUS INSTITUTION-UGC, Govt. of India) Page 96

You might also like