OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
OPERATING SYSTEMS LAB MANUAL
III Year- B.Tech. CSE - I SEM
...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
INDEX
S.no Experiment
No.
NAME OF THE EXPERIMENT PAGE
NO.
1...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
CPU SCHEDULING
1. Aim: Simulate the following CPU schedulin...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
Head process gets the CPU
Uses less than a time quantum imp...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
printf("enter process name:");
scanf("%s",p[i]);
printf("en...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
}
}
if(found!=0)
{
wt[i]=wt[found]-(count*timer);
count=0;
...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
enter process time:4
enter process name:bbb
enter process t...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
Source code:
Shortest Job First Scheduling Algorithm
#inclu...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
{
temp1=bt[i];
bt[i]=bt[j];
bt[j]=temp1;
temp2=p[i];
p[i]=p...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
OUTPUT:
enter number of process:3
enter process name:p1
ent...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
First-come-First-Served Scheduling Algorithm
Source code:
#...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
p[i]=p[j];
p[j]=temp1;
temp2=bt[i];
bt[i]=bt[j];
bt[j]=temp...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
OUTPUT:
enter no.of processors2
enter process namep1
enter ...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
Priority Scheduling Algorithm
source code:
#include<stdio.h...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
{
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
temp1=p[i];
p[i]=p[j...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
OUTPUT:
enter no.of processes:3
enter process name:x
enter ...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
FILE ALLOCATION METHODS
2. Aim: Simulate all file allocatio...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
#include<stdio.h>
#include<conio.h>
struct FILE
{
int rno,m...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
fscanf(stdin,"%dt%st%dt%dt%dtn",&x[i].rno,&x[i].name,&x[i]....
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20]...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
printf("%d-->",blocks[i][j]);
getch();
}
OUTPUT:
enter no.o...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
#include<conio.h>
#include<string.h>
void main()
{
int i,j,...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
}
OUTPUT:
enter no.of files:3
enter name of file 1:A
enter ...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
CPU runs program instructions only when program is in memor...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
{
int m,i,p[10],a[10],pr[15],re[15];
char ch;
clrscr();
pri...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
getch();
}
OUTPUT:
enter memory to be allocated700
enter pr...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
clrscr();
printf("enter the total memory");
scanf("%d",&t);...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
interval fragmentation for this block33
enter memory for 1 ...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
Higher level file organization techniques such as ISAM (Ind...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
int nf=0,i=0,j=0,ch;
char mdname[10],fname[10][10],name[10]...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
OUTPUT:
enter the directory name:sss
enter the number of fi...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
waiting for access to a critical section
Waiting for a reso...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
What if P2 requests 1 more and is allocated 1 more instance...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
avil[i]=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
avil[j]+=alloc...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
else
{
printf("n System is in safe mode");
printf("n the gi...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
all the resources can be allocated to process 1
avilable re...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
• waiting for a resource Note that it is usually a non-pree...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
BANKERS ALGORITHM FOR DEADLOCK PREVENTION
#include<stdio.h>...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
if(temp[i]>temp[j])
{
t=temp[i];
temp[i]=temp[j];
temp[j]=t...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
enter name and time:a 1
enter name and time:b 2
enter name ...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
When a page needs to be replaced, the page at the front of ...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
void qins(int a[],int c);
void main()
{
int i,j,k,l=1,c=0;
...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
}
}
}
else
{
for(i=0;i<n;i++)
{
if(cq[i]==-1)
break;
printf...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
cr++;
cq[cr%n]=e;
}
OUTPUT:
enter no.of frames:3
enter the ...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
do you want to enter another page?y
enter the page number:7...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
scanf("%d",&seq[i]);
printf("enter the no.of frames");
scan...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
if(flag!=0)
{
for(j=0;j<n;j++)
{
m=fr[j];
for(k=i;k<max;k++...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
fr[s]=seq[i];
for(k=0;k<n;k++)
printf("%dt",fr[k]);
pf++;
p...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
2
4
5
3
2
5
2
enter the no.of frames3
2 3 1
2 3 5
4 3 5
2 3...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
clrscr();
printf("enter no.of frames:");
scanf("%d",&n);
fo...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
scanf("%d",&k);
}while(k!=-1);
printf("page faults:%d",pf);...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
{
if(cq[i].count<=mm)
{
ll=i;
mm=cq[i].count;
}
}
cq[ll].no...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
2
3
enter the page:
4
frame
4
2
3
enter the page:
-1
page f...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
MEMORY MANAGEMENT
8.Aim: Simulate Paging Technique of memor...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
• Trap to OS –save user registers and process table
• Deter...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
}
clrscr();
for(i=0;i<no_frames;i++)
{
printf("format of vi...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
physical address=2:9999
Viva Questions:
1. What is fragment...
OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE
Lakshma Reddy pace
Upcoming SlideShare
Loading in …5
×

Os

859 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
859
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
40
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Os

  1. 1. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace OPERATING SYSTEMS LAB MANUAL III Year- B.Tech. CSE - I SEM Objective: To provide an understanding of the language translation peculiarities by designing a complete translator for a mini language. To provide an understanding of the design aspects of operating system. Operating System is the interface between system and user. So we can get clarity about functionalities of Operating system. In this lab we can learn about different types of algorithms, memory management, file allocation, and file organization techniques. Recommended Systems/Software Requirements: Intel based desktop PC with minimum of 166 MHZ or faster processor with atleast 64 MB RAM and 100 MB free disk space. C++ compiler and JDK kit.
  2. 2. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace INDEX S.no Experiment No. NAME OF THE EXPERIMENT PAGE NO. 1 1.a 1.b 1.c 1.d Perform CPU scheduling algorithm for round robin. Perform CPU scheduling algorithm for SJF. Perform CPU scheduling algorithm for FCFS. Perform CPU scheduling algorithm for PRIORITY. 3 2 2.a 2.b 2.C Perform Sequential file allocation method Perform Indexed file allocation method Perform Linked allocation method 18 3 3.a 3.b Memory management with MVT. Memory management with MFT. 26 4 4 File organization for single level directory. 31 5 5. Banker’s algorithm for deadlock avoidance. Banker’s algorithm for deadlock prevention. 35 6 6. Banker’s algorithm for deadlock prevention. 40 7 7.a 7.b 7.c Page replacement algorithm for FIFO. Page replacement algorithm for LFU. Page replacement algorithm for LRU. 45 8 8 Perform the paging technique of memory management 60
  3. 3. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace CPU SCHEDULING 1. Aim: Simulate the following CPU scheduling algorithms a) Round Robin b) SJF c) FCFS d) Priority Description: FCFS (first-come-first-serve) Scheduling First-come, First served is simplest scheduling algorithm. Ready queue is a FIFO queue: Longest waiting process at the front of queue. New ready processes join the rear. Non-preemptive: executes until voluntarily gives up CPU finished or waits for some event Problem: CPU bound process may require a long CPU burst . Other processes, with very short CPU bursts, wait in queue Reduces CPU and I/O device utilization. SJF (shortest-job-first) Scheduling Assume the next burst time of each process is known SJF selects process which has the shortest burst time Optimal algorithm because it has the shortest average waiting time Impossible to know in advance OS knows the past burst times- make a prediction using an average Non-preemptive Or preemptive Shortest remaining time first Interrupts running process if a new process enters the queue New process must have shorter burst than remaining time Round Robin Scheduling Similar to FCFS, but preemption to switch between processes Time quantum(time slice) is a small unit of time (10 to 100 ms) Process is executed on the CPU for at most one time quantum Implemented by using the ready queue as a circular queue
  4. 4. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace Head process gets the CPU Uses less than a time quantum implies gives up the CPU voluntary Uses full time quantum implies timer will cause an interrupt Context switch will be executed Process will be put at the tail of queue Priority Scheduling Assume a priority is associated with each process Assume all processes arrive at the same time Select highest priority process from the ready queue Let T be the next CPU burst of a process SJF is a special case of priority scheduling Equal-priority processes are scheduled in FCFS order PRIORITY can be preemptive or Non-preemptive Priorities can be defined internally Memory requirements, number of open files, burst times Priorities can be defined externally Source code: Round Robin Scheduling Algorithm #include<stdio.h> #include<conio.h> #include<process.h> #include<string.h> void main() { char p[10][5]; int et[10],wt[10],timer=3,count,pt[10],rt,i,j,totwt=0,t,n=5,found=0,m; float avgwt; clrscr(); for(i=0;i<n;i++) {
  5. 5. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace printf("enter process name:"); scanf("%s",p[i]); printf("enter process time:"); scanf("%d",&pt[i]); } m=n; wt[0]=0; i=0; do { if(pt[i]>timer) { rt=pt[i]-timer; strcpy(p[n],p[i]); pt[n]=rt; et[i]=timer; n++; } else { et[i]=pt[i]; } i++; wt[i]=wt[i-1]+et[i-1]; }while(i<n); count=0; for(i=0;i<m;i++) { for(j=i+1;j<=n;j++) { if(strcmp(p[i],p[j])==0) { count++; found=j;
  6. 6. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace } } if(found!=0) { wt[i]=wt[found]-(count*timer); count=0; found=0; } } for(i=0;i<m;i++) { totwt+=wt[i]; } avgwt=(float)totwt/m; for(i=0;i<m;i++) { printf("n%st%dt%d",p[i],pt[i],wt[i]); } printf("ntotal waiting time%dn",totwt); printf("total average time%f",avgwt); } OUTPUT: enter process name:aaa
  7. 7. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace enter process time:4 enter process name:bbb enter process time:3 enter process name:ccc enter process time:2 enter process name:ddd enter process time:5 enter process name:eee enter process time:1 aaa 4 9 bbb 3 3 ccc 2 6 ddd 5 10 eee 1 11 total waiting time39 total average time7.800000
  8. 8. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace Source code: Shortest Job First Scheduling Algorithm #include<stdio.h> #include<conio.h> void main() { int wt[10],bt[10],tot[10],at[10],n,i,j,temp,temp1,temp2,s1=0,s2=0; char p[10]; float avgwt,avgtot; clrscr(); printf("enter number of process:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("enter process name:"); scanf("%s",&p[i]); printf("enter arrival time:"); scanf("%d",&at[i]); printf("enter burst time:"); scanf("%d",&bt[i]); } printf("process id bt wt totn"); for(i=1;i<=n;i++) { temp1=bt[i]; temp2=p[i]; for(j=i+1;j<=n;j++) { if(bt[i]>bt[j])
  9. 9. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace { temp1=bt[i]; bt[i]=bt[j]; bt[j]=temp1; temp2=p[i]; p[i]=p[j]; p[j]=temp2; } } } for(i=1;i<=n;i++) { wt[0]=0; bt[0]=0; wt[i]=bt[i-1]+wt[i-1]; tot[i]=wt[i]+bt[i]; printf("%c%5d%4d%5d%5dn",p[i],bt[i],at[i],wt[i],tot[i]); s1+=wt[i]; s2+=tot[i]; avgwt=(float)s1/n; avgtot=(float)s2/n; } printf("avgwt:%ft avgtot:%f",avgwt,avgtot); getch(); }
  10. 10. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace OUTPUT: enter number of process:3 enter process name:p1 enter arrival time:0 enter burst time:5 enter process name:p2 enter arrival time:0 enter burst time:3 enter process name:p3 enter arrival time:0 enter burst time:7 process id bt wt tot p 3 0 0 3 p 5 0 3 8 p 7 0 8 15 avgwt:3.666667 avgtot:8.666667
  11. 11. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace First-come-First-Served Scheduling Algorithm Source code: #include<stdio.h> #include<conio.h> void main() { int wt[10],bt[10],at[10],tot[10],n,i,j; int temp,temp1,temp2,s1=0,s2=0; char p[10]; float avgwt,avgtot; clrscr(); printf("enter no.of processors"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("enter process name"); scanf("%s",&p[i]); printf("enter burst time"); scanf("%d",&bt[i]); printf("enter arrivel time"); scanf("%d",&at[i]); } printf(" pidt attbttwtttotn"); for(i=1;i<=n;i++) { if(at[i]>at[j]) { temp=at[i]; at[i]=at[j]; at[j]=temp; temp1=p[i];
  12. 12. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace p[i]=p[j]; p[j]=temp1; temp2=bt[i]; bt[i]=bt[j]; bt[j]=temp2; } } for(i=1;i<=n;i++) { wt[0]=0; bt[0]=0; wt[i]=wt[i-1]+bt[i-1]; tot[i]=bt[i]+wt[i]; printf("%ct%dt%dt%dt%dn",p[i],at[i],bt[i],wt[i],tot[i]); s1+=wt[i]; s2+=tot[i]; } avgwt=(float)s1/n; avgtot=(float)s2/n; printf("avgwt:%fnavgtot:%f",avgwt,avgtot); getch(); }
  13. 13. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace OUTPUT: enter no.of processors2 enter process namep1 enter burst time4 enter arrivel time3 enter process namep2 enter burst time3 enter arrivel time2 pid at bt wt tot p 3 4 0 4 p 2 3 4 7 avgwt:2.000000 avgtot:5.500000
  14. 14. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace Priority Scheduling Algorithm source code: #include<stdio.h> #include<conio.h> void main() { int wt[10],bt[10],pr[10],tot[10],i,j,temp,temp1,temp2,s1=0,s2=0,n; char p[10]; float avgwt,avgtot; clrscr(); printf("enter no.of processes:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("enter process name:"); scanf("%s",&p[i]); printf("enter burst time:"); scanf("%d",&bt[i]); printf("enter priority:"); scanf("%d",&pr[i]); } printf("processor id pr bt wt tot"); for(i=1;i<=n;i++) { temp=pr[i]; temp1=p[i]; temp2=bt[i]; for(j=i+1;j<=n;j++) { if(pr[i]>pr[j])
  15. 15. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace { temp=pr[i]; pr[i]=pr[j]; pr[j]=temp; temp1=p[i]; p[i]=p[j]; p[j]=temp1; temp2=bt[i]; bt[i]=bt[j]; bt[j]=temp2; } } } for(i=1;i<=n;i++) { wt[0]=0; bt[0]=0; wt[i]=wt[i-1]+bt[i-1]; tot[i]=bt[i]+wt[i]; printf("n %c %d %d %d %d ",p[i],pr[i],bt[i],wt[i],tot[i]); s1+=wt[i]; s2+=tot[i]; } avgwt=(float)s1/n; avgtot=(float)s2/n; printf("n avgwt: %f avgtot: %f ",avgwt,avgtot); getch(); }
  16. 16. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace OUTPUT: enter no.of processes:3 enter process name:x enter burst time:3 enter priority:2 enter process name:y enter burst time:4 enter priority:2 enter process name:z enter burst time:9 enter priority:7 processor id pr bt wt tot x 2 3 0 3 y 2 4 3 7 z 7 9 7 16 avgwt: 3.333333 avgtot: 8.666667 Viva Questions: 1. What is CPU Scheduler? 2. What is Throughput, Turnaround time, waiting time and Response time? 3. What Is the average time in round robin algorithm? 4. What is the average time in SJF? 5. What is the average time in FCFS? 6. What is the average time in priority scheduling?
  17. 17. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace FILE ALLOCATION METHODS 2. Aim: Simulate all file allocation strategies a)sequential b)Indexed c)Linked Description: Sequential file allocation 1) simplest method is a contiguous (sequential) set of blocks 2) disk address or start(base) block and length (in blocks) 3) sequential access is easy – read next block 4) direct access (seek) is easy 5) logical offset= physical base + offset 6) how to fine space for new file 7) first-fit: first hole that is big enough 8) best fit: smallest hole that is big enough 9) external fragmentation 10) blocks are available but not sequentially 11) internal fragmentation 12) pre-allocation of blocks is too large 13) left-over amount in last block Indexed Allocation Indexed: direct access and no fragmentation 1) Bring all pointers into one location: the index block(IB) 2) Each file has its own IB 3) Ith entry in the IB points to the ith block 4) Suffers from wasted space(IB may not be full) Linked Allocation 1) solves external fragmentation: can use any block for any file 2) solves pre-allocation internal fragmentation 3) good for sequential access: chase the pointer 4) not effective for direct access 5) where is the nth block of the file 6) requires space for pointers 7) if a pointer is bad, file is lost /*Sequential file allocation*/
  18. 18. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace #include<stdio.h> #include<conio.h> struct FILE { int rno,marks1,marks2,marks3; char name[10]; }; main() { FILE *fp; float avg; int i,total,n; struct FILE x[30]; clrscr(); printf("enter no.of.students"); scanf("%d",&n); fp=fopen("student.txt","w"); printf("input student list datann"); printf("rno name marks1 marks2 marks3n"); for(i=1;i<n;i++) { fscanf(stdin,"%dt%st%dt%dt%dtn",&x[i].rno,&x[i].name,&x[i].marks1,&x[i].marks2,&x[i].m arks3); fprintf(fp,"%dt%st%dt%dt%dtn",x[i].rno,x[i].name,x[i].marks1,x[i].marks2,x[i].marks3); fclose(fp); } fprintf(stdout,"nn"); fp=fopen("student.txt","r"); printf("rno name marks1marks2marks3total avgn"); for(i=1;i<n;i++) {
  19. 19. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace fscanf(stdin,"%dt%st%dt%dt%dtn",&x[i].rno,&x[i].name,&x[i].marks1,&x[i].marks2,&x[i].m arks3); total=x[i].marks1+x[i].marks2+x[i].marks3; avg=(float)total/3; fprintf(fp,"%dt%st%dt%dt%dt%dt%fn",x[i].rno,x[i].name,x[i].marks1,x[i].marks2,x[i].marks 3,total,avg); } fclose(fp); getch(); return; } OUTPUT: Enter no. of students : 3 Input student data regno name marks1 marks2 marks3 1 A 5 1 4 2 B 1 4 4 3 C 3 5 5 regno name marks1 marks2 marks3 total Avg 1 A 5 1 4 10 3.33 2 B 1 4 4 9 3.00 3 C 3 5 5 13 4.00 /*Indexed file allocation*/ Source code:
  20. 20. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace #include<stdio.h> #include<conio.h> void main() { int a[20],b[20],i,j,n,blocks[20][20]; char e[20][20],s[10]; clrscr(); printf("enter no.of files:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter file %d :",i+1); scanf("%s",e[i]); printf("enter starting block of file %d:",i+1); scanf("%d",&a[i]); printf("enter the blocks in file %d :",i+1); scanf("%d",&b[i]); printf("enter the blocks of the file %d :n",i+1); for(j=0;j<b[i];j++) scanf("%d",&blocks[i][j]); } printf("enter the file to be searched :"); scanf("%s",&s); for(i=0;i<n;i++) { if(strcmp(s,e[i])==0) break; } printf("nFILENAME STARTBLOCK NOOFBLOCKS BLOCKSn"); printf("n%s %d %d ",e[i],a[i],b[i]); for(j=0;j<b[i];j++)
  21. 21. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace printf("%d-->",blocks[i][j]); getch(); } OUTPUT: enter no.of files:3 enter file 1 :a enter starting block of file 1:44 enter the blocks in file 1 :4 enter the blocks of the file 1 : 44 54 64 74 enter file 2 :b enter starting block of file 2:29 enter the blocks in file 2 :4 enter the blocks of the file 2 : 29 39 49 59 enter file 3 :c enter starting block of file 3:28 enter the blocks in file 3 :5 enter the blocks of the file 3 : 28 38 48 58 68 enter the file to be searched :c FILENAME STARTBLOCK NOOFBLOCKS BLOCKS c 28 5 28-->38-->48-->58-->68--> /*linked file allocation */ Source code: #include<stdio.h>
  22. 22. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace #include<conio.h> #include<string.h> void main() { int i,j,n,l[10],sb[10],eb[10],b[10][10]; char na[10][10],fname[10]; clrscr(); printf("enter no.of files:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter name of file %d:",i+1); scanf("%s",na[i]); printf("enter length of file%d :",i+1); scanf("%d",&l[i]); printf("enter start block of file%d :",i+1); scanf("%d",&sb[i]); printf("enter end block of file%d :",i+1); scanf("%d",&eb[i]); printf("enter %d blocks occupied by file %d:",l[i],i+1); for(j=0;j<l[i];j++) scanf("%d",&b[i][j]); } printf("enter the file name to be searched:"); scanf("%s",fname); for(i=0;i<n;i++) { if(strcmp(fname,na[i])==0) break; } for(j=0;j<l[i]-1;j++) printf("%d/%d-->",b[i][j],b[i][j+1]); printf("%d/-1",b[i][j]); getch();
  23. 23. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace } OUTPUT: enter no.of files:3 enter name of file 1:A enter length of file1 :3 enter start block of file1 :5 enter end block of file1 :20 enter 3 blocks occupied by file 1:5 15 20 enter name of file 2:B enter length of file2 :4 enter start block of file2 :18 enter end block of file2 :25 enter 4 blocks occupied by file 2:18 55 20 25 enter name of file 3:C enter length of file3 :5 enter start block of file3 :0 enter end block of file3 :89 enter 5 blocks occupied by file 3:0 55 478 12 89 enter the file name to be searched:C Viva Questions: 1. What are the types of file allocation methods? 2. What is indexed file allocation method? 3. Difference between sequential and indexed methods ? 4. What is the main use of sequential file allocation? 5. What are the file accessing methods? 6. What is the file? 7. What are the file operations ? PROGRAMMING TECHNIQUES 3. Aim: Simulate MVT and MFT Description: Memory management
  24. 24. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace CPU runs program instructions only when program is in memory. Programs do I/O sometimes IMPLY CPU wasted. Solution : Multiprogramming Multiple programs share the memory One program at a time gets CPU Simultaneous resource possession Better performance Multiple Programming with Fixed Number of Tasks (MFT): IBM in their Mainframe Operating system OS/MFT implements the MFT concept. OS/MFT uses fixed partitioning concept to load programs into main memory. Fixed Partitioning: In fixed partitioning concept, RAM is divided into set of fixed partitions of equal size. Programs having the size less than the partition size are loaded into memory Programs having size more than the size of partition size is rejected The program having the size less than the partition size will lead to internal fragmentation If all partitions are allocated and if a new program is to be loaded, the program that leads to maximum internal fragmentation can be replaced. Multi-programming with variable number of tasks (MVT): IBM in their Mainframe Operating system OS/MVT implements the MVT concept. OS/MVT uses dynamic partitioning concept to load programs into main memory. Dynamic Partitioning: Initially RAM is portioned according to the size of programs to be loaded into Memory till such time that no other program can be loaded. The left over memory is called a hole which is too small to fit any process When a new program is to be loaded into memory look for the partition, which leads to least external fragmentation and load the program. The space that is not used in a partition is called as external fragmentation Multi-programming with variable number of tasks (MVT): Source code: #include<stdio.h> #include<conio.h> void main()
  25. 25. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace { int m,i,p[10],a[10],pr[15],re[15]; char ch; clrscr(); printf("enter memory to be allocated"); scanf("%d",&m); printf("n enter process size"); scanf("%d",&p[0]); i=0; do { m=m-p[i]; printf("n remaining memory is %d",m); abc:printf(" do you want to continue "); fflush(stdin); scanf("%c",&ch); i++; if(ch=='y') { printf("enter the process size"); scanf("%d",&p[i]); } else { printf("remaining memory is %d",m); break; } if(m<p[i]) { printf("n required memory is not available n"); goto abc; } } while((ch=='y')&&(m>=p[i]));
  26. 26. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace getch(); } OUTPUT: enter memory to be allocated700 enter process size200 remaining memory is 500 do you want to continue y enter the process size400 remaining memory is 100 do you want to continue y enter the process size200 required memory is not available do you want to continue n remaining memory is 100 Multiple Programming with Fixed Number of Tasks (MFT): Source code: #include<stdio.h> #include<conio.h> void main() { int t,b,h,i,j,p,a[10],pr[10],c[15],temp=0,total=0;
  27. 27. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace clrscr(); printf("enter the total memory"); scanf("%d",&t); printf(" enter no.of process"); scanf("%d",&p); b=t/p; for(i=0;i<p;i++) a[i]=b; for(i=0;i<p;i++) { label: printf("n enter memory for %d process",i); scanf("%d",&temp); if(temp<b) { c[i]=a[i]-temp; printf("interval fragmentation for this block"); scanf("%d",&c[i]); } else { printf("required memory is not available"); goto label; } total=total+c[i]; } printf(" total internal fagmentation %d", total); getch(); } OUTPUT: enter the total memory1000 enter no.of process3 enter memory for 0 process300
  28. 28. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace interval fragmentation for this block33 enter memory for 1 process300 interval fragmentation for this block33 enter memory for 2 process300 interval fragmentation for this block33 total internal fagmentation 99 Viva Questions: 1. What is multiprogramming? 2. What is meant by fixed partitioning? 3. What is meant by dynamic partitioning? 4. Which method is preferable among these methods? 5. Compare between MVT and MFT? FILE ORGANIZATION TECHNIQUES 4. Aim: Simulate all File Organization Techniques Single level directory Description:
  29. 29. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace Higher level file organization techniques such as ISAM (Indexed Sequential Access Method) or VSAM (Virtual System Access Method) could incur large seek times because of a double of triple access to retrieve one record (index(s) and data). Disk optimization is critical in these cases, but is more complex, because data retrieved from one disk (the index) indicates where the next seek is (data which that index points to). Data and index portions of data sets are not normally stored next to each other, and sometimes are stored on different packs. What might be the impact of placing the index on one pack and the data on another? Major concepts: Files are made up of records; records are made up of fields Disk blocks are smaller than files and larger than records; files must be split into disk blocks for storage (and the records in a file must be grouped somehow for storage on disk blocks, independent of the file organization) Fixed-length records Variable length records Block structure: fixed-packed or slotted page File structure: heap, sequential, hashed, or clustered Details of the above file structures Files are logical units mapped onto physical secondary storage File name: logical object Physical objects: blocks on disk, tape, optical disk One or more sectors: smallest unit to read from or write to disk Block: unit of I/o transfer from disk to memory Secondary storage: nonvolatile File attributes: frequency of additions and deletions Activity: percentage of records accessed during time frame Directory : keep track of files Create the illusion of compartments Single Level Directory #include<stdio.h> #include<conio.h> #include<string.h> void main() {
  30. 30. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace int nf=0,i=0,j=0,ch; char mdname[10],fname[10][10],name[10]; int n; clrscr(); printf("enter the directory name:"); scanf("%s",&mdname); printf("enter the number of files:"); scanf("%d",&nf); do { printf("nenter file name to be created:"); scanf("%s",&name); for(i=0;i<nf;i++) { if(!strcmp(name,fname[i])) break; } if(i==nf) { strcpy(fname[j++],name); nf++; } else printf("there is already%sn",name); printf("do u want to enter another file(1 or 0):"); scanf("%d",&ch); }while(ch==1); printf("directory name is :%sn",mdname); printf("files names are:"); for(i=0;i<j;i++) printf("n%s",fname[i]); getch(); }
  31. 31. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace OUTPUT: enter the directory name:sss enter the number of files:4 enter file name to be created:laks do u want to enter another file(1 or 0):1 enter file name to be created:a do u want to enter another file(1 or 0):1 enter file name to be created:d do u want to enter another file(1 or 0):1 enter file name to be created:g do u want to enter another file(1 or 0):0 directory name is :sss files names are: laks a d g Viva Questions: 1. What are the types of file organization techniques? 2. Difference between single level and two level directories? 3. Which file organization is used in UNIX OPERATING SYSTEM? DEADLOCK AVOIDANCE 5. Aim: To simulate banker‟s algorithm for deadlock avoidance Description: Deadlock Definition A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause (including itself). Waiting for an event could be:
  32. 32. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace waiting for access to a critical section Waiting for a resource Note that it is usually a non-preempt able (resource). Preempt able resources can be yanked away and given to another. Conditions for Deadlock Mutual exclusion: resources cannot be shared. Hold and wait: processes request resources incrementally, and hold on to what they've got. No preemption: resources cannot be forcibly taken from processes. Circular wait: circular chain of waiting, in which each process is waiting for a resource held by the next process in the chain. Strategies for dealing with Deadlock ignore the problem altogether detection and recovery avoidance by careful resource allocation prevention by structurally negating one of the four necessary conditions. Deadlock Avoidance Avoid actions that may lead to a deadlock. Think of it as a state machine moving from one state to another as each instruction is executed. Safe State Safe state is one where It is not a deadlocked state There is some sequence by which all requests can be satisfied. To avoid deadlocks, we try to make only those transitions that will take you from one safe state to another. We avoid transitions to unsafe state (a state that is not deadlocked, and is not safe) eg. Total # of instances of resource = 12 (Max, Allocated, Still Needs) P0 (10, 5, 5) P1 (4, 2, 2) P2 (9, 2, 7) Free = 3 - Safe The sequence is a reducible sequence the first state is safe.
  33. 33. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace What if P2 requests 1 more and is allocated 1 more instance? - results in Unsafe state So do not allow P2's request to be satisfied. BANKERS ALGORITHM FOR DEADLOCK AVOIDANCE #include<stdio.h> #include<conio.h> #include<string.h> void main() { int alloc[10][10],max[10][10]; int avil[10],work[10],total[10]; int i,j,k,n,need[10][10]; int m; int count=0,c=0; char finish[10]; clrscr(); printf("enter the no.of process and resource:"); scanf("%d%d",&n,&m); for(i=0;i<=n;i++) finish[i]='n'; printf("enter the claim matrix:"); for(i=0;i<n;i++) for(j=0;j<m;j++) scanf("%d",&max[i][j]); printf("enter the allocation matrix:"); for(i=0;i<n;i++) for(j=0;j<m;j++) scanf("%d",&alloc[i][j]); printf("resource vector:"); for(i=0;i<m;i++) scanf("%d",&total[i]); for(i=0;i<m;i++)
  34. 34. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace avil[i]=0; for(i=0;i<n;i++) for(j=0;j<m;j++) avil[j]+=alloc[i][j]; for(i=0;i<m;i++) work[i]=avil[i]; for(j=0;j<m;j++) work[j]=total[j]-work[j]; for(i=0;i<n;i++) for(j=0;j<m;j++) need[i][j]=max[i][j]-alloc[i][j]; A:for(i=0;i<n;i++) { c=0; for(j=0;j<m;j++) if((need[i][j]<=work[j])&&(finish[i]=='n')) c++; if(c==m) { printf("all the resources can be allocated to process %d",i+1); printf("nn avilable resources are:"); for(k=0;k<m;k++) { work[k]+=alloc[i][k]; printf("%4d",work[k]); } printf("n"); finish[i]='y'; printf("nprocess %d executed?:%c n",i+1,finish[i]); count++; } } if(count!=n) goto A;
  35. 35. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace else { printf("n System is in safe mode"); printf("n the given state is safe state"); } getch(); } OUTPUT: enter the no.of process and resource:4 3 enter the claim matrix:3 2 2 6 1 3 3 1 4 4 2 2 enter the allocation matrix: 1 0 0 6 1 2 2 1 1 0 0 2 resource vector:9 3 6 all the resources can be allocated to process 2 avilable resources are: 6 2 3 process 2 executed?:y all the resources can be allocated to process 3 avilable resources are: 8 3 4 process 3 executed?:y all the resources can be allocated to process 4 avilable resources are: 8 3 6 process 4 executed?:y
  36. 36. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace all the resources can be allocated to process 1 avilable resources are: 9 3 6 process 1 executed?:y System is in safe mode the given state is safe state Viva Questions: 1. What is deadlock? 2. How to avoid a process in a deadlock state? 3. By using which algorithm we can avoid the deadlock? 4. What is meant by starvation? DEADLOCK PREVENTION 6. Aim: To simulate Bankers Algorithm for Deadlock Prevention Description: Deadlock Definition A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause (including itself). Waiting for an event could be: • waiting for access to a critical section
  37. 37. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace • waiting for a resource Note that it is usually a non-preemptable (resource). Preemptable resources can be yanked away and given to another. Conditions for Deadlock • Mutual exclusion: resources cannot be shared. • Hold and wait: processes request resources incrementally, and hold on to what they've got. • No preemption : resources cannot be forcibly taken from processes. • Circular wait: circular chain of waiting, in which each process is waiting for a resource held by the next process in the chain. Strategies for dealing with Deadlock • ignore the problem altogether • detection and recovery • avoidance by careful resource allocation • prevention by structurally negating one of the four necessary conditions. Deadlock Prevention Difference from avoidance is that here, the system itself is built in such a way that there are no deadlocks. Make sure atleast one of the 4 deadlock conditions is never satisfied. This may however be even more conservative than deadlock avoidance strategy. Attacking Mutex condition never grant exclusive access. but this may not be possible for several resources. Attacking preemption not something you want to do. Attacking hold and wait condition • make a process hold at the most 1 resource at a time. • make all the requests at the beginning. All or nothing policy. If you feel, retry. eg. 2-phase locking Attacking circular wait Order all the resources. Make sure that the requests are issued in the correct order so that there are no cycles present in the resource graph. Resources numbered 1 ... n. Resources can be requested only in increasing order. ie. you cannot request a resource whose no is less than any you may be holding.
  38. 38. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace BANKERS ALGORITHM FOR DEADLOCK PREVENTION #include<stdio.h> #include<conio.h> void main() { char job[10][10]; int time[10],avail,tem[10],temp[10]; int safe[10]; int ind=1,i,j,q,n,t; clrscr(); printf("n enter no jobs"); scanf("%d",&n); for(i=0;i<n;i++) { printf("n enter name and time:"); scanf("%s%d",&job[i],&time[i]); } printf("n enter available resources:"); scanf("%d",&avail); for(i=0;i<n;i++) { printf("n enter the available resources"); scanf("%d",&avail); for(i=0;i<n;i++) { temp[i]=time[i]; tem[i]=i; } for(i=0;i<n;i++) for(j=i+1;j<n;j++) {
  39. 39. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace if(temp[i]>temp[j]) { t=temp[i]; temp[i]=temp[j]; temp[j]=t; t=tem[i]; tem[i]=tem[j]; tem[j]=t; } } for(i=0;i<n;i++) { q=tem[i]; if(time[q]<=avail) { safe[ind]=tem[i]; avail=avail-tem[q]; ind++; } else { printf("n no safe sequence"); } } printf("n safe sequence:"); for(i=1;i<ind;i++) printf("%s%dt",job[safe[i]],time[safe[i]]); getch(); } } OUTPUT: enter no jobs4
  40. 40. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace enter name and time:a 1 enter name and time:b 2 enter name and time:c 3 enter name and time:d 4 enter available resources:20 enter the available resources10 safe sequence:a1 b2 c3 d4 Viva Questions: 1. What are the methods for preventing the deadlocks? 2. What is meant by safe state and unsafe state? 3. What is meant by resource allocation graph? 4. What are the deadlock conditions? PAGE REPLACEMENT ALGORITHMS 7. Aim: To simulate all page replacement algorithms a.FIFO b.LRU c.LFU Etc. … Description: FIFO The first-in, first-out (FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on the part of the operating system. The idea is obvious from the name - the operating system keeps track of all the pages in memory in a queue, with the most recent arrival at the back, and the earliest arrival in front.
  41. 41. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace When a page needs to be replaced, the page at the front of the queue (the oldest page) is selected. While FIFO is cheap and intuitive, it performs poorly in practical application. Thus, it is rarely used in its unmodified form. This algorithm experiences Belady's anomaly. Least Recently Used (LRU) The least recently used page (LRU) replacement algorithm works on the idea that pages that have been most heavily used in the past few instructions are most likely to be used heavily in the next few instructions too. While LRU can provide near-optimal performance in theory it is rather expensive to implement in practice. There are a few implementation methods for this algorithm that try to reduce the cost yet keep as much of the performance as possible. The most expensive method is the linked list method, which uses a linked list containing all the pages in memory. At the back of this list is the least recently used page, and at the front is the most recently used page. The cost of this implementation lies in the fact that items in the list will have to be moved about every memory reference, which is a very time-consuming process. Another method that requires hardware support is as follows: suppose the hardware has a 64-bit counter that is incremented at every instruction. Whenever a page is accessed, it gains a value equal to the counter at the time of page access. Whenever a page needs to be replaced, the operating system selects the page with the lowest counter and swaps it out. With present hardware, this is not feasible because the required hardware counters do not exist. One important advantage of LRU algorithm is that it is amenable to full statistical analysis. It has been proved, for example, that LRU can never result in more than N-times more page faults than OPT algorithm, where N is proportional to the number of pages in the managed pool. On the other hand, LRU's weakness is that its performance tends to degenerate under many quite common reference patterns. For example, if there are N pages in the LRU pool, an application executing a loop over array of N + 1 pages will cause a page fault on each and every access. As loops over large arrays are common, much effort has been put into modifying LRU to work better in such situations. FIFO PAGE REPLACEMENT ALGORITHM Source code: #include<stdio.h> #include<conio.h> int cf=0,cr=-1,cq[10],n; int qdel(int*);
  42. 42. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace void qins(int a[],int c); void main() { int i,j,k,l=1,c=0; char ch; clrscr(); printf("enter no.of frames:"); scanf("%d",&n); for(i=0;i<n;i++) cq[i]=-1; printf("enter the sequence of pages:n"); do { l++; printf("n enter the page number:"); scanf("%d",&k); j=search(k); if(j==-1) { qins(cq,k); c++; } if(l<=n) { printf("n"); for(i=cf;i<=cr;i++) { if(cq[i]==-1) { break; } else { printf("nt%d",cq[i]);
  43. 43. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace } } } else { for(i=0;i<n;i++) { if(cq[i]==-1) break; printf("n%d",cq[i]); } } printf("n do you want to enter another page?"); ch=getch(); printf("%c",ch); }while(ch=='y'); printf("nPAGE FAULTS:%dn",c); getch(); } search(int k) { int i=0,p=-1; for(i=0;i<n;i++) { if(cq[i]==k) { p=i; break; } } return p; } void qins(int cq[],int e) {
  44. 44. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace cr++; cq[cr%n]=e; } OUTPUT: enter no.of frames:3 enter the sequence of pages: enter the page number:1 1 do you want to enter another page?y enter the page number:2 1 2 do you want to enter another page?y enter the page number:2 1 2 do you want to enter another page?y enter the page number:3 1 2 3 do you want to enter another page?y enter the page number:4 4 2 3
  45. 45. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace do you want to enter another page?y enter the page number:7 4 7 3 do you want to enter another page?n PAGE FAULTS:5 LFU PAGE REPLACEMENT ALGORITHM Source code: #include<stdio.h> int n; void main() { int seq[30],fr[5],pos[5],find,flag,max,i,j,m,k,t,s,pf=0; int count=1,p=0; float pfr; clrscr(); printf("enter max limit of the sequence"); scanf("%d",&max); printf("enter the sequence"); for(i=0;i<max;i++)
  46. 46. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace scanf("%d",&seq[i]); printf("enter the no.of frames"); scanf("%d",&n); fr[0]=seq[0]; pf++; printf("%dt",fr[0]); i=1; while(count<n) { flag=1; p++; for(j=0;j<i;j++) { if(seq[i]==seq[j]) flag=0; } if(flag!=0) { fr[count]=seq[i]; printf("%dt",fr[count]); count++; pf++; } i++; } printf("n"); for(i=p;i<max;i++) { flag=1; for(j=0;j<n;j++) { if(seq[i]==fr[j]) flag=0; }
  47. 47. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace if(flag!=0) { for(j=0;j<n;j++) { m=fr[j]; for(k=i;k<max;k++) { if(seq[k]==m) { pos[j]=k; break; } else pos[j]=-1; } } for(k=0;k<n;k++) { if(pos[k]==-1) flag=0; } if(flag!=0) s=findmax(pos); if(flag==0) { for(k=0;k<n;k++) { if(pos[k]==-1) { s=k; break; } } }
  48. 48. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace fr[s]=seq[i]; for(k=0;k<n;k++) printf("%dt",fr[k]); pf++; printf("n"); } } pfr=(float)pf/(float)max; printf("n the no of page faults are:%d",pf); printf("n page fault rate:%f",pfr); getch(); } int findmax(int a[]) { int max,i,k=0; max=a[0]; for(i=0;i<n;i++) { if(max<a[i]) { max=a[i]; k=i; } } return k; } OUTPUT: enter max limit of the sequence12 enter the sequence2 3 2 1 5
  49. 49. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace 2 4 5 3 2 5 2 enter the no.of frames3 2 3 1 2 3 5 4 3 5 2 3 5 the no of page faults are:6 page fault rate:0.500000 LRU PAGE REPLACEMENT ALGORITHM Source code: #include<stdio.h> #include<conio.h> int p,cf=0,cr=-1,n,c,l; int mm=0,ll=-1; void qins(int,int); struct frame { int no; int count; }cq[20]; void main() { int i,j,l=1,k,pf=0; char ch;
  50. 50. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace clrscr(); printf("enter no.of frames:"); scanf("%d",&n); for(i=0;i<n;i++) { cq[i].no=-1; cq[i].count=-1; } printf("enter the sequence of pages:"); c=0; printf("enter the page:"); scanf("%d",&k); do { c++; j=search(k); if(j==-1) { qins(j,k); pf++; } if(j!=-1) cq[j].count=c; printf("n frame"); for(i=0;i<n;i++) { if(cq[i].no==-1) break; printf("nt%dn",cq[i].no); } printf("enter the page:n");
  51. 51. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace scanf("%d",&k); }while(k!=-1); printf("page faults:%d",pf); getch(); } int search(int k) { int i=0,p=-1; for(i=0;i<n;i++) { if(cq[i].no==k) { p=i; break; } } return p; } void qins(int p,int e) { int i; if(cr<n-1) { cr++; cq[cr].no=e; cq[cr].count=c; } else { mm=1000; for(i=0;i<n;i++)
  52. 52. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace { if(cq[i].count<=mm) { ll=i; mm=cq[i].count; } } cq[ll].no=e; cq[ll].count=c; } } OUTPUT: enter no.of frames:3 enter the sequence of pages:enter the page:1 frame 1 enter the page: 2 frame 1 2 enter the page: 3 frame 1
  53. 53. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace 2 3 enter the page: 4 frame 4 2 3 enter the page: -1 page faults:4 Viva Questions: 1. what is paging,page,frame? 2. What is meant by page table? 3. What is the demand paging? 4. What is difference physical address and logical address?
  54. 54. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace MEMORY MANAGEMENT 8.Aim: Simulate Paging Technique of memory management. Description: • Goal: eliminate external fragmentation • Each process is a set of fixed size pages • Pages are stored in same size physical memory “frame” • Page table connects logical pages with physical frames • May still have internal fragmentation • Logical Address: page(p) and displacement/offset (d) in page • Physical address: frame (f) and displacement/offset (d) in frame • PTBR: page table base register • PTLR: page table length register • Byte „g‟: logical address = Page size: 2**n Logical address space : 2**m Page number: high-order m-n bits Page offset: low-order n bits Page fault details:
  55. 55. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace • Trap to OS –save user registers and process table • Determine that interrupt was a page fault check page reference and location in disk • Issue read from the disk to a free frame • Wait in queue for device • Wait for seek and /or latency • Begin transfer • While waiting, allocate CPU to other user • Interrupt from the disk • Save registers and process state of other user • Determine that interrupt was from disk • Correct page table • Wait for CPU to be allocated to this process again • Restore user registers, process state, new page table • RESUME execution PAGING Source code: #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> void main() { char *virtual_address,*ptr,c=':',dest[20]; int no_frames,page_table[20][1]={0},page,offset,frame; int i=0; clrscr(); printf("enter number of frames allocated to process:n"); scanf("%d",&no_frames); printf("nenter page table for the process:n"); for(i=0;i<no_frames;i++) { printf("enter page number that frame %d contain:",i); scanf("%d",&page_table[i][0]);
  56. 56. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace } clrscr(); for(i=0;i<no_frames;i++) { printf("format of virtual address is pppp:0000n"); printf("enter virtual addressn"); scanf("%s",virtual_address); ptr=strchr(virtual_address,c); strxfrm(dest,virtual_address,ptr-virtual_address+1); page=atoi(dest); virtual_address+=(ptr-virtual_address+1); offset=atoi(virtual_address); for(i=0;i<no_frames;i++) { if(page_table[i][0]==page) { frame=i; continue; } } printf("physical address=%d:%d",frame,offset); } getch(); } OUTPUT: enter no.of frames allocated to process:4 enter page table for the process enter page number that frame 0 contain:1111 enter page number that frame 1 contain:2222 enter page number that frame 2 contain:3333 enter page number that frame 3 contain:4444 format of virtual address is pppp:0000 enter virtual address 3333:9999
  57. 57. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace physical address=2:9999 Viva Questions: 1. What is fragmentation? 2. What is the technique that is used to overcome the drawback of external fragmentation? 3. What are types of fragmentation? 4. What is swapping? 5. What are contiguous and non contiguous memories?
  58. 58. OS LAB MANUAL CSE DEPARTMENT B.Tech III CSE Lakshma Reddy pace

×