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

Vemu Institute of Technology: Department of Computer Science & Engineering

The program simulates the Round Robin CPU scheduling algorithm. It accepts the number of processes and time quantum as input. It calculates the waiting time and turnaround time for each process. It outputs the burst time, waiting time and turnaround time for each process. It also calculates the average waiting time and average turnaround time.

Uploaded by

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

Vemu Institute of Technology: Department of Computer Science & Engineering

The program simulates the Round Robin CPU scheduling algorithm. It accepts the number of processes and time quantum as input. It calculates the waiting time and turnaround time for each process. It outputs the burst time, waiting time and turnaround time for each process. It also calculates the average waiting time and average turnaround time.

Uploaded by

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

VEMU INSTITUTE OF TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

LAB MANUAL

15A05501-OPERATING SYSTEMS LAB


Regulation – R15
Year / Semester: III / I
INDEX

S. No Name of the Program Page.No

Simulate the following CPU scheduling algorithms


1 1-9
a) Round Robin b) SJF c) FCFS d) Priority

2 Simulate all file allocation strategies 10-16


a) Sequential b) Linked c) Indexed.
3 Simulate MVT and MFT. 17-19

4 Simulate all File Organization Techniques 20-29


a) Single level directory b) Two level c) Hierarchical d) DAG

5 30-32
Simulate Banker‟s Algorithm for Dead Lock Avoidance and Dead Lock Prevention.

6 Simulate all page replacement algorithms 33-41


a) FIFO b) LRU c) LFU
7 Simulate Paging Technique of memory management. 42-47

8 48-49
Simulate how parent and child processes use shared memory and address space

9 Simulate sleeping barber problem 50-52

10 53-55
Simulate dining philosopher„s problem

11 Simulate producer and consumer problem using threads (use java) 56-58

12 59-60
Develop a code to detect a cycle in wait-for graph

13 61-63
Develop a code to convert virtual address to physical address

14 Simulate how operating system allocates frame to process 64-66

15 Simulate the prediction of deadlock in operating system when all the processes announce their 67-69
resource requirement in advance
Additional Programs:
16 70-75
1. Optimal Page Replacement Algorithm.
2. Dekker‟s Algorithm.
3.

4.
JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY ANANTAPUR

B. Tech III-I Sem. (CSE) L T P C

0 0 4 2

15A05501 OPERATING SYSTEMS LABORATORY

Course Objectives:

 To understand the design aspects of operating system

 To solve various synchronization problems

Course out comes:

 Ensure the development of applied skills in operating systems elated a eas.

 Able to write software routines modules or implementing vari us concepts of operating


system.

1. Simulate the following CPU scheduling algorithms

a) Round Robin b) SJF c) FCFS d) Priority

2. Simulate all file allocation strategies a) Sequential b) Indexed c) Linked

3. Simulate MVT and MFT

4. Simulate all File Organization Techniques a) Single level directory b) Two level c)
Hierarchical d) DAG

5. Simulate Bankers Algorithm for Dead Lock Avoidance

6. Simulate Bankers Algorithm for Dead Lock Prevention

7. Simulate all page replacement algorithms a) FIFO b) LRU c) LFU Etc. …

8. Simulate Paging Technique of memory management

9. Control the number of ports opened by the operating system with a) Semaphore b) monitors

10. Simulate how parent and child processes use shared memory and address space

11. Simulate sleeping barber problem


12. Simulate dining philosopher„s problem

13. Simulate producer and consumer problem using threads (use java)

14. Simulate little„s formula to predict next burst time of a process for SJF scheduling

Algorithm.

15. Develop a code to detect a cycle in wait-for graph

16. Develop a code to convert virtual address to physical address

17. Simulate how operating system allocates frame to process

18. Simulate the prediction of deadlock in operating system when all

The processes announce their resource requirement in advance.


EXPERIMENT-1.1

1.1.1 OBJECTIVE:

Simulate the Round Robin CPU scheduling algorithm

1.1.2 PROGRAM LOGIC:

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 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)
(a) Turnaround 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

1.1.3 PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int ts,need[10],wt[10],tat[10],i,j,n,n1;
int bt[10],flag[10],ttat=0,twt=0;
float awt,atat;
clrscr();
printf("\n Round Robbin Scheduling \n");
printf(" Enter the number of processes ");
scanf("%d",&n);
n1=n;
printf(" Enter the time slice ");
scanf("%d",&ts);
for(i=1;i<=n;i++)
{
printf("Enter the burst time for process %d ",i);
scanf("%d",&bt[i]);
need[i]=bt[i];
}
for(i=1;i<=n;i++)
{
flag[i]=1;
wt[i]=0;
}
while(n!=0)
{
for(i=1;i<=n;i++)
{
if(need[i]>=ts)
{
for(j=1;j<=n;j++)

Operating Systems Lab 1 Dept. of CSE, VEMUIT


{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=ts;
}
need[i]-=ts;
if(need[i]==0)
{
flag[i]=0;
n--;
}
}
else
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=need[i];
}
need[i]=0;
n--;
flag[i]=0;
}
}
}
for(i=1;i<=n1;i++)
{
tat[i]=wt[i]+bt[i];
twt=twt+wt[i];
ttat=ttat+tat[i];
}
awt=(float)twt/n1;
atat=(float)ttat/n1;
printf("process bt \t wt \t tat \n");
for(i=1;i<=n1;i++)
printf("%d \t %d \t %d \t %d \n",i,bt[i],wt[i],tat[i]);
printf("avgwt=%f \n\n",awt);
printf("avgtat=%f",atat);
getch();
}

1.1.4 INPUT & OUTPUT:


Round Robbin Scheduling
Enter the number of processes 5
Enter the time slice 7
Enter the burst time for process 1 87
Enter the burst time for process 2 54
Enter the burst time for process 3 65
Enter the burst time for process 4 34
Enter the burst time for process 5 34
process bt wt tat
1 87 165 252
2 54 167 221
3 65 172 237
4 34 133 167
5 34 139 173
avgwt=155.199997
avgtat=210.000000

Operating Systems Lab 2 Dept. of CSE, VEMUIT


EXPERIMENT-1.2

1.2.1 OBJECTIVE

Simulate SJF CPU scheduling algorithm

1.2.2 PROGRAM LOGIC

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest to highest burst time.
Step 5: Set the waiting time of the first process as „0‟ and its turnaround time as its burst time.
Step 6: For each process in the ready queue, calculate
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
(b) Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
Step 6: Calculate
(c) Average waiting time = Total waiting Time / Number of process
(d) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process

1.2.3 PROGRAM :

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp,n,k,h;
int b[10],w[10],t[10],br[10],pr[10];
char p[10];
temp=0;
clrscr();
printf("enter the number of processes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i]=0;
b[i]=0;
w[i]=0;
t[i]=0;
br[i]=0;
pr[i]=0;
}
for(i=0;i<n;i++)
{
printf("Enter the process name %d \n",i);
scanf("%s",&p[i]);
printf("Enter the burst time of process %d \n",i);
scanf("%d",&b[i]);
}
printf("\n");
for(i=0;i<n;i++)
{
t[i]=p[i];
}

Operating Systems Lab 3 Dept. of CSE, VEMUIT


for(i=0;i<n;i++)
{
for(j=0;j<(n-i-1);j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
temp=0;
for(i=0;i<n;i++)
{
pr[i]=temp;
temp=temp+b[i];
}
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{
if(t[i]==p[j])
{
br[i]=b[j];
w[i]=pr[j];
}
}
}
printf("\nprocess\tburst time\twaiting time \tTurnaround time\n");
printf("===================================\n");
for(i=0;i<n;i++)
{
printf("%c\t ",t[i]);
printf("%d\t\t ",br[i]);
printf("%d\t\t ",w[i]);
printf("%d\t",br[i]);
printf("\n");
}
k=0,h=0;
for(i=0;i<n;i++)
{
k=k+w[i];
h=h+br[i];
}
printf("Total waiting time is %d\n",k);
i=k/n;
printf("Average waiting time is %d\n",i);
printf("Total TurnAround Time is %d\n",h);
printf("Average waiting time is %d\n",h/n);
getch();
}

1.2.4 INPUT AND OPUTPUT

Operating Systems Lab 4 Dept. of CSE, VEMUIT


enter the number of processes 3
Enter the process name 0
a
Enter the burst time of process 0
24
Enter the process name 1
b
Enter the burst time of process 1
3
Enter the process name 2
c
Enter the burst time of process 22

process burst time waiting time Turnaround time


===========================================
a 24 5 24
b 3 2 3
c 2 0 2
Total waiting time is 7
Average waiting time is 2
Total Turn Around Time is 29
Average waiting time is 9

EXPERIMENT-1.3

1.3.1 OBJECTIVE

Program to perform FCFS CPU scheduling algorithm

1.3.2 PROGRAM LOGIC

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Set the waiting of the first process as „0‟ and its burst time as its turn around time
Step 5: for each process in the Ready Q calculate
(c) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
(d) Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
Step 6: Calculate
(e) Average waiting time = Total waiting Time / Number of process
(f) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process
1.3.3 PROGRAM :

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp,n,k,h;
int b[10],w[10];
char p[10];
temp=0;
clrscr();
printf("enter the number of processes");
scanf("%d",&n);
for(i=0;i<n;i++)

Operating Systems Lab 5 Dept. of CSE, VEMUIT


{
printf("Enter the process name %d \n",i);
scanf("%s",&p[i]);
printf("Enter the burst time of process %d \n",i);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
w[i]=temp;
temp=temp+b[i];
}
printf("\nprocess\tburst time\twaiting time \tTurnaround time\n");
printf("====================================\n");
for(i=0;i<n;i++)
{
printf("%c\t ",p[i]);
printf("%d\t\t ",b[i]);
printf("%d\t\t ",w[i]);
printf("%d\t",b[i]);
printf("\n");
}
k=0,h=0;
for(i=0;i<n;i++)
{
k=k+w[i];
h=h+b[i];
}
printf("Total waiting time is %d\n",k);
i=k/n;
printf("Average waiting time is %d\n",i);
printf("Total TurnAround Time is %d\n",h);
printf("Average waiting time is %d\n",h/n);
getch();
}
1.3.4 INPUT AND OUTPUT

enter the number of processes3


Enter the process name 0
a
Enter the burst time of process 0
24
Enter the process name 1
b
Enter the burst time of process 1
3
Enter the process name 2
c
Enter the burst time of process 2
3

process burst time waiting time Turnaround time


=======================================
a 24 0 24
b 3 24 3
c 3 27 3
Total waiting time is 51
Average waiting time is 17

Operating Systems Lab 6 Dept. of CSE, VEMUIT


Total TurnAround Time is 30
Average waiting time is 10

EXPERIMENT-1.4

1.4.1 OBJECTIVE

Program to perform priority CPU scheduling algorithm

1.4.2 PROGRAM LOGIC

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Sort the ready queue according to the priority number.
Step 5: Set the waiting of the first process as „0‟ and its burst time as its turn around time
Step 6: For each process in the Ready Q calculate
(e) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
(f) Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
Step 7: Calculate
(g) Average waiting time = Total waiting Time / Number of process
(h) Average Turnaround time = Total Turnaround Time / Number of process
Step 8: Stop the process

1.4.3 PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp,n,k,h;
int b[10],w[10],pr[10];
char p[10];
temp=0;
clrscr();
printf("enter the number of processes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i]=0;
b[i]=0;
w[i]=0;
}
for(i=0;i<n;i++)
{
printf("Enter the process name %d \n",i);
scanf("%s",&p[i]);
printf("Enter the burst time of process %d \n",i);
scanf("%d",&b[i]);
printf("Enter the priority of process %d \n",i);
scanf("%d",&pr[i]);
}
for(j=0;j<n;j++)
{

Operating Systems Lab 7 Dept. of CSE, VEMUIT


for(i=0;i<n;i++)
{
if(pr[i]==j)
{
w[i]=temp;
temp=temp+b[i];
break;
}
}
}
printf("\nprocess\tburst time\twaiting time \tTurnaround time\tpriority\n");
printf("==============================\n");
for(i=0;i<n;i++)
{
printf("%c\t ",p[i]);
printf("%d\t\t ",b[i]);
printf("%d\t\t ",w[i]);
printf("%d\t\t ",b[i]);
printf("%d",pr[i]);
printf("\n");
}
k=0,h=0;
for(i=0;i<n;i++)
{
k=k+w[i];
h=h+b[i];
}
printf("Total waiting time is %d\n",k);
i=k/n;
printf("Average waiting time is %d\n",i);
printf("Total TurnAround Time is %d\n",h);
printf("Average waiting time is %d\n",h/n);
getch();
}

1.4.4 INPUT AND OUTPUT

enter the number of processes3


Enter the process name 0
a
Enter the burst time of process 0
24
Enter the priority of process 0
2
Enter the process name 1
b
Enter the burst time of process 1
3
Enter the priority of process 1
0
Enter the process name 2
c
Enter the burst time of process 2
3
Enter the priority of process 2
1

Operating Systems Lab 8 Dept. of CSE, VEMUIT


process burst time waiting time Turnaround time priority
============================================
a 24 6 24 2
b 3 0 3 0
c 3 3 3 1
Total waiting time is 9
Average waiting time is 3
Total Turn Around Time is 30
Average waiting time is 10

1.5 PRE LAB QUESTIONS


1. Define scheduling
2. Discuss different types of scheduling
3. Differentiate FCFS,SJF
4. How Round Robin cpu scheduling algorithm works?
5. What is throughput and response time?

1.6 LAB ASSIGNMENT


1. In a c program, print the address of a variable and enter into a long loop. Start 3/4 processes of the same program
and observe the printed address values. Try the experiment on different architectures/OSs. Are the addresses same?

2. Write a collection of processes p1, p2, p3 which execute with same PID.

1.7 POST LAB QUESTIONS


1. What is the relationship between threads and processes?
2. What is the function of the ready queue?
3. What is Dispatcher?
4. Why is round robin algorithm considered better than FCFS algorithm

Operating Systems Lab 9 Dept. of CSE, VEMUIT


EXPERIMENT-2.1

2.1.1 OBJECTIVE

Program to perform SEQUENTIAL file allocation technique

2.1.2 PROGRAM LOGIC

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.
2.1.3 PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],s[10],l[10],n,m,c,i,j,k;
clrscr();
printf("Enter the total memory you want");
scanf("%d",&m);
printf("Enter the Total number of files");
scanf("%d",&n);
for(i=0;i<=m;i++)
a[i]=-1;
for(i=0;i<n;i++)
{
printf("Enter the starting block of file %d\n ",(i+1));
scanf("%d",&s[i]);
printf("Enter the length of the file %d\n",(i+1));
scanf("%d",&l[i]);
}
for(i=0;i<n;i++)
{
for(k=s[i],j=0;j<l[i];j++,k++)
{
if(a[k]==-1)
c=0;
else
{
c=1;
break;
}
}
if(c==1)
printf("This block was already filled by file %d\n",(a[k]+1));
if(c==0)
{
for(k=s[i],j=0;j<l[i];j++,k++)

Operating Systems Lab 10 Dept. of CSE, VEMUIT


a[k]=i;
printf("The file %d is filled from %d to %d\n",(i+1),s[i],(s[i]+l[i]-1));
}
}
getch();
}

2.1.4 INPUT AND OUTPUT

Enter the total memory you want35


Enter the Total number of files5
Enter the starting block of file 1
1
Enter the length of the file 1
2
Enter the starting block of file 2
14
Enter the length of the file 2
4
Enter the starting block of file 3
19
Enter the length of the file 3
3
Enter the starting block of file 4
28
Enter the length of the file 4
6
Enter the starting block of file 5
6
Enter the length of the file 5
5
The file 1 is filled from 1 to 2
The file 2 is filled from 14 to 17
The file 3 is filled from 19 to 21
The file 4 is filled from 28 to 33
The file 5 is filled from 6 to 10

EXPERIMENT-2.2

2.2.1 OBJECTIVE

Program to perform Linked file allocation mechanism.

2.2.2 PROGRAM LOGIC

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.

2.2.3 PROGRAM

#include<conio.h>

Operating Systems Lab 11 Dept. of CSE, VEMUIT


#include<stdio.h>
void main()
{
int i,m,n,j,count;
int a[100],s[10],e[10],in[10],inn[10][25];
clrscr();
printf("enter the total memory");
scanf("%d",&m);
for(i=0;i<m;i++)
a[i]=-1;
i=0;
do{
printf("\n to insert file press : 1 else press : 2");
printf(" \n enter your choice: \n ");
scanf("%d",&n);
if(n==1)
{
L1:
printf("Enter the starting block of file %d",(i+1));
scanf("%d",&s[i]);
if(a[s[i]]==0)
{
printf("this block was already filled \n");
goto L1;
}
a[s[i]]=0;
L2:
printf("Enter the ending block of file %d",(i+1));
scanf("%d",&e[i]);
if(a[e[i]]==0)
{
printf("this block was already filled \n");
goto L2;
}
a[e[i]]=0;
printf("enter the number of intermediate blocks of file %d",(i+1));
scanf("%d",&in[i]);
printf("enter the intermediate blicks one by one \n");
for(j=0;j<in[i];j++)
{
scanf("%d",&inn[i][j]);
if(a[inn[i][j]]==0)
{
printf("this block was already filled \n");
printf("enter another block\n");
j--;
continue;
}
a[inn[i][j]]=0;
}
i++;
}
}while(n==1);
count=i;
for(i=0;i<count;i++)
{
printf("the file %d is linked as follows",(i+1));

Operating Systems Lab 12 Dept. of CSE, VEMUIT


printf("%d",s[i]);
for(j=0;j<in[i];j++)
{
printf("-->%d",inn[i][j]);
}
printf("-->%d\n",e[i]);
}
getch();
}

2.2.4 INPUT AND OUTPUT

enter the total memory100


to insert file press : 1 else press : 2
enter your choice:
1
Enter the starting block of file 121
Enter the ending block of file 171
enter the number of intermediate blocks of file 14
enter the intermediate blocks one by one
2
78
77
90
to insert file press : 1 else press : 2
enter your choice:
1
Enter the starting block of file 277
this block was already filled
Enter the starting block of file 221
this block was already filled
Enter the starting block of file 23
Enter the ending block of file 26
enter the number of intermediate blocks of file 22
enter the intermediate blocks one by one
7980
To insert file press: 1 else press: 2
Enter your choice:
2
The file 1 is linked as follows21-->2-->78-->77-->90-->71
The file 2 is linked as follows3-->79-->80-->6

EXPERIMENT-2.3

2.3.1 OBJECTIVE

Program to simulate indexed file allocation algorithm

2.3.2 PROGRAM LOGIC

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

Operating Systems Lab 13 Dept. of CSE, VEMUIT


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.

2.3.3 PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],b[99],m,n,i;
int *p;
for(i=0;i<100;i++)
a[i]=-1;
printf("enter the starting address of file");
scanf("%d",&n);
printf("enter the number of nodes");
scanf("%d",&m);
L1: for(i=0;i<m;i++)
{
printf("enter index %d \n ",(i+1));
scanf("%d",&b[i]);
}
p=&b[0];
a[n]=1;
printf("\n\nfile 1 starting address is: %d\n Your indexed locations are\n",n);

for(i=0;i<m;i++)
{
if(a[b[i]]==-1)
{
a[b[i]]=1;
printf("%d\n",*p);
p=p+1;
}
else
{
printf("same block already allocated %d\n",b[i]);
for(i=0;i<m;i++)
a[b[i]]=-1;
printf("sorry, no vacancy try again!");
goto L1;
}
}
getch();
}

Operating Systems Lab 14 Dept. of CSE, VEMUIT


2.3.4 INPUT AND OUTPUT

enter the starting address of file20


enter the number of nodes5
enter index 1
25
enter index 2
10
enter index 3
90
enter index 4
77
enter index 5
66
file 1 starting address is: 20
Your indexed locations are
25
10
90
77
66

2.5 PRE LAB QUESTIONS


1. Define file.
2. How many types of file allocation methods are there? Briefly explain with
3. example.
4. What file allocation strategy is most appropriate for random access files?
5. In how many ways we can access a file? What are they?
6. What file access pattern is particularly suited to chained file allocation on
disk?

2.6 LAB ASSIGNMENT

1. Write a collection of sufficient no. of processes which carry out the following different types of tasks
independently:

Only computation
Only printf s
Low computation, heavy console output (printfs)
Heavy File I/O

2.7 POST LAB QUESTIONS

1. File systems can support sparse files, what does this mean?
2. Give an example of a scenario that might benefit from a file system supporting an append-only access write.
3. Give a scenario where choosing a large file system block size might be a benefit
4. What file access pattern is particularly suited to chained file allocation on disk

Operating Systems Lab 15 Dept. of CSE, VEMUIT


EXPERIMENT – 3.1

3.1.1 OBJECTIVE

Simulate Multi programming with fixed number of tasks and Multi programming with variable number of tasks.

MFT

3.1.2 PROGRAM LOGIC

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.

Operating Systems Lab 16 Dept. of CSE, VEMUIT


3.1.3 PROGRAM

#include<stdio.h>
#include<conio.h>
main()
{
int ms,i,ps[20],n,size,p[20],s,intr=0;
clrscr();
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]);
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);
getch();
}

3.1.4 INPUT AND OUTPUT

Enter total memory size : 50


Enter memory for OS :10
Enter no.of partitions to be divided:4
Enter size of page : 10
Enter size of page : 9
Enter size of page : 9
Enter size of page : 8
Internal Fragmentation is = 4

EXPERIMENT-3.2

3.2.1 OBJECTIVE

Simulate Multi programming with fixed number of tasks and Multi programming with variable number of tasks

MVT

3.2.2 PROGRAM LOGIC

Step1: start the process.


Step2: Declare variables.

Operating Systems Lab 17 Dept. of CSE, VEMUIT


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.

3.2.3 PROGRAM

#include<stdio.h>
#include<conio.h>
main()
{
int i,m,n,tot,s[20];
clrscr();
printf("Enter total memory size:");
scanf("%d",&tot);
printf("Enter no. of pages:");
scanf("%d",&n);
printf("Enter memory for OS:");
scanf("%d",&m);
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);
getch();
}

3.2.4 INPUT AND OUTPUT

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
External Fragmentation is = 2

3.5 PRE LAB QUESTIONS


1. What is segmentation?
2. Define fragmentation.
3. Explain different types of fragmentations.
4. What are the advantages of external fragmentation?

Operating Systems Lab 18 Dept. of CSE, VEMUIT


5. What is swapping?

3.6 LAB ASSIGNMENT


1. Implement file storage allocation techniques: Contiguous (using array)
2. Implementation of Contiguous allocation techniques:

(a) Worst-Fit
(b) Best-Fit
(c) First-Fit

3.7 POST LAB QUESTIONS

1. What is a drawback of MVT?

2. How many jobs can be run concurrently on MVT?

3. What is the problem of dynamic storage allocation problem?

4. What is Translation look aside buffer?

5. What is hash table?

EXPERIMENT-4.1

4.1.1 OBJECTIVE

Simulate Single level directory File Organization Technique

4.1.2 PROGRAM LOGIC

Step 1: Start the program.


Step 2: Get the name of the directory.
Step 3: get the number of files.
Step 4: Get the name of the each file.
Step 5: Now each file is in the form of filled circle
Step 6: Every file is connected with the given directory
Step 7: Display the connected gragh along with name using graphics

Operating Systems Lab 19 Dept. of CSE, VEMUIT


Step 8: Stop the program.

4.1.3 PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm,count,i,j,mid,cir_x;
char fname[10][20];
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
setbkcolor(GREEN);
puts("Enter no of files do u have?");
scanf("%d",&count);
for(i=0;i<count;i++)
{
cleardevice(); setbkcolor(GREEN);
printf("Enter file %d name",i+1);
scanf("%s",fname[i]);
setfillstyle(1,MAGENTA);
mid=640/count;
cir_x=mid/3;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextjustify(1,1);
outtextxy(320,125,"Root Directory");
setcolor(BLUE);
for(j=0;j<=i;j++,cir_x+=mid)
{
line(320,150,cir_x,250);
fillellipse(cir_x,250,30,30);
outtextxy(cir_x,250,fname[j]);
}
getch();
}
}

4.1.4 INPUT AND OUTPUT

Operating Systems Lab 20 Dept. of CSE, VEMUIT


EXPERIMENT-4.2

4.2.1 OBJECTIVE

Simulate Two level directory File Organization Technique

4.2.2 PROGRAM LOGIC

Step 1: Start the program.


Step 2: Get the name of the directories.
Step 3: get the number of files.
Step 4: Get the name of the each file.
Step 5: Now each file is in the form of fill circle
Step 6: Every file is connected with respective directory
Step 7: Display the connected graph along with name using graphics
Step 8: Stop the program.

4.2.3 PROGRAM
#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,"null",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
display(root);

Operating Systems Lab 21 Dept. of CSE, VEMUIT


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);
if(lev==0||lev==1)
(*root)->ftype=1;
else
(*root)->ftype=2;
(*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)
{
if(lev==0||lev==1)
{
if((*root)->level==0)
printf("How many users");
else
printf("hoe many files");
printf("(for%s):",(*root)->name);
scanf("%d",&(*root)->nc);
}
else (*root)->nc=0;
if((*root)- >nc==0)
gap=rxlx;
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)
{
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);

Operating Systems Lab 22 Dept. of CSE, VEMUIT


}
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]);
}
}}
4.2.4 INPUT AND OUTPUT

EXPERIMENT-4.3

4.3.1 OBJECTIVE

Simulate Hierarchical level directory File Organization Technique

4.3.2 PROGRAM LOGIC

Step 1: Start the program.


Step 2: Get the name of the directories.
Step 3: get the number of files.
Step 4: Get the name of the each file.
Step 5: Now each file is in the form of fill circle
Step 6: Every file is connected with respective directory
Step 7: Display the connected graph along with name in hierarchical way using graphics
Step 8: Stop the program.

4.3.3 PROGRAM
#include<stdio.h>
#include<graphics.h>
struct tree_element

Operating Systems Lab 23 Dept. of CSE, VEMUIT


{
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);
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)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)

Operating Systems Lab 24 Dept. of CSE, VEMUIT


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

4.3.4 INPUT AND OUTPUT

EXPERIMENT-4.4

4.4.1 OBJECTIVE

Simulate DAG File Organization Technique

Operating Systems Lab 25 Dept. of CSE, VEMUIT


4.4.2 PROGRAM LOGIC

Step 1: Start the program.


Step 2: Get the name of the directories.
Step 3: get the number of files.
Step 4: Get the name of the each file.
Step 5: Now each file is in the form of fill circle
Step 6: Every file is connected with respective directory
Step 7: Display the connected graph along with name using graphics
Step 8: Stop the program.

4.4.3 PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.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;
typedef struct
{
char from[20];
char to[20];
}link;
Link L[10];
Int nofl;
node * root;
void main()
{
int gd=DETECT,gm;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
read_links();
clrscr();
initgraph(&gd,&gm,"c:\\tc\\BGI");
draw_link_lines();
display(root);
getch();
closegraph();
}
read_links()
{
int i;
printf("how many links");
scanf("%d",&nofl);
for(i=0;i<nofl;i++)
{
printf("File/dir:");
fflush(stdin);
gets(L[i].from);
printf("user name:");

Operating Systems Lab 26 Dept. of CSE, VEMUIT


fflush(stdin);
gets(L[i].to);
}
}
draw_link_lines()
{
int i,x1,y1,x2,y2;
for(i=0;i<nofl;i++)
{
search(root,L[i].from,&x1,&y1);
search(root,L[i].to,&x2,&y2);
setcolor(LIGHTGREEN);
setlinestyle(3,0,1);
line(x1,y1,x2,y2);
setcolor(YELLOW);
setlinestyle(0,0,1);
}
}
search(node *root,char *s,int *x,int *y)
{
int i;
if(root!=NULL)
{
if(strcmpi(root->name,s)==0)
{
*x=root->x;
*y=root->y;
return;
}
else
{
for(i=0;i<root->nc;i++)
search(root->link[i],s,x,y);
}
}
} 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);

Operating Systems Lab 27 Dept. of CSE, VEMUIT


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

4.4.4 INPUT AND OUTPUT

Operating Systems Lab 28 Dept. of CSE, VEMUIT


4.5 PRE LAB QUESTIONS
1. Define file
2. What are the attributes of a file?
3. Define directory.
4. In how many ways we can implement the directory?
5. Differentiate DAG and other directories

4.6 LAB ASSIGNMENT

1. Write a C program to simulate acyclic graph directory structure?


2. Write a C Program to simulate general graph directory structure?

4.7 POST LAB QUESTIONS

1. Which of the directory structures is efficient? Why?


2. Which directory structure does not provide user-level isolation and protection?
3. What is the advantage of hierarchical directory structure?

Operating Systems Lab 29 Dept. of CSE, VEMUIT


EXPERIMENT – 5

5.1 OBJECTIVE

Program for Bankers algorithm for deadlock avoidance and deadlock prevention.

5.2 PROGRAM LOGIC

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
or not if we allow the request.
Step-9: stop the program.

5.3 PROGRAM

#include<stdio.h>
#include<conio.h>
#define s 15
void main()
{
int n,m,a[5],e[5],c[5][5],r[5][5];
int i,j,k,cou,l,flag,count;
cou=flag=count=0;
printf("enter no of processes");
scanf("%d",&n);
printf("enter no of resource classes");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("\n enter instances of resources class %d "i+1);
scanf("%d",&e[i]);
printf("\n enter free vectors of resources class %d (resources available):",i+1);
scanf("%d",&a[i]);
}
printf("\n enter the current allocation matrix \n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&c[i][j]);
printf("\n enter the request matrix \n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&r[i][j]);
for(l=0;l<n;l++)
{
cou=0;flag=0;
for(i=0;i<n;i++)
{
cou=0;flag=0;
for(j=0;j<m;j++)

Operating Systems Lab 30 Dept. of CSE, VEMUIT


if(a[j]>=r[i][j])
{
cou++;
if(c[i][j]==-1)
flag++;
}
if(cou==m)
{
if(flag!=m)
{
printf("\n process %d is satisfied",i+1);
count++;
for(k=0;k<m;k++)
{
a[k]+=c[i][k];
printf("\n a[%d]=%d \t",k,a[k]);
getch();
c[i][k]=-1;
} } } } }
if(count==n)
printf("\n process sre completed \n");
else
printf("\n unsafe state occurs \n");
getch();}

5.4 INPUT AND OUTPUT

enter no of processes5
enter no of resource classes3
enter instances of resources class 1 10
enter free vectors of resources class 1 (resources available):3
enter instances of resources class 2 5
enter free vectors of resources class 2 (resources available):3
enter instances of resources class 3 7
enter free vectors of resources class 3 (resources available):2
enter the current allocation matrix
010
200
302
211
002
enter the request matrix
743
122
600
011
431
process 2 is satisfied
a[0]=5
a[1]=3
a[2]=2
process 4 is satisfied
a[0]=7
a[1]=4
a[2]=3
process 5 is satisfied
a[0]=7

Operating Systems Lab 31 Dept. of CSE, VEMUIT


a[1]=4
a[2]=5
process 1 is satisfied
a[0]=7
a[1]=5
a[2]=5
process 3 is satisfied
a[0]=10
a[1]=5
a[2]=7
process are completed

5.5 PRE LAB QUESTIONS

1. Is it possible to have a deadlock involving only one process?


2. Define Resource Allocation Graph
3. State Bankers algorithm.
4. Differentiate preemption and No preemption

5.6 LAB ASSIGNMENT

1. Write a program with following specifications:


Command line input: name of a file
- The file contains the initial state of the system as given below (example included in right
column)
#no of resources 4
#no of instances of each resource 2 4 5 3
#no of processes 3
#no of instances of each resource that 1 1 1 1, 2 3 1 2, 2 2 1 3
each processes needs in its lifetime.The rule is that once a process uses a resource instance and returns it, the process no
more needs it.
2. Implementation of resource allocation graph (RAG).
3. Conversion of resource allocation graph (RAG) to wait-for-graph (WFG) for each type of method used for
storing graph.

5.7 POST LAB QUESTIONS


1. What is a Safe State and what is its use in deadlock avoidance?
2. What is deadlock?
3. What are read write locks?
4. What are the necessary conditions for deadlock to occur?

Operating Systems Lab 32 Dept. of CSE, VEMUIT


EXPERIMENT-6.1

6.1.1 OBJECTIVE

Program to perform FIFO page replacement algorithm.

6.1.2 PROGRAM LOGIC

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: When the page fault occurs replace page present at the head of thequeue
Step 5: Stop the program
6.1.3 PROGRAM

#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,n,k,m,r,pagef;
int a[100],b[100];
k=0,pagef=0;
clrscr();
printf("enter the number of pages\n");
scanf("%d",&n);
printf("enter the number of frames in main memory\n");
scanf("%d",&m);
for(i=0;i<m;i++)
b[i]=-1;
for(i=0;i<n;i++)
{
printf("enter the element %d\n",i);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(b[j]==a[i])
{
printf("the page %d is already present in main memory\n",a[i]);
break;
}
}
if(j==m)
{
b[k]=a[i];
k++;
if(k==m)
k=0;
for(r=0;r<m;r++)
printf("%d ",b[r]);
printf("\n");
pagef++;
}
}
printf("number of page faults: %d",pagef);

Operating Systems Lab 33 Dept. of CSE, VEMUIT


getch();
}

6.1.4 INPUT AND OUTPUT

enter the number of pages


20
enter the number of frames in main memory
3
enter the element 0
7
enter the element 1
0
enter the element 2
1
enter the element 3
2
enter the element 4
0
enter the element 5
3
enter the element 6
0
enter the element 7
4
enter the element 8
2
enter the element 9
3
enter the element 10
0
enter the element 11
3
enter the element 12
2
enter the element 13
1
enter the element 14
2
enter the element 15
0
enter the element 16
1
enter the element 17
7
enter the element 18
0
enter the element 19
1
7 -1 -1
7 0 -1
7 0 1
2 0 1
the page 0 is already present in main memory
2 3 1
2 3 0

Operating Systems Lab 34 Dept. of CSE, VEMUIT


4 3 0
4 2 0
4 2 3
0 2 3
the page 3 is already present in main memory
the page 2 is already present in main memory
0 1 3
0 1 2
the page 0 is already present in main memory
the page 1 is already present in main memory
7 1 2
7 0 2
7 0 1
number of page faults: 15

EXPERIMENT-6.2

6.2.1 OBJECTIVE

Program to perform LRU page replacement algorithm

6.2.2 PROGRAM LOGIC

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
6.2.3 PROGRAM

#include<conio.h>
#include<stdio.h>
int lrupage(int w[],int m);
void main()
{
int i,j,n,m,r,pagef,l,h,p;
int a[100],b[100],w[10];
pagef=0;
clrscr();
printf("enter the number of pages\n");
scanf("%d",&n);
printf("enter the number of frames in main memory\n");
scanf("%d",&m);
for(i=0;i<m;i++)
b[i]=-1;
for(i=0;i<n;i++)
{
printf("enter the element %d\n",i);
scanf("%d",&a[i]);
}
for(i=0;i<m;i++)
{
b[i]=a[i];
for(r=0;r<m;r++)

Operating Systems Lab 35 Dept. of CSE, VEMUIT


printf("%d ",b[r]);
printf("\n");
pagef++;
}
for(i=m;i<n;i++)
{
for(j=0;j<m;j++)
{
if(b[j]==a[i])
{
printf("the page %d is already present in main memory\n",a[i]);
break;
}
}
if(j==m)
{
for(l=0;l<m;l++)
{
for(p=i-1;p>=0;p--)
{
if(b[l]==a[p])
{
w[l]=p;
break;
}
}
if(p==n)
{
b[l]=a[i];
break;
}
}
if(l==m)
{
h=lrupage(w,m);
b[h]=a[i];
}
for(r=0;r<m;r++)
printf("%d ",b[r]);
printf("\n");
pagef++;
}
}
printf("number of page faults: %d",pagef);
getch();
}
int lrupage(int w[],int m)
{
int max,k,h=0;
max=w[0];
for(k=1;k<m;k++)
if(w[k]<max)
{
max=w[k];
h=k; }
return h;
}

Operating Systems Lab 36 Dept. of CSE, VEMUIT


6.2.4 INPUT AND OUTPUT

enter the number of pages


20
enter the number of frames in main memory
3
enter the element 0
7
enter the element 1
0
enter the element 2
1
enter the element 3
2
enter the element 4
0
enter the element 5
3
enter the element 6
0
enter the element 7
4
enter the element 8
2
enter the element 9
3
enter the element 10
0
enter the element 11
3
enter the element 12
2
enter the element 13
1
enter the element 14
2
enter the element 15
0
enter the element 16
1
enter the element 17
7
enter the element 18
0
enter the element 19
1
7 -1 -1
7 0 -1
7 0 1
2 0 1
the page 0 is already present in main memory
2 0 3
the page 0 is already present in main memory
4 0 3
4 0 2
4 3 2

Operating Systems Lab 37 Dept. of CSE, VEMUIT


0 3 2
the page 3 is already present in main memory
the page 2 is already present in main memory
1 3 2
the page 2 is already present in main memory
1 0 2
the page 1 is already present in main memory
1 0 7
the page 0 is already present in main memory
the page 1 is already present in main memory
number of page faults: 12

EXPERIMENT-6.3

6.3.1 OBJECTIVE

Program to perform LFU page replacement algorithm

6.4.1 PROGRAM LOGIC

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

6.4.2 PROGRAM

#include<stdio.h>
int main()
{
int f,p;
int pages[50],frame[10],hit=0,count[50],time[50];
int i,j,page,flag,least,minTime,temp;

printf("Enter no of frames : ");


scanf("%d",&f);
printf("Enter no of pages : ");
scanf("%d",&p);

for(i=0;i<f;i++)
{
frame[i]=-1;
}
for(i=0;i<50;i++)
{
count[i]=0;
}
printf("Enter page no : \n");
for(i=0;i<p;i++)
{
scanf("%d",&pages[i]);
}

Operating Systems Lab 38 Dept. of CSE, VEMUIT


printf("\n");
for(i=0;i<p;i++)
{
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
{
if(frame[j]==-1 || frame[j]==pages[i])
{
if(frame[j]!=-1)
{
hit++;
}
flag=0;
frame[j]=pages[i];
break;
}
if(count[least]>count[frame[j]])
{
least=frame[j];
}
}
if(flag)
{
minTime=50;
for(j=0;j<f;j++)
{
if(count[frame[j]]==count[least] && time[frame[j]]<minTime)
{
temp=j;
minTime=time[frame[j]];
}
}
count[frame[temp]]=0;
frame[temp]=pages[i];
}
for(j=0;j<f;j++)
{
printf("%d ",frame[j]);
}
printf("\n");
}
printf("Page hit = %d",hit);
return 0;
}

6.4.3 INPUT AND OUTPUT

enter the number of pages


20
enter the number of frames in main memory
3
enter the element 0
7

Operating Systems Lab 39 Dept. of CSE, VEMUIT


enter the element 1
0
enter the element 2
1
enter the element 3
2
enter the element 4
0
enter the element 5
3
enter the element 6
0
enter the element 7
4
enter the element 8
2
enter the element 9
3
enter the element 10
0
enter the element 11
3
enter the element 12
2
enter the element 13
1
enter the element 14
2
enter the element 15
0
enter the element 16
1
enter the element 17
7
enter the element 18
0
enter the element 19
1
7 -1 -1
7 0 -1
7 0 1
2 0 1
the page 0 is already present in main memory
2 0 3
the page 0 is already present in main memory
2 4 3
the page 2 is already present in main memory
the page 3 is already present in main memory
2 0 3
the page 3 is already present in main memory
the page 2 is already present in main memory
2 0 1
the page 2 is already present in main memory
the page 0 is already present in main memory
the page 1 is already present in main memory
7 0 1
the page 0 is already present in main memory

Operating Systems Lab 40 Dept. of CSE, VEMUIT


the page 1 is already present in main memory
number of page faults: 9

6.5 PRE LAB QUESTIONS

1. What are the different Dynamic Storage-Allocation methods?


2. Under what circumstances do page faults occur?
3. What are local and global page replacements
4. Define latency, transfer and seek time with respect to disk I/O
5. What are demand-paging and pre-paging?

6.6 LAB ASSIGNMENT


1. Write a memory allocator simulator that implements following memory allocation scheme:
a. The calling processes requests memory chunks of needed sizes
b. The returned chunk is treated as contiguous by the process
c. The process may free an allocated chunk

6.7 POST LAB QUESTIONS


1. Define paging.
2. Discuss the advantages and disadvantages of paging
3. Differentiate segmentation and fragmentation
4. Differentiate LFU and LRU
5. Why Optimal page replacement is better than other algorithms?

EXPERIMENT-7.1

Operating Systems Lab 41 Dept. of CSE, VEMUIT


7.1.1 OBJECTIVE
Simulate the Paging Technique of memory management

a) Memory management policy- Paging

7.1.2 PROGRAM LOGIC

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 process.

7.1.3 PROGRAM

#include <stdio.h>
#include <conio.h>
struct pstruct
{
int fno;
int pbit;
}ptable[10];

int pmsize,lmsize,psize,frame,page,ftable[20],frameno;

void info()
{
printf("\n\nMEMORY MANAGEMENT USING PAGING\n\n");
printf("\n\nEnter the Size of Physical memory: ");
scanf("%d",&pmsize);
printf("\n\nEnter the size of Logical memory: ");
scanf("%d",&lmsize);
printf("\n\nEnter the partition size: ");
scanf("%d",&psize);
frame = (int) pmsize/psize;
page = (int) lmsize/psize;
printf("\nThe physical memory is divided into %d no.of frames\n",frame);
printf("\nThe Logical memory is divided into %d no.of pages",page);
}
void assign()
{
int i;
for (i=0;i<page;i++)
{
ptable[i].fno = -1;
ptable[i].pbit= -1;
}
for(i=0; i<frame;i++)
ftable[i] = 32555;
for (i=0;i<page;i++)
{
printf("\n\nEnter the Frame number where page %d must be placed: ",i);
scanf("%d",&frameno);

Operating Systems Lab 42 Dept. of CSE, VEMUIT


ftable[frameno] = i;
if(ptable[i].pbit == -1)
{
ptable[i].fno = frameno;
ptable[i].pbit = 1;
}
}
getch();
printf("\n\nPAGE TABLE\n\n");
printf("PageAddress FrameNo. PresenceBit\n\n");
for (i=0;i<page;i++)
printf("%d\t\t%d\t\t%d\n",i,ptable[i].fno,ptable[i].pbit);
printf("\n\n\n\tFRAME TABLE\n\n");
printf("FrameAddress PageNo\n\n");
for(i=0;i<frame;i++)
printf("%d\t\t%d\n",i,ftable[i]);
}
void cphyaddr()
{
int laddr,paddr,disp,phyaddr,baddr;
getch();
printf("\n\n\n\tProcess to create the Physical Address\n\n");
printf("\nEnter the Base Address: ");
scanf("%d",&baddr);
printf("\nEnter theLogical Address: ");
scanf("%d",&laddr);
paddr = laddr / psize;
disp = laddr % psize;
if(ptable[paddr].pbit == 1 )
phyaddr = baddr + (ptable[paddr].fno*psize) + disp;
printf("\nThe Physical Address where the instruction present: %d",phyaddr);
}
void main()
{
clrscr();
info();
assign();
cphyaddr();
getch();
}

7.1.4 INPUT AND OUTPUT

MEMORY MANAGEMENT USING PAGING


Enter the Size of Physical memory: 16
Enter the size of Logical memory: 8
Enter the partition size: 2
The physical memory is divided into 8 no.of frames
The Logical memory is divided into 4 no.of pages
Enter the Frame number where page 0 must be placed: 5
Enter the Frame number where page 1 must be placed: 6
Enter the Frame number where page 2 must be placed: 7
Enter the Frame number where page 3 must be placed: 2
PAGE TABLE
PageAddress FrameNo. PresenceBit
0 5 1
1 6 1

Operating Systems Lab 43 Dept. of CSE, VEMUIT


2 7 1
3 2 1
FRAME TABLE
FrameAddress PageNo
0 32555
1 32555
2 3
3 32555
4 32555
5 0
6 1
7 2
Process to create the Physical Address
Enter the Base Address: 1000
Enter theLogical Address: 3
The Physical Address where the instruction present: 1013

EXPERIMENT-7.2
7.2.1 OBJECTIVE

To implement the memory management policy-segmentation

7.2.2 PROGRAM LOGIC

Step 1: Start the program.


Step 2: Get the number of segments.
Step 3: get the base address and length for each segment.
Step 4: Get the logical address.
Step 5: check whether the segment number is within the limit, if not display the error message.
Step 6: Check whether the byte reference is within the limit, if not display the error message.
Step 7: Calculate the physical memory and display it.
Step 8: Stop the program
7.2.3 PROGRAM

#include <stdio.h>
#include <conio.h>
#include <math.h>
int sost;
void gstinfo();
void ptladdr();

struct segtab
{
int sno;
int baddr;
int limit;
int val[10];
}st[10];
void gstinfo()
{
int i,j;
printf("\n\tEnter the size of the segment table: ");
scanf("%d",&sost);

for(i=1;i<=sost;i++)
{

Operating Systems Lab 44 Dept. of CSE, VEMUIT


printf("\n\tEnter the information about segment: %d",i);
st[i].sno = i;
printf("\n\tEnter the base Address: ");
scanf("%d",&st[i].baddr);
printf("\n\tEnter the Limit: ");
scanf("%d",&st[i].limit);
for(j=0;j<st[i].limit;j++)
{
printf("Enter the %d address Value: ",(st[i].baddr + j));
scanf("%d",&st[i].val[j]);
}
}
}

void ptladdr()
{
int i,swd,d=0,n,s,disp,paddr;
clrscr();
printf("\n\n\t\t\t SEGMENT TABLE \n\n");
printf("\n\t SEG.NO\tBASE ADDRESS\t LIMIT \n\n");
for(i=1;i<=sost;i++)
printf("\t\t%d \t\t%d\t\t%d\n\n",st[i].sno,st[i].baddr,st[i].limit);
printf("\n\nEnter the logical Address: ");
scanf("%d",&swd);
n=swd;
while (n != 0)
{
n=n/10;
d++;
}

s = swd/pow(10,d-1);
disp = swd%(int)pow(10,d-1);

if(s<=sost)
{
if(disp < st[s].limit)
{
paddr = st[s].baddr + disp;
printf("\n\t\tLogical Address is: %d",swd);
printf("\n\t\tMapped Physical address is: %d",paddr);
printf("\n\tThe value is: %d",( st[s].val[disp] ) );
}
else
printf("\n\t\tLimit of segment %d is high\n\n",s);
}
else
printf("\n\t\tInvalid Segment Address \n");
}
void main()
{
char ch;
clrscr();
gstinfo();
do
{
ptladdr();

Operating Systems Lab 45 Dept. of CSE, VEMUIT


printf("\n\t Do U want to Continue(Y/N)");
flushall();
scanf("%c",&ch);
}while (ch == 'Y' || ch == 'y' );

getch();
}

7.2.4 INPUT AND OUTPUT

Enter the size of the segment table: 3


Enter the information about segment: 1
Enter the base Address: 4
Enter the Limit: 5
Enter the 4 address Value: 11
Enter the 5 address Value: 12
Enter the 6 address Value: 13
Enter the 7 address Value: 14
Enter the 8 address Value: 15
Enter the information about segment: 2
Enter the base Address: 5
Enter the Limit: 4
Enter the 5 address Value: 21
Enter the 6 address Value: 31
Enter the 7 address Value: 41
Enter the 8 address Value: 51
Enter the information about segment: 3
Enter the base Address: 3
Enter the Limit: 4
Enter the 3 address Value: 31
Enter the 4 address Value: 41
Enter the 5 address Value: 41
Enter the 6 address Value: 51

SEGMENT TABLE

SEG.NO BASE ADDRESS LIMIT

1 4 5

2 5 4

3 3 4

Enter the logical Address: 3


Logical Address is: 3
Mapped Physical address is: 3
The value is: 31
Do U want to Continue(Y/N)
SEGMENT TABLE
SEG.NO BASE ADDRESS LIMIT
1 4 5
2 5 4
3 3 4

Enter the logical Address: 1

Operating Systems Lab 46 Dept. of CSE, VEMUIT


Logical Address is: 1
Mapped Physical address is: 4
The value is: 11
Do U want to Continue(Y/N)

7.5 PRE LAB QUESTION

1. Define Memory Partitioning, Paging, Segmentation.


2. In loading programs into memory, what is the difference between load-time
dynamic linking and run-time dynamic linking?
3. Give the functionalities of OS in memory management
4. Why might the direct blocks be stored in the inode itself?
5. List out memory allocation algorithms which was commonly used

7.6 LAB ASSIGNMENT

1. Implement memory allocation algorithms first fit and best fit.


2. Implement different paging techniques

7.7 POST LAB QUESTIONS

1. What is random access time?


2. What is head crash
3. What is storage area network
4. What is NAS
5. What is Disk scheduling

EXPERIMENT-8

Operating Systems Lab 47 Dept. of CSE, VEMUIT


8.1 OBJECTIVE

Simulate how parent and child processes use shared memory and address space.

8.2 PROGRAM LOGIC

Step 1: Pointer Shm points to the shared memory segment


Step 2: Parent forks a child process to run function ClientProcess
Step 3: Two identical copies of address spaces are created, each of which has a variable Shm whose value is a
pointer to the shared memory
Step 4: The child process has already known the location of the shared memory segment and does not have to
use shmget() and shmat()
Step 5: Both the process will share the a to z characters which is resided in shared memory region having same
address space

8.3 PROGRAM

Client program to demonstrate shared memory

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>
#define SHMSZ 27
main()
{
int shmid;
key_t key;
char *shm, *s;
key=5678;
if((shmid=shmget(key,SHMSZ,0666))<0)
{
perror("shmget");
exit(1);
}
if((shm=shmat(shmid,NULL,0))==(char*)-1)
{
perror("shmat");
exit(1);
}
for(s=shm;*s!= NULL;s++)
putchar(*s);
putchar('\n');
*shm='*';
exit(0);
}
SERVER:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>
#define SHMSZ 27
main()

Operating Systems Lab 48 Dept. of CSE, VEMUIT


{
char c;
int shmid;
key_t key;
char*shm,*s;
key=5678;
if((shmid=shmget(key,SHMSZ,IPC_CREAT | 0666))<0)
{
perror("shmget");
exit(1);
}
if((shm=shmat(shmid,NULL,0))==(char*)-1)
{
perror("shmat");
exit(1);
}
s=shm;
for(c='a';c<='z';c++)
*s++=c;
s=NULL;
while(*shm!='*')
sleep(1);
exit(0);
}

8.4 INPUT AND OUTPUT


Sh client.sh
Sh server.sh
abcdefghijklmnopqrstuvwxyz

8.5 PRE-LAB QUESTIONS

1.What is the need for process synchronization?


2.Define a semaphore?
3.Define Readers writers problem?

8.6 LAB ASSIGNMENT

1.Write a C program to simulate Readers Writers problem using message-passing system.

8.7 POST-LAB QUESTIONS

1.Discuss the consequences of considering bounded and unbounded buffers in producer-consumer problem?

2.Can producer and consumer processes access the shared memory concurrently? If not which techniqueprovides
such a benefit?

EXPERIMENT-9

Operating Systems Lab 49 Dept. of CSE, VEMUIT


9.1 OBJECTIVE

Simulate sleeping barber problem

9.2 PROGRAM LOGIC

def Barber():
while true:
wait(custReady)
wait(accessWRSeats)
numberOfFreeWRSeats += 1
signal(barberReady)
signal(accessWRSeats)
# (Cut hair here.)
def Customer():
wait(accessWRSeats)
if numberOfFreeWRSeats > 0:
numberOfFreeWRSeats -= 1
signal(custReady)
signal(accessWRSeats)
wait(barberReady)
else:
signal(accessWRSeats)
# (Leave without a haircut.)

9.3 PROGRAM

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h> // The maximum number of customer threads.
#define MAX_CUSTOMERS 25 // Function prototypes...

void *customer(void *num);


void *barber(void *);
void randwait(int secs);
sem_t waitingRoom;
sem_t barberPillow;
sem_t seatBelt;
int allDone = 0;

int main(int argc, char *argv[])


{
pthread_t btid;
pthread_t tid[MAX_CUSTOMERS];

Operating Systems Lab 50 Dept. of CSE, VEMUIT


int i, x, numCustomers, numChairs; int Number[MAX_CUSTOMERS];
printf("Maximum number of customers can only be 25. Enter number of customers and chairs.\n");
scanf("%d",&x);
numCustomers = x;
scanf("%d",&x);
numChairs = x;
if (numCustomers > MAX_CUSTOMERS) {
printf("The maximum number of Customers is %d.\n", MAX_CUSTOMERS);
system("PAUSE");
return 0;
}
printf("A solution to the sleeping barber problem using semaphores.\n");
for (i = 0; i < MAX_CUSTOMERS; i++) {
Number[i] = i;
}
sem_init(&waitingRoom, 0, numChairs);
sem_init(&barberChair, 0, 1);
sem_init(&barberPillow, 0, 0);
sem_init(&seatBelt, 0, 0);
pthread_create(&btid, NULL, barber, NULL);

for (i = 0; i < numCustomers; i++) {


pthread_create(&tid[i], NULL, customer, (void *)&Number[i]);
}
for (i = 0; i < numCustomers; i++) {
pthread_join(tid[i],NULL);
}
sem_post(&barberPillow); // Wake the barber so he will exit.
pthread_join(btid,NULL);
system("PAUSE");
return 0;
}

void *customer(void *number) {


int num = *(int *)number;
printf("Customer %d leaving for barber shop.\n", num);
randwait(5);
printf("Customer %d arrived at barber shop.\n", num); sem_wait(&waitingRoom);
printf("Customer %d entering waiting room.\n", num); sem_wait(&barberChair);
sem_post(&waitingRoom);
printf("Customer %d waking the barber.\n", num);
sem_post(&barberPillow);
sem_wait(&seatBelt); // Give up the chair.
sem_post(&barberChair);
printf("Customer %d leaving barber shop.\n", num);
}

void *barber(void *junk)


{

while (!allDone) {
printf("The barber is sleeping\n");
sem_wait(&barberPillow);
if (!allDone)
{
printf("The barber is cutting hair\n");
randwait(3);

Operating Systems Lab 51 Dept. of CSE, VEMUIT


printf("The barber has finished cutting hair.\n"); sem_post(&seatBelt);
}
else {
printf("The barber is going home for the day.\n");
}
}
}

void randwait(int secs) {


int len = 1; // Generate an arbit number...
sleep(len);
}

9.4 INPUT AND OUTPUT

A solution to the sleeping barber problem using semaphores.


Maximum number of customers can only be 25.
Enter number of customers and chairs.
4
4
The barber is sleeping
The barber is cutting hair
The barber has finished cutting hair.
Customer 1 leaving for barber shop.
Customer 2 arrived at barber shop.
Customer 2 entering waiting room.
The barber is sleeping
Customer 2 wakingup the barber
The barber is cutting hair
The barber has finished cutting hair.
Customer 2 leaving barber shop.
Customer 3 arrived at barber shop.
Customer 3 entering waiting room.
The barber is sleeping
Customer 3 wakingup the barber
The barber is cutting hair
The barber has finished cutting hair.
9.5 PRE-LAB QUESTIONS
1. Differentiate between a monitor, semaphore and a binary semaphore?
2. Define clearly the Sleeping Barber problem?
3. Define Semaphore.
4. Write Test and set method.

9.6 LAB ASSIGNMENT


1. Write a C program to simulate readers-writers problem using semaphores?
9.7 POST-LAB QUESTIONS
1. Identify the scenarios in the sleeping barber problem that leads to the deadlock situations?
2. Define critical Section.
3. What are three properties to satisfy to overcome critical section problem

EXPERIMENT-10

Operating Systems Lab 52 Dept. of CSE, VEMUIT


10.1 OBJECTIVE

Simulate dining philosopher„s problem.

10.2 PROGRAM LOGIC

process P[i]
while true do
{ THINK;
PICKUP(CHOPSTICK[i], CHOPSTICK[i+1 mod 5]);
EAT;
PUTDOWN(CHOPSTICK[i], CHOPSTICK[i+1 mod 5])
}

10.3 PROGRAM
int tph, philname[20], status[20], howhung, hu[20], cho; main()
{
int i;
clrscr();
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); switch(cho)
{
case 1: one();
break;
case 2: two();
break;
case 3:
exit(0);

Operating Systems Lab 53 Dept. of CSE, VEMUIT


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

10.4 INPUT AND OUTPUT

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: 1Allow 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

Operating Systems Lab 54 Dept. of CSE, VEMUIT


1.One can eat at a time
2.Two can eat at a time
3.Exit
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 c
ombination 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

10.5 PRE-LAB QUESTIONS


1. Differentiate between a monitor, semaphore and a binary semaphore?
2. Define clearly the dining-philosophers problem?
10.6 LAB ASSIGNMENT
1.Write a C program to simulate readers-writers problem using monitors?
10.7 POST-LAB QUESTIONS
1. Identify the scenarios in the dining-philosophers problem that leads to the
deadlock situations?

EXPERIMENT-11

Operating Systems Lab 55 Dept. of CSE, VEMUIT


11.1 OBJECTIVE

Simulate producer and consumer problem using threads (use java).

11.2 PROGRAM LOGIC

Producer algorithm:
while (true)
{
/* produce an item in nextproduced */
while (counter == BUFFER_SIZE) ;
/* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
Consumer algorithm:
while (true)
{
while (counter == 0) ; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}

11.3 PROGRAM

import java.io.*;
import java.lang.*;
class Q
{
int n;
boolean valueSet=false;
synchronized int get()
{
if(!valueSet)
try
{
wait();
}
catch(InterruptedException e){
System.out.println("InterruptedException caught");
}
System.out.println("Got:"+n);
valueSet=false;
notify();
return n;
}
synchronized void put(int n)
{
if(valueSet)
try
{
wait();
}catch(InterruptedException e){

Operating Systems Lab 56 Dept. of CSE, VEMUIT


System.out.println("InterruptedException caught");
}
this.n=n;
valueSet=true;
System.out.println("put:"+n);
notify();
}
}
class Producer implements Runnable
{
Q q;
Producer(Q q)
{
this.q=q;
new Thread(this,"Producer").start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class Consumer implements Runnable
{
Q q;
Consumer(Q q)
{
this.q=q;
new Thread(this,"Consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
class PCFixed
{
public static void main(String args[])
{
Q q=new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press ctrl+c to stop");
}
}

11.4 INPUT AND OUTPUT

Put 1
Get1
Put 2

Operating Systems Lab 57 Dept. of CSE, VEMUIT


Get 2
Put 3
Get 3
.
.
.
.
.
.
.

11.5 PRE-LAB QUESTIONS


1.What is the need for process synchronization?
2.Define a semaphore?
3.Define producer-consumer problem?

11.6 LAB ASSIGNMENT


1. Write a C program to simulate producer-consumer problem using message-passing system.

11.7 POST-LAB QUESTIONS


1.Discuss the consequences of considering bounded and unbounded buffers in producer-consumer problem?
2.Can producer and consumer processes access the shared memory concurrently? If not which techniqueprovides such a
benefit?

EXPERIMENT-12

Operating Systems Lab 58 Dept. of CSE, VEMUIT


12.1 OBJECTIVE

Develop a code to detect a cycle in wait-for graph

12.2 PROGRAM LOGIC

Step 1: Read the vertices and edges of a graph


Step 2: Define a method addEdge to connect the vertices
Step 3: Define a method isCyclicUtil to visit each edge and vertex of a graph
Step 4: Define a method isCyclic to check the graph contains cycle or not
Step 5: Display whether graph forms a cycle or not

12.3 PROGRAM

import java.io.*;
import java.util.*;
class Graph
{
private int V;
private LinkedList<Integer> adj[];
Graph(int v) {
V = v;
adj = new LinkedList[v];
for(int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
void addEdge(int v,int w) {
adj[v].add(w);
adj[w].add(v);
}

Boolean isCyclicUtil(int v, Boolean visited[], int parent)


{
visited[v] = true;
Integer i;
Iterator<Integer> it = adj[v].iterator();
while (it.hasNext())
{
i = it.next();
if (!visited[i])
{
if (isCyclicUtil(i, visited, v))
return true;
}
else if (i != parent)
return true;
}
return false;
}
Boolean isCyclic()
{
Boolean visited[] = new Boolean[V];
for (int i = 0; i < V; i++)
visited[i] = false;
for (int u = 0; u < V; u++)
if (!visited[u])

Operating Systems Lab 59 Dept. of CSE, VEMUIT


if (isCyclicUtil(u, visited, -1))
return true;
return false;
}
public static void main(String args[])
{
Graph g1 = new Graph(5);
g1.addEdge(1, 0);
g1.addEdge(0, 2);
g1.addEdge(2, 0);
g1.addEdge(0, 3);
g1.addEdge(3, 4);
if (g1.isCyclic())
System.out.println("Graph contains cycle");
else
System.out.println("Graph doesn't contains cycle");
Graph g2 = new Graph(3);
g2.addEdge(0, 1);
g2.addEdge(1, 2);
if (g2.isCyclic())
System.out.println("Graph contains cycle");
else
System.out.println("Graph doesn't contains cycle");
}
}

12.3 INPUT AND OUTPUT

javac CheckCycle.java
javaCheckCycle

Graph contains cycle


Graph doesn't contains cycle
12.4 PRE-LAB QUESTIONS

1.Define resource. Give examples.


2.What is deadlock?
3.What are the conditions to be satisfied for the deadlock to occur?

12.6 LAB ASSIGNMENT


1. Implementation of resource allocation graph (RAG).

12.7 POST LAB QUESTIONS


1. What is a Safe State and what is its use in deadlock avoidance?
2. What is deadlock?
3. What are read write locks?
4. What are the necessary conditions for deadlock to occur?

EXPERIMENT-13

Operating Systems Lab 60 Dept. of CSE, VEMUIT


13.1 OBJECTIVE

Develop a code to convert virtual address to physical address

13.2 PROGRAM LOGIC

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 process.

13.3 PROGRAM

#include <stdio.h>
#include <conio.h>
struct pstruct
{
int fno;
int pbit;
}ptable[10];

int pmsize,lmsize,psize,frame,page,ftable[20],frameno;

void info()
{
printf("\n\nMEMORY MANAGEMENT USING PAGING\n\n");
printf("\n\nEnter the Size of Physical memory: ");
scanf("%d",&pmsize);
printf("\n\nEnter the size of Logical memory: ");
scanf("%d",&lmsize);
printf("\n\nEnter the partition size: ");
scanf("%d",&psize);
frame = (int) pmsize/psize;
page = (int) lmsize/psize;
printf("\nThe physical memory is divided into %d no.of frames\n",frame);
printf("\nThe Logical memory is divided into %d no.of pages",page);
}
void assign()
{
int i;
for (i=0;i<page;i++)
{
ptable[i].fno = -1;
ptable[i].pbit= -1;
}
for(i=0; i<frame;i++)
ftable[i] = 32555;
for (i=0;i<page;i++)
{
printf("\n\nEnter the Frame number where page %d must be placed: ",i);
scanf("%d",&frameno);
ftable[frameno] = i;
if(ptable[i].pbit == -1)

Operating Systems Lab 61 Dept. of CSE, VEMUIT


{
ptable[i].fno = frameno;
ptable[i].pbit = 1;
}
}
getch();
printf("\n\nPAGE TABLE\n\n");
printf("PageAddress FrameNo. PresenceBit\n\n");
for (i=0;i<page;i++)
printf("%d\t\t%d\t\t%d\n",i,ptable[i].fno,ptable[i].pbit);
printf("\n\n\n\tFRAME TABLE\n\n");
printf("FrameAddress PageNo\n\n");
for(i=0;i<frame;i++)
printf("%d\t\t%d\n",i,ftable[i]);
}
void cphyaddr()
{
int laddr,paddr,disp,phyaddr,baddr;
getch();
printf("\n\n\n\tProcess to create the Physical Address\n\n");
printf("\nEnter the Base Address: ");
scanf("%d",&baddr);
printf("\nEnter theLogical Address: ");
scanf("%d",&laddr);
paddr = laddr / psize;
disp = laddr % psize;
if(ptable[paddr].pbit == 1 )
phyaddr = baddr + (ptable[paddr].fno*psize) + disp;
printf("\nThe Physical Address where the instruction present: %d",phyaddr);
}
void main()
{
clrscr();
info();
assign();
cphyaddr();
getch();
}

13.4 INPUT AND OUTPUT

MEMORY MANAGEMENT USING PAGING


Enter the Size of Physical memory: 16
Enter the size of Logical memory: 8
Enter the partition size: 2
The physical memory is divided into 8 no.of frames
The Logical memory is divided into 4 no.of pages
Enter the Frame number where page 0 must be placed: 5
Enter the Frame number where page 1 must be placed: 6
Enter the Frame number where page 2 must be placed: 7
Enter the Frame number where page 3 must be placed: 2
PAGE TABLE
PageAddress FrameNo. PresenceBit
0 5 1
1 6 1
2 7 1
3 2 1

Operating Systems Lab 62 Dept. of CSE, VEMUIT


FRAME TABLE
FrameAddress PageNo
0 32555
1 32555
2 3
3 32555
4 32555
5 0
6 1
7 2
Process to create the Physical Address
Enter the Base Address: 1000
Enter theLogical Address: 3
The Physical Address where the instruction present: 1013

13.5 PRE LAB QUESTION

1. Define Memory Partitioning, Paging, Segmentation.


2. In loading programs into memory, what is the difference between load-time dynamic linking and run-time dynamic
linking?
3. Give the functionalities of OS in memory management
4. Why might the direct blocks be stored in the inode itself?
5. List out memory allocation algorithms which was commonly used

13.6 LAB ASSIGNMENT

1. Implement memory allocation algorithms first fit and best fit.


2. Implement different paging techniques

13.7 POST LAB QUESTIONS


1.Define the concept of virtual memory?
2.What is the purpose of page replacement?
3.Define the general process of page replacement?
4.List out the various page replacement techniques?
5.What is page fault?

EXPERIMENT-14

Operating Systems Lab 63 Dept. of CSE, VEMUIT


14.1 OBJECTIVE

Simulate how operating system allocates frame to process.

14.2 PROGRAM LOGIC


Step 1: Read no of process,process names,size of each process,Total
no of frames
Step 2: Read choice 1.Fixed Allocation 2.Proportional Allocation
Step 3: If choice is Fixed Allocation Calculate
Allocation of frames of each process
= Total no of process / No of process
Step 4: If choice is Proportional Allocation Calculate
Allocation of frames to each process =
(size of each process/total size of processes)*total no of frames
Step 5: Display the output

14.3 PROGRAM
#include<stdio.h>
#include<conio.h>
Void main()
{
int i,s[10],S,m,a[10],n,ch,total=0;
char p[10];
printf(“enter total no of process”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“enter the process name %d”,i)
scanf(“%s”,&p[i])
printf(“enter the size of each process %d”,i)
scanf(“%d”,&s[i])
}
printf(“enter total no of frames”);
scanf(“%d”,&m);
printf(“enter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf(“Fixed Allocation”);
for(i=0;i<n;i++)
{
a[i]=m/n;
printf(“Allocation of frames to process %c is %d”,p[i],a[i]);
}
Case 2: printf(“Proportional Allocation”);
for(i=0;i<n;i++)
{
total+=s[i];
a[i]=(s[i]/total)*m;
Printf(“Allocation of frames for process %c is %d”,p[i],a[i]);
}
default: printf(“invalid choice”);
}
}

Operating Systems Lab 64 Dept. of CSE, VEMUIT


14.4 INPUT & OUTPUT:

Enter total no of process: 4

Enter process Names: a

Enter the size of each process: 5

Enter process Names: b

Enter the size of each process: 6

Enter process Names: c

Enter the size of each process: 7

Enter process Names: d

Enter the size of each process: 8

Enter total No of frames: 40

Enter ur choice: 1

Fixed Allocation

Allocation of frames to process a is 10

Allocation of frames to process b is 10

Allocation of frames to process c is 10

Allocation of frames to process d is 10

Enter ur choice 2

Proportional Allocation

Allocation of frames for process a is 7

Allocation of frames for process b is 9

Allocation of frames for process c is 10

Allocation of frames for process d is 12

14.5 PRE-LAB QUESTIONS

1. What are the advantages of noncontiguous memory allocation schemes?

2. What is the process of mapping a logical address to physical address with respect to the paging
memorymanagement technique?

3. Define the terms – base address, offset?

14.6 ASSIGNMENT QUESTIONS

1.Write a C program to simulate two-level paging technique.

Operating Systems Lab 65 Dept. of CSE, VEMUIT


2.Write a C program to simulate segmentation memory management technique

14.7 POST-LAB QUESTIONS

1.Differentiate between paging and segmentation memory allocation techniques?

2.What is the purpose of page table?

3.Whether the paging memory management technique suffers with internal or external fragmentationproblem.
Why?

4.What is the effect of paging on the overall context-switching time?

Operating Systems Lab 66 Dept. of CSE, VEMUIT


EXPERIMENT-15

15.1 OBJECTIVE

Simulate the prediction of deadlock in operating system when all the processes announce their resource requirement in
advance.

15.2 PROGRAM LOGIC


Step 1: Read values for No of processes, No of Resources , Claim Matrix,Available Matrix, Resources vector
Step 2: Calculate no of resources allocated to each process
Step 3: Check whether the claim matrix is greater than availability matrix
Step 4: if it is true NO Allocation will be done
Else Resources will be allocated
Step 5: Check each process which is causing deadlock
If it is found Display deadlock causing process
Else
Requested Resources are allocated to process
15.3 PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int found,flag,l,p[4][5],tp,e[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0,tr;
clrscr();
pritnf(“enter total no of process”);
scanf(“%d”,&tp);
pritnf(“enter total no of resources”);
scanf(“%d”,&tr);
printf(“enter claim matrix”);
for(i=0;i<tp;i++)
for(j=0;j<tr;j++)
{
scanf(“%d”,&c[i][j]);
}
printf(“enter allocation matrix”);
for(i=0;i<tp;i++)
for(j=0;j<tr;j++)
{
scanf(“%d”,&p[i][j]);
}
printf(“enter the resource vectors”);
for(i=0;i<tr;i++)
scanf(“%d”,&r[i]);
printf(“enter the availability matrix”));
for(i=0;i<tr;i++)
{
scanf(“%d”,&r[i]);
temp[i]=a[i];
}
for(i=0;i<tp;i++)
{
sum=0;
for(j=0;j<tr;j++)
{
sum+=p[i][j];

Operating Systems Lab 67 Dept. of CSE, VEMUIT


}
if(sum==0)
{
m[k]=i;
k++;
}
for(i=1;i<=tp;i++);
{
for(l=1;l<k;l++)
if(i!=m[l])
{
flag=1;
for(j=i;j<=5;j++)
if(c[i][j]>temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
++;
for(j=1;j<=tr;j++)
temp[j]+=p[i][j];
}
}
printf(“\n deadlock causing process are”);
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf(“%d”,j);
}
}

15.4 INPUT AND OUTPUT:

Enter total no of process 4

Enter total no of resources 5

Enter claim matrix

01001

00101

00001

1 0 1 0 1

Enter resources vector:

Operating Systems Lab 68 Dept. of CSE, VEMUIT


21121

Enter availability vector

00001

Deadlock causing process will be 2

15.5 PRE-LAB QUESTIONS

1.Define resource. Give examples.

2.What is deadlock?

3.What are the conditions to be satisfied for the deadlock to occur?

15.6 LAB ASSIGNMENT

1.Write a C program to implement deadlock detection technique for the following scenarios?

a.Single instance of each resource type.

b.Multiple instances of each resource type.

15.7 POST-LAB QUESTIONS

1.How can be the resource allocation graph used to identify a deadlock situation?

2.How is Banker‟s algorithm useful over resource allocation graph technique?

3.Differentiate between deadlock avoidance and deadlock prevention?

Operating Systems Lab 69 Dept. of CSE, VEMUIT


ADDITIONAL PROGRAMS

A. Optimal Page Replacement Algorithm :

1. OBJECTIVE

Program to perform Optimal page replacement algorithm.

2. PROGRAM

#include<conio.h>
#include<stdio.h>
int optimalframe(int w[],int m);
void main()
{
int i,j,n,m,r,pagef,l,h,p;
int a[100],b[100],w[10];
pagef=0;
clrscr();
printf("enter the number of pages\n");
scanf("%d",&n);
printf("enter the number of frames in main memory\n");
scanf("%d",&m);
for(i=0;i<m;i++)
b[i]=-1;
for(i=0;i<n;i++)
{
printf("enter the element %d\n",i);
scanf("%d",&a[i]);
}
for(i=0;i<m;i++)
{
b[i]=a[i];
for(r=0;r<m;r++)
printf("%d ",b[r]);
printf("\n");
pagef++;
}
for(i=m;i<n;i++)
{
for(j=0;j<m;j++)
{
if(b[j]==a[i])
{
printf("the page %d is already present in main memory\n",a[i]);
break;
}
}
if(j==m)
{
for(l=0;l<m;l++)
{
for(p=i+1;p<n;p++)
{

Operating Systems Lab 70 Dept. of CSE, VEMUIT


if(b[l]==a[p])
{
w[l]=p;
break;
}
}
if(p==n)
{
b[l]=a[i];
break;
}}
if(l==m)
{
h=optimalframe(w,m);
b[h]=a[i];
}
for(r=0;r<m;r++)
printf("%d ",b[r]);
printf("\n");
pagef++;
}
}
printf("number of page faults: %d",pagef);
getch();
}
int optimalframe(int w[],int m)
{
int max,k,h=0;
max=w[0];
for(k=1;k<m;k++)
if(w[k]>max)
{
max=w[k];
h=k;
}
return h;
}

3. INPUT AND OUTPUT

enter the number of pages


20
enter the number of frames in main memory
3
enter the element 0
7
enter the element 1
0
enter the element 2
1
enter the element 3
2
enter the element 4
0
enter the element 5

Operating Systems Lab 71 Dept. of CSE, VEMUIT


3
enter the element 6
0
enter the element 7
4
enter the element 8
2
enter the element 9
3
enter the element 10
0
enter the element 11
3
enter the element 12
2
enter the element 13
1
enter the element 14
2
enter the element 15
0
enter the element 16
1
enter the element 17
7
enter the element 18
0
enter the element 19
1
7 -1 -1
7 0 -1
7 0 1
2 0 1
the page 0 is already present in main memory
2 0 3
the page 0 is already present in main memory
2 4 3
the page 2 is already present in main memory
the page 3 is already present in main memory
2 0 3
the page 3 is already present in main memory
the page 2 is already present in main memory
2 0 1
the page 2 is already present in main memory
the page 0 is already present in main memory
the page 1 is already present in main memory
7 0 1
the page 0 is already present in main memory
the page 1 is already present in main memory
number of page faults: 9

Operating Systems Lab 72 Dept. of CSE, VEMUIT


B. Dekker’s Algorithm :

1. OBJECTIVE

Program to perform Dekker‟s algorithm.

2. PROGRAM

# include <stdio.h>

# include<pthread.h>

# include<cursors.h>

int turn=0, flag[2]={0,0},bal=0;

void longdelay()

long int t1,t2;

for(t1=0;t1<64000;t1++);

for(t2=0;t2<64000;t2++);

void shortdelay()

int t3,t4;

for(t3=0;t3<32000;t3++)

for(t4=0;t4<32000;t4++)

void test(int i)

int j,k,m,p,q,r,c;

j=1-i;

for(k=0;k<3;k++)

flag[i]=1;

while(flag[j])

Operating Systems Lab 73 Dept. of CSE, VEMUIT


{

if (turn==j)

flag[i]=0;

printf(“Iam waiting:%d”,i);

while(turn==j);

flag[i]=1;

longdelay();

printf(“Iam entering %d”,i);

c=bal;

/*critical section*/

printf(“\n process: %d”,l);

printf(“flag[%d]=%d, flag[%d]=%d”,i,flag[i],j,flag[j]);

printf(“turn=%d\n”,turn);

bal=c+1000;

printf(“the balance is %d”,bal);

turn=j;

flag[i]=0;

printf(“Iam exiting: %d”,i);

shortdelay();

int main()

pthread_t t1,t2;

pthread_creat(&t,NULL,(void*)&test,(void*)0);

printf(“After creation of first thread…..”);

pthread_creat(&t2,NULL,(void*)&test,(void*)1);

pthread_join(t1,NULL);

Operating Systems Lab 74 Dept. of CSE, VEMUIT


pthread_join(t2,NULL);

sleep(s);

printf(“Parent terminated:%d”,bal);

return(0); }

Operating Systems Lab 75 Dept. of CSE, VEMUIT

You might also like