Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 58

JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY,ANANTAPUR.

III Year B.Tech. CSE -II Sem

OPERATING SYSTEMS AND COMPILER DESIGN LAB

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

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++ complier and JDK kit

1.
a.
b.
c.
d.
2.
a.
b.
c.
3.
4.
a.
b.
c.
d.
5.
6.
7.
a.
b.
c.
8.

Part-A (OS)
Simulate the following CPU scheduling algorithms
Round Robin
SJF
FCFS
Priority
Simulate all file allocation strategies
Sequential
Indexed
Linked
Simulate MVT and MFT
Simulate all File Organization Techniques
Single level directory
Two level
Hierarchical
DAG
Simulate Bankers algorithm for deadlock avoidance
Simulate Bankers Algorithm for Deadlock prevention
Simulate all page replacement algorithms
FIFO
LRU
LFU Etc.
Simulate Paging Technique of memory management.

Part-B (CD)
1.
2.
3.
4.

Lexical Analysis
Lexical Analyzer using LEX-tool
Compute FIRST() and FOLLOW() functions for the given language
Design predictive parser for the given language

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

Program:

Round Robin Scheduling Algorithm

#include<stdio.h>
#include<conio.h>
struct process
{
int at,ts,st,ft,wait,ts2,ta;
float nta;
}p[20];
main()
{
int i,j,slice,n;
float tamean=0,ntamean=0;
clrscr();
printf("Enter Number of Processes :: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Arrival Time for Process-%c : ",65+i);
scanf("%d",&p[i].at);
printf("\nEnter Service Time for process-%c : ",65+i);
scanf("%d",&p[i].ts);
}
printf("\nEnter Time Slice: ");
scanf("%d",&slice);
for(i=0;i<n+1;i++)
{
if(i==0)
p[i].ts2=n*slice;
else
p[i].ts2=p[i-1].ts2+(p[i-1].ts-slice);
if(i<n)
p[i].st=i*slice;
if(i>=1)
p[i-1].ft=p[i].ts2;
}
for(i=0;i<n;i++)
p[i].wait=(i*slice-p[i].at)+(p[i].ts2-(i+1)*slice);
for(i=0;i<n;i++)
{
p[i].ta=p[i].ft-p[i].at;
p[i].nta=(float)p[i].ta/p[i].ts;
tamean=tamean+p[i].ta;
ntamean=ntamean+p[i].nta;
}
tamean=(float)tamean/n;
ntamean=(float)ntamean/n;
printf("\n Process AT ST StT FT WT TA NTA\n");

for(i=0;i<n;i++)
{
printf("Process-%c%9d%9d%12d%12d%10d%6d
%10.4f",65+i,p[i].at,p[i].ts,p[i].st,p[i].ft,p[i].wait,p[i].ta,p[i].nta);
printf("\n");
}

printf("\nturn around mean is : %f",tamean);


printf("\nnorm.turn around mean is : %f",ntamean);
getch();
}
Output:
Enter Number of Processes:: 3
Enter Arrival Time for Process -A : 0
Enter Service Time for process-A : 5
Enter Arrival Time for Process -B : 3
Enter Service Time for process-B : 7
Enter Arrival Time for Process -C : 5
Enter Service Time for process-C : 9
Enter Time Slice: 4
Process
Process-A
Process-B
Process-C

AT
0
3
5

ST

StT

5
7
9

0
4
8

FT
13
16
21

WT

TA
8
6
7

NTA
13
13
16

2.6000
1.8571
1.7778

turn around mean is : 14.000000


norm.turn around mean is : 2.078307

Program:

Shortest Job First Scheduling Algorithm

#include<stdio.h>
#include<conio.h>
main()
{
int [10],sbt[10],swt[10],st[10],stt[10],sft[10],n,i,j,wt,tt,temp;
float avgwt,avgtt;
wt=0;tt=0;temp=0;st[1]=0;
clrscr();

printf("enter no.of jobs");


scanf("%d",&n);
printf("enter the burst times of jobs");
for(i=1;i<=n;i++)
scanf("%d",&sbt[i]);
for(i=1;i<=(n-1);i++)
for(j=i+1;j<=n;j++)
if((sbt[i]>sbt[j])&&(sbt[i]!=sbt[j]))
{
temp=sbt[i];
sbt[i]=sbt[j];
sbt[j]=temp;
}
for(i=1;i<=n;i++)
{
st[i+1]=st[i]+sbt[i];
sft[i]=st[i]+sbt[i];
if(i==1)
swt[i]=0;
else
swt[i]=swt[i-1]+sbt[i-1];
stt[i]=swt[i]+sbt[i];
wt=wt+swt[i];
tt=tt+stt[i];
}
avgwt=((float)wt/(float)n);
avgtt=((float)tt/(float)n);
printf("\nJOB sert
st
wt
ft
turt");
for(i=1;i<=n;i++)
printf("\nJ%d\t%d\t%d\t %d\t%d\t%d\n",i,sbt[i],st[i],sft[i],swt[i],stt[i]);
printf("\navg waiting time=%0.2f,turnover total
time=%0.2f",avgwt,avgtt);
getch();
}
Output:
enter no.of jobs3
enter the burst times of jobs4

JOB sert
st
wt
ft
turt
J1
4
0
4
0
4
J2
5
4
9
4
9
J3
6
9
15
9
15
avg waiting time=4.33,turnover total time=9.33
Program:
First-come-First-Served Scheduling Algorithm
#include<stdio.h>
#include<conio.h>
struct process
{
int at,ts,st,ft,ta;
float nta;
};
main()
{
struct process p[20];
int n,i,j;
float tamean=0,ntamean=0;
clrscr();
printf("\nEnter Number of Processes:: ");

scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Arrival Time for Process-%c :: ",65+i);
scanf("%d",&p[i].at);
printf("\nEnter Service Time for Process-%c :: ",65+i);
scanf("%d",&p[i].ts);
}
for(i=0;i<n;i++)
{
if(i==0)
p[i].st=p[i].at;
else
{
p[i].st=0;
for(j=0;j<i;j++)
p[i].st=p[i].st+p[j].ts;
}
p[i].ft=p[i].ts+p[i].st;
p[i].ta=p[i].ft-p[i].at;
p[i].nta=(float)p[i].ta/p[i].ts;
tamean=tamean+p[i].ta;
ntamean=ntamean+p[i].nta;
}
tamean=(float)(tamean/n);
ntamean=(float)(ntamean/n);
printf("\nProcess AT ST StT FT TA NTA");
for(i=0;i<n;i++)
printf("\n%3c%12d%10d%10d%10d%10d
%15f",65+i,p[i].at,p[i].ts, p[i].st,p[i].ft,p[i].ta,p[i].nta);
printf("\n\n Mean of Turn-around time : %f",tamean);
printf("\n\n Mean of Normalized turn-around time : %f",ntamean);
getch();
}

Output:
Enter Number of Processes:: 3
Enter Arrival Time for Process-A :: 0
Enter Service Time for Process-A :: 12
Enter Arrival Time for Process-B :: 2
Enter Service Time for Process-B :: 9
Enter Arrival Time for Process-C :: 6
Enter Service Time for Process-C :: 14
Process
A
B
C

AT
0
2
6

ST
2
9
14

StT
FT
TA
NTA
0
12
12
1.000000
12
21
19
2.111111
21
35
29
2.071429

Mean of Turn-around time: 20.000000


Mean of Normalized turn-around time: 1.727513

Program:

Priority Scheduling Algorithm

#include<stdio.h>
#include<conio.h>
struct process
{
int ts,pri,wait,ft;
}p[20];
main()
{
int n,pri1[20],i,j,temp,ft1[25];
clrscr();
printf("\n Enter Number of Processes:: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Service Time for Process-%c : ",65+i);
scanf("%d",&p[i].ts);
printf("\nEnter Priority for Process-%c : ",65+i);
scanf("%d",&p[i].pri);
}
for(i=0;i<n;i++)
pri1[i]=p[i].pri;

for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pri1[i]>pri1[j])
{
temp=pri1[i];
pri1[i]=pri1[j];
pri1[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(pri1[i]==p[j].pri)
{
if(i==0)
{
p[j].wait=0;
p[j].ft=p[j].ts;
ft1[i]=p[j].ft;
}
else
{
p[j].ft=ft1[i-1]+p[j].ts;
p[j].wait=ft1[i-1];
ft1[i]=p[j].ft;
}
}
}
}
printf("\nProcess ST
PRI
FT WT ");
for(i=0;i<n;i++)
{
printf("\nprocess-%c%10d%13d%14d%15d",65+i,p[i].ts,p[i].pri,p[i].ft,p[i].wait);
}
getch();
}
Output:
Enter Number of Processes:: 3
Enter Service Time for Process-A : 5
Enter Priority for Process-A : 2
Enter Service Time for Process-B : 8
Enter Priority for Process-B : 1
Enter Service Time for Process -C : 6
Enter Priority for Process-C : 3
Process

ST

PRI

FT

WT

process-A
process-B
process-C

5
8
6

2
1
3

13
8
19

8
0
13

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
1.
2.
3.
4.
5.

Indexed: direct access and no fragmentation


Bring all pointers into one location: the index block(IB)
Each file has its own IB
Ith entry in the IB points to the ith block
Suffers from wasted space(IB may not be full)

Linked Allocation
1. solves external fragmentation: can use any block for any file

2.
3.
4.
5.
6.
7.

solves pre-allocation internal fragmentation


good for sequential access: chase the pointer
not effective for direct access
where is the nth block of the file
requires space for pointers
if a pointer is bad, file is lost

/*Sequential file allocation*/


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int q=100,b[100],m;
main ()
{
int ch,i,j,no;
char fnm[20], tnm[20]="null";
struct FAT /* declaration of structure*/
{
char name[20];
int start;
int len;
}p[20];
clrscr( );
m=0;
printf("\n SIMULATION OF FILE ALLOCATION METHODS\n\n");
do
/* reading choices */
{
printf("\n\n Main Menu\n\n\t1.insertion \t2.deletion\n\t3.retrieval\n\t4.exit \n\nEnter
your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n enter file name");
scanf("%d",&(p[m].len));

while(1)
{
no=random(q);
if(b[no]==0)
{
for(j=no+1;j<no+p[m].len;j++)
if(b[j]==1)
break;
}
if(j==no+p[m].len)
break;
}
p[m].start=no;
for(i=no;i<no+p[m].len;i++)
b[i]=1;
printf("page table");

for(i=0;i<m;i++) /* printing file name*/


{
printf("\n%s\t%d\t%d",p[i].name,p[i].start,p[i].len);
printf("\n");
}
break;
case2:printf("enter file name you want to delete");
/* deleting file*/
scanf("%s",fnm);
for(i=0;i<m;i++)
{
if(strcmp(p[i].name,fnm)==0)
{
for(j=p[i].start ;j<(p[i].len+p[i].start);j++)
b[j]=0;
strcpy(p[i].name,"null");
p[i].start=-1;
p[i].len=-1;
printf("%s deleted successfully",fnm);
break;
}
}
for(i=0;i<m;i++)
{
printf("%s",p[i].name);
printf("%d\t%d\t",p[i].start,p[i].len);
printf("\n");
}
case 3:printf("enter file name you want to retrieve"); /* file retrieving */
scanf("%s",fnm);
printf("\nblocks allocated are \n");

for(i=0;i<m;i++)
{
if(strcmp(p[i].name,fnm)==0)
{
for(j=p[i].start;j<(p[i].start+p[i].len);j++)
printf("%d\t",j);
break;
}
}
break;
case 4:exit(0);
default:printf("INVALID CHOICE");
}
}while(1);
}

OUTPUT
SIMULATION OF FILE ALLOCATION METHODS
Main Menu
1.insertion
2.deletion
3.retrieval
4.exit
enter your choice :1
enter file name:q
enter length(in kb):12
page table
qq 46 12
Main Menu
1.insertion
2.deletion
3.retrieval
4.exit
enter your choice :1
enter file name : ww
enter length(in kb):3
page table
qq 46 12
ww 30 3
Main Menu
1.insertion
2.deletion
3.retrieval
4.exit
enter your choice :3
enter file name you want to retrieve ww
blocks allocated are
30 31 32

Main Menu
1.insertion
2.deletion
3.retrieval
4.exit
enter your choice :3
enter file name you want to delete ww
ww is deleted successfully
qq 46 12
null -1 -1
Main Menu
1.insertion 2.deletion
3.retrieval 4.exit enter your choice: 4

/*linked file allocation */


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
main()
{
struct FAT
/* declaration of structure */
{
char name[5];
int len,start,arr[10];
}p[20];
int ch,l,i,j,rn,v,m,q=100;
char fnm[5];
int b[100];
clrscr();
for(i=0;i<100;i++)
b[i]=0;
while(1)
{
Printf(" 1.create 2.delete data from file 3.retrive file 4.exit ");
choices */
Scanf("%d",& ch);
switch(ch)
{
case 1:
printf("enter the file name");
scanf("%s",&p[m].name);
printf("enter the length of the file");
scanf("%d",&p[m].len);
l=0;
for(j=0;j<p[m].len;j++)
{
do

/* reading

{
rn=random(q);
if(b[rn]==0)
{
p[m].arr =rn;
l++;
b[rn]=1;
break;
}
}while(b[rn]==1);
p[m].start=p[m].arr[0];
printf(" page table is");
printf("name start length");
for(i=0;i<m;i++)
printf("\n%s %d %d", p[i].name,p[i].start,p[i].len);
break;
case 2: printf("enter the file name that you want to delete"); /* deleting file*/
for(i =0; i<m;i++)
{
if(strcmp(p[i].name,fnm)==0)
{
for(j=0;j< p[i].len;j++)
{
v=p[i].arr[j] ;
b[v]=0;
p[i].arr=0;
}
p[i].len=-1;
p[i].start=-1;
strcmp( p[i].name,NULL);
}
}
printf("the page tableis :\n");
for(i=0;i<=m;i++)
{
printf("%s",p[i].name);
printf("%d %d",p[i].start,p[i].len);
}
break;
case 3: printf("enter the name of the file that you want to retrive"); /*file retrieving*/
scanf("%s",&fnm);
for(i=0; i<=m;i++)
{
if(strcmp(p[i].name,fnm)==0)
{
for (j=0;j<=p[i].len;j++)
printf("%d\t",p[i].arr[j]);
}
}

break;
case 4:exit(0);
default: printf("invalid");
}
}
getch();

Output:
Enter the choice:
1.create
2.delete data from file
3.retrive file
4.exit
1
Enter the file name : fdhgf
Enter the length of file:12
Page table is
Name
start
length
fdfgf
46
12
Enter the choice:
1.create
2.delete data from file
3.retrive file
4.exit
1
Enter the file name : kjkl
Enter the length of file:10
Page table is
Name
start
length
fdfgf
46
12
kjkl
71
10
Enter the choice:
1.create
2.delete data from file
3.retrive file
4.exit
3
enter the name of the file that you want to retrieve kjkl
71 79 60 12 21 63 47 19 41
Enter the choice:
1.create
2.delete data from file
3.retrive file
4.exit
2
Enter the file you want to delete : kjkl
Page table is
Name
start
length
fdfgf
46
12
NULL
-1
-1
Enter the choice:

1. Create

2.delete data from file

3.retrive file

4.exit

3.Aim: Simulate MVT and MFT


Description:
Memory management
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):


#include<stdio.h>
#include<conio.h>
main()
{
static int jobs[20][20],flag[10];
int ch;
static int i,k,nj,nb,tms;
clrscr();
printf("Enter time"); /* reading time */
scanf("%d",&tms);
printf("Enter no. of jobs"); /* reading no of jobs */
scanf("%d",&nj);
printf("Enter job information 1.jobid 2.jobsize");
for(i=0;i<nj;i++)
scanf("%d%d",&jobs[i][0],&jobs[i][1]);
for(i=0;i<nj;i++)
{
if(tms>=jobs[i][1])
{
tms=tms-jobs[i][1];
nb=nb+1;
flag[i]=1;
}
}
printf("Total memory space available which is not allocated is:%d\n",tms);
printf("Jobs which are not allocated:");
for(i=0;i<nj;i++)
if(flag[i] == 0)
printf("%d\t%d\n",jobs[i][0],jobs[i][1]);
if(nb!=nj)
{
while(1)
{
printf("enter jobid to deallocate:");
scanf("%d",&k);
for(i=0;i<nj;i++)
{
if(jobs[i][0] == k)
{
if(flag[i] ==1)
{
tms=tms+jobs[i][1];
flag[i]=2;
printf("Deallocated job %d\t%d\n", jobs[i][0],jobs[i][1]);
}

}
}
for(i=0;i<nj;i++)
{
if (tms>=jobs[i][1])
{
if(flag[i] == 0)
{
tms=tms-jobs[i][1];
flag[i]=1;
}
}
}
printf("Remaining memory is: %d",tms);
printf("Jobs which are not allocated are:");
for( i=0;i<nj;i++)
/* dellocating mamory*/
if(flag[i] ==0)
printf("%d\t%d\n", jobs[i][0],jobs[i][1]);
printf("Do you want to deallocate 1.Yes 2.No");
scanf("%d",&ch);
if(ch ==2)
break;
}
}
printf("Allocated jobs are:");
for(i=0;i<nj;i++)
if(flag[i]==1)
printf("%d\t%d\n",jobs[i][0],jobs[i][1]);
getch();
}

Output:
1)

Enter time: 100

1
2
3
4
5

2
3
2)

1
2
3
4

1
2
3

Enter no. of jobs: 5


Enter job information 1.jobid 2.jobsize
20
25
15
30
15
Total memory space available which is not allocated is: 10
Jobs which are not allocated: 5 15
Enter jobid to deallocate: 1
Deallocated job: 1 20
Remaining memory is: 15
Jobs which are not allocated are:
Do you want to deallocate 1.Yes 2.No : 1
Enter jobid to deallocate: 4
Deallocated job: 4 30
Remaining memory is 45
Jobs which are not allocated are:
Do you want to deallocate 1.Yes 2.No : 2
Allocated jobs are
25
15
5
15
Enter time: 100
Enter no. of jobs: 4
Enter job information 1.jobid 2.jobsize
25
30
40
25
Total memory space available which is not allocated is: 10
Jobs which are not allocated: 4 25
Enter jobid to deallocate: 3
Deallocated job: 3 40
Remaining memory is: 20
Jobs which are not allocated are:
Do you want to deallocate 1.Yes 2.No : 2
Allocated jobs are
25
30
25

Multiple Programming with Fixed Number of Tasks (MFT):


#include<stdio.h>
#include<conio.h>
void main()

{
int tms,element,nb,i,j,t,index,frag,ch,count=0;
static int jobs[20][20],sz[20][20],nj,s;
clrscr();
printf("enter total memory space"); /* reading memory */
scanf("%d",&tms); /* reading choices */
printf("enter choice\n1.equal partition 2.unequal partition\n");
scanf("%d",&ch);
if(ch==1)
{
printf("enter size of each block");
scanf("%d",&s);
nb=tms/s;
for(i=0;i<nb;i++)
scanf("%d",&sz[i][0]);
}
else
{
printf("enter no. of blocks");
scanf("%d",&nb);
printf("enter size of %d blocks");
for(i=0;i<nb;i++)
scanf("%d",sz[i][0]);
}
printf("enter no. of jobs"); /* reading no of jobs */
scanf("%d",&nj);
printf("enter job information 1.jobid 2.job size\n");
for(i=0;i<nj;i++)
scanf("%d%d",&jobs[i][0],&jobs[i][1]);
frag=0;
for(j=0;j<nj;j++)
{
if(sz[j][0]>=element && sz[i][0]<=t)
{
if(sz[j][1]!=1)
{
t=sz[j][0];
index=j;
}
}
}

if(sz[index][1]!=1)
{
sz[index][1]=1;
jobs[i][2]=2;
frag=frag+(t-element);
count++;
}

printf("total internal fragmentation : %d", frag);


printf("no. of free blocks: %d" , nb-count);
printf("the jobs which are not allocated");
if(count==nj)
printf(" 0");
for(i=0;i<nj;i++)
{
if(jobs[i][2]!=2)
printf("jobid ------%d\tjob size-----%d\n",jobs[i][0],jobs[i][1]);
}
getch();
}

Output:

1
2
3
4
5

a.
b.

Enter total memory space: 100


enter choice 1. equal partition 2. unequal partition 2
enter no. of blocks: 3
enter size of 3 blocks: 50 25 25
enter no. of jobs 4
enter job information 1.jodid 2. jobsize
25
30
26
20
25
total internal fragmentation : 9
no. of free blocks : 0
the jobs which are not allocated :
job id----4 jobsize----20
jobid----5
jobsize----25

4.Aim: Simulate all File Organization Techniques


Single level directory
Two level
Description:
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<stdlib.h>
#include<graphics.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);
printf("enter number of files");
scanf("&d",&count);
if(i<count)
// for(i=0;i<count;i++)
{
cleardevice();
setbkcolor(GREEN);
printf("enter %d file 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);
i++;
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[i]);
}
}
getch();
}

Output:
Enter number of files: 2
Enter file 1 name: it
Root
directory

it

Two Level Directory:


#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);
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 the name of ir file name %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(" how many files");
printf("(for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
}
else
(*root)->nc=0;
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]);
}
}
}

Output:
Enter the name of the dir file ROOT
Ho many users :2
Enter the name of the dir file: 007
How many files :2
Enter the name of the dir file007_1
Enter the name of the dir file007_2
Enter the name of the dir file xx
How many files: 2
Enter the name of dir file xx_1
Enter the name of dir file xx_2
ROOT

007

007_
1

xx

007_
2

Xx_1

Xx_2

5.Aim: To simulate bankers 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:


waiting for access to a critical section
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 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.
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>
main()
{
int a[10][10],c[10][10],r[10],av[10],ca[10][10],i,j,k,n,m,temp=0,tem,ch;
clrscr();
printf("enter no processes");
scanf("%d",&m);
printf("enter no of resources");
scanf("%d",&n);
printf("\nenter claim\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&c[i][j]);
printf("\nenter alocation matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nenter resourse vector\n");
for(i=0;i<n;i++)
scanf("%d",&r[i]);
k=0;
do
{
printf("claim\tallocation\tca\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%3d",c[i][j]);printf("\t");
for(j=0;j<n;j++)
printf("%3d",a[i][j]);printf("\t\t");

for(j=0;j<n;j++)
{
ca[i][j]=c[i][j]-a[i][j];
printf("%3d",ca[i][j]);
}
printf("\n");
}
temp=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
temp=temp+a[j][i];
av[i]=r[i]-temp;temp=0;
}
printf("\nresource vector: ");
for(i=0;i<n;i++)
printf("%3d",r[i]);
printf("\navailable vector: ");
for(i=0;i<n;i++)
printf("%3d",av[i]);
if(k==0)
printf("\n****initial state****\n");
else
printf("\n***p%d runs to completion*****\n",tem+1);
if(k<n)
{
temp=0;
for(i=0;i<m;i++)
{
lab:for(j=0;j<n;j++)
{
if(ca[i][j]==0)
temp++;
}
if(temp==n)
{
i++; temp=0;goto lab;
}
else
{
for(j=0;j<n;j++)
{
if(ca[i][j]<=av[j])
tem=i;
else
{
if(i>m)
{
printf("\nunshafe state");goto end;
}

else
{i++;goto lab;}
}
}
}
for(j=0;j<n;j++)
{
a[tem][j]=0;c[tem][j]=0;ca[tem][j]=0;
}
break;
}
}
else
{
printf("\nprocesses are completed");
goto end;
}
k++;
printf("continue press ZERO");
scanf("%d",&ch);
}while(ch==0);
end: getch();
}

Output:
enter no processes 3
enter no of resources 3
enter claim
322
613
314
enter allocation matrix
100
612
211
enter resource vector
936
claim
allocation
3 2 2
1 0 0
6 1 3
6 1 2
3 1 4
2 1 1
resource vector: 9 3 6
available vector: 0 1 3
****initial state****
continue press ZERO 0
enter allocation matrix

ca
2 2 2
0 0 1
1 0 3

100
612
211
enter resource vector
936

claim
3 2 2

6 1 3
3 1 4

allocation

0
6 1 2
2 1 1

1 0

ca

2 2 2
0 0 1
1 0 3

resource vector: 9 3 6
available vector: 0 1 3
****initial state****
continue press ZERO 0
claim
allocation
ca
3 2 2
1 0 0
2 2 2
0 0 0
0 0 0
0 0 0
3 1 4
2 1 1
1 0 3
resource vector: 9 3 6
available vector: 6 2 5
***p2 runs to completion*****
continue press ZERO 0
claim
allocation
ca
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
3 1 4
2 1 1
1 0 3
resource vector: 9 3 6
available vector: 7 2 5
***p1 runs to completion*****
continue press ZERO0
claim allocation
ca
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
resource vector: 9 3 6
available vector: 9 3 6
***p3 runs to completion*****
processes are completed

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

o
o

o
o

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.

BANKERS ALGORITHM FOR DEADLOCK PREVENTION

#include<stdio.h>
#include<conio.h>
int max[10][10],alloc[10][10],need[10][10],avail[10],i,j,p,r,finish[10]={0},flag=0;
main( )
{
clrscr( );
printf("\n\nSIMULATION OF DEADLOCK PREVENTION");
printf("Enter no. of processes, resources");
scanf("%d%d",&p,&r);
printf("Enter allocation matrix");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("enter max matrix");
for(i=0;i<p;i++)
/*reading the maximum matrix and availale matrix*/
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
printf("enter available matrix");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
fun();
/*calling function*/
if(flag==0)
{
if(finish[i]!=1)
{
printf("\n\n Failing :Mutual exclusion");
for(j=0;j<r;j++)
{ /*checking for mutual exclusion*/
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun();
printf("\n By allocating required resources to process %d dead lock is prevented ",i);
printf("\n\n lack of preemption");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])

avail[j]=need[i][j];
alloc[i][j]=0;
}
fun( );
printf("\n\n daed lock is prevented by allocating needed resources");
printf(" \n \n failing:Hold and Wait condition ");
for(j=0;j<r;j++)
{
/*checking hold and wait condition*/
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun( );
printf("\n AVOIDING ANY ONE OF THE CONDITION, U CAN PREVENT DEADLOCK");
}
}
getch( );
}
fun( )
{
while(1)
{
for(flag=0,i=0;i<p;i++)
{
if(finish[i]==0)
{
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[j])
continue;
else
break;
}
if(j==r)
{
for(j=0;j<r;j++)
avail[j]+=alloc[i][j];
flag=1;
finish[i]=1;
}
}
}
if(flag==0)
break;
}
}

Output:
SIMULATION OF DEADLOCK PREVENTION
Enter no. of processes, resources 3, 2
enter allocation matrix 2 4 5
4 5
Enter max matrix4 3 4
5 6 1
Enter available matrix2
5
Failing : Mutual Exclusion
by allocating required resources to process dead is prevented
Lack of no preemption
deadlock is prevented by allocating needed resources
Failing : Hold and Wait condition
BY AVOIDING ONE OF THE ABOVE CONDITION, YOU CAN PREVENT DEADLOCK

a.
b.

7.Aim: To simulate all page replacement algorithms


FIFO
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.
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 Ntimes 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


#include<stdio.h>

#include<conio.h>
#include<string.h>
void main()
{
char prs[40],fp[10],ps;
int fs,i,j,k=0,flg1,flg2,x=5,y,pfc=0;
clrscr();
printf("\n enter page reference string:");
gets(prs);
printf("\n enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
fp[i]='x';
clrscr();
printf("\n page replacement technique :: FIFO algorithm:");
printf("\n .............................................");
printf("\n F-Page Fault \t H- Page Hit \n");
for(i=0;i<strlen(prs);i++,x+=2)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
if(fp[j]==prs[i]){
ps='H';
flg1=1;
break;
}
if(flg1==0)
{
flg2=0;
for(j=0;j<fs;j++)
if(fp[j]=='x')
{
fp[j]=prs[i];
pfc++;
flg2=1;
break;
}
if(flg2==0)
{
pfc++;
fp[k]=prs[i];
k++;
if(k==fs)
k=0; }
}
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);

printf("--");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c",fp[j]);
}
y++;
gotoxy(x,y);
printf("--");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n \n\n\n\n Total page Faults=%d",pfc);
getch();
}

OUTPUT
enter page reference string :
232152453252
enter the frame size:
3
page replacement technique:: FIFO algorithm
................................................................................
F-page fault H-page hit
2 3 2 1 5 2 4 5 3 2 5 2
---------------------------2 2 2 2 5 5 5 5 3 3 3 3
x 3 3 3 3 2 2 2 2 2 5 5
x x x 1 1 1 4 4 4 4 4 2
--------------------------F FHFF F F H F H F F
total page faults=9

LFU PAGE REPLACEMENT ALGORITHM


#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{

char prs[40],fp[10],ps;
int fs,i,j,k,flg1,flg2,x=5,y,pfc=0,ru[10],min;
clrscr();
printf("\n ENTER THE PAGE REFERENCE STRING:");
gets(prs);
printf("\n enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
{
fp[i]='X';
ru[i]=0;
}
clrscr();
printf("\n PAGE REPLACEMENT TECHNIQUE ::LFU ALGORITHM \n");
printf("\n .......................................... \n");
printf("F-Page Fault \t H-Page Hit\n");
for(i=0;i<strlen(prs);i++,x+=5)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
{
if(fp[j]==prs[i])
{
ps='H';
(ru[j])++;
flg1=1;
break;
}
}
if(flg1==0)
{
pfc++;
flg2=0;

for(j=0;j<fs;j++)
{
if(fp[j]=='X')
{
fp[j]=prs[i];
ru[j]=1;
flg2=1;
break;
}
}
if(flg2==0)
{

min=0;
for(j=1;j<fs;j++)
{
if(ru[min]>=ru[j])
{
if(ru[min]>ru[j])
min=j;
else
{
for(k=0;k<i;k++)
{
if(prs[k]==fp[min])
break;
if(prs[k]==fp[j])
{
min=j;
break;
}}}}}
fp[min]=prs[i];
ru[min]=1;
}
}
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("-----");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c(%d)",fp[j],ru[j]);
}

y++;
gotoxy(x,y);
printf("-----");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n\n\n\n\n TOTAL PAGE FAULTS =%d",pfc);
getch();
}

OUTPUT
enter the page referrence string :232152453252
enter frame size :3

2
3
2
1
5
2
4
5
3
2
5
2
----------------------------------------------------------------------------------------------------2(1)
2(1)
2(2)
2(2)
2(2)
2(3) 2(3) 2(3) 2(3) 2(4) 2(4) 2(5)
X(0)
3(1)
3(1)
3(1)
5(1)
5(1) 5(2) 5(2) 5(2) 5(2) 5(3) 5(3)
X(0)
X(0)
X(0) 1(1)
1(1)
1(1) 4(1) 4(1) 3(1) 3(1)
3(1) 3(1)
----------------------------------------------------------------------------------------------------F
F
H
F
F
H
F
H
F
H
H
H
TOTAL PAGE FAULTS = 6

LRU PAGE REPLACEMENT ALGORITHM


#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char prs[40],fp[10],ps;
int fs,i,j,k,flg1,flg2,x=5,y,pfc=0,ru[10],min;
clrscr();
printf("enter the page reference string:");
gets(prs);
printf("enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
{
fp[i]='x';
ru[i]=0;

}
clrscr();
printf("PAGE REPLACEMENT TECHNIQUE::LRU algorithm\n");
printf("-----------------------------------\n");
printf("F-Page fault\tH-Page Hit\n");
for(i=0;i<strlen(prs);i++,x+=2)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
{
if(fp[j]==prs[i])
{
ps='H';
ru[j]=i;
flg1=1;
break;
}
}
if(flg1==0)
{
pfc++;
flg2=0;
for(j=0;j<fs;j++)
{
if(fp[j]=='X')
{
fp[j]=prs[i];
ru[j]=i;
flg2=1;
break;
}
}
if(flg2==0)
{
min=0;
for(j=1;j<fs;j++)
{
if(ru[min]>ru[j])
min=j;
}
fp[min]=prs[i];
ru[min]=i;
}
}
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("- -");
y++;
for(j=0;j<fs;y++,j++)

{
gotoxy(x,y);
printf("%c",fp[j]);
}
y++;
gotoxy(x,y);
printf("--");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n\n\n\n\n\n total page faults=%d",pfc);
getch();
}
OUTPUT:
enter the page referrence string :232152453252
enter frame size :3
2 3 2 1 5 2 4 5 3 2 5 2
--------------------------------------------2 3 3 3 5 5 5 5 5 5 5 5
X X 2 2 2 2 2 2 3 3 3 3
X X X 1 1 1 4 4 4 2 2 2
--------------------------------------------F F F F F H F H F F H H
TOTAL NO OFPAGE FAULTS=8
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:
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
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int size,m,n,pgno,pagetable[3]={5,6,7},i,j,frameno;
double m1;
int ra=0,ofs;
clrscr();
printf("Enter process size (in KB of max 12KB):");/*reading memeory size*/
scanf("%d",&size);
m1=size/4;
n=ceil(m1);
printf("Total No. of pages: %d",n);
printf("\nEnter relative address (in hexadecimal notation eg.0XRA) \n");
//printf("The length of relative Address is : 16 bits \n\n The size of offset is :12
bits\n");
scanf("%d",&ra);
pgno=ra/1000;
/*calculating physical address*/
ofs=ra%1000;
printf("page no=%d\n",pgno);
printf("page table");
for(i=0;i<n;i++)
printf("\n %d [%d]",i,pagetable[i]);

frameno=pagetable[pgno];
printf("\n Equivalent physical address : %d%d",frameno,ofs);
getch();
}

Output:
Enter process size(in KB of max 12KB) : 12
Total no. of pages : 3
Enter relative address (in hexadecimal notation eg.0XRA): 2643

0
1
2

Page no=2
Page table
[5]
[6]
[7]
Equivalent physical address: 7643

LAXICAL ANALYSIS
9.Aim:To write a program for dividing the given input program into exemes.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int i,j,k,p,c;
char s[120],r[100];
char par[6]={'(',')','{','}','[',']'};
char sym[9]={'.',';',':',',','<','>','?','$','#'};
char key[9][10]={"main","if","else","switch","void","do","while","for","return"};
char dat[4][10]={"int","float","char","double"};
char opr[5]={'*','+','-','/','^'};
FILE *fp;
clrscr();
printf("\n\n\t enter the file name");
scanf("%s",s);
fp=fopen(s,"r");
c=0;
do {
fscanf(fp,"%s",r);
getch();
for(i=0;i<6;i++)
if(strchr(r,par[i])!=NULL)
printf("\n paranthesis :%c",par[i]);
for(i=0;i<9;i++)

if(strchr(r,sym[i])!=NULL)
printf("\n symbol :%c",sym[i]);
for(i=0;i<9;i++)
if(strstr(r,key[i])!=NULL)
printf("\n keyword :%s",key[i]);
for(i=0;i<4;i++)
if((strstr(r,dat[i])&&(!strstr(r,"printf")))!=NULL)
{
printf("\n data type :%s",dat[i]);
fscanf(fp,"%s",r);
printf("\n identifiers :%s",r);
}
for(i=0;i<5;i++)
if(strchr(r,opr[i])!=NULL)
printf("\n operator :%c",opr[i]);
p=c;
c=ftell(fp);
} while(p!=c);
return 0;}
OUTPUT:

Enter the C program


a+b*c

Ctrl-D

The nos in the program are:

The keywords and identifiers are:


a is an identifier and terminal
b is an identifier and terminal
c is an identifier and terminal

Special characters are:


+*

Total no. of lines are: 1

10)Implement the Lexical Analyzer Using Lex Tool.


/* program name is lexp.l */
%{
/* program to recognize a c program */
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* { printf("\n%s is a PREPROCESSOR DIRECTIVE",yytext);}
int |
float |
char |
double |
while |
for |
do |
if |
break |
continue |
void |
switch |
case |
long |
struct |
const |
typedef |
return |
else |
goto
{printf("\n\t%s is a KEYWORD",yytext);}
"/*" {COMMENT = 1;}
/*{printf("\n\n\t%s is a COMMENT\n",yytext);}*/

"*/" {COMMENT = 0;}


/* printf("\n\n\t%s is a COMMENT\n",yytext);}*/
{identifier}\( {if(!COMMENT)printf("\n\nFUNCTION\n\t%s",yytext);}
\{ {if(!COMMENT) printf("\n BLOCK BEGINS");}
\} {if(!COMMENT) printf("\n BLOCK ENDS");}
{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}
\".*\" {if(!COMMENT) printf("\n\t%s is a STRING",yytext);}

[0-9]+ {if(!COMMENT) printf("\n\t%s is a NUMBER",yytext);}


\)(\;)? {if(!COMMENT) printf("\n\t");ECHO;printf("\n");}
\(
=

ECHO;
{if(!COMMENT)printf("\n\t%s is an ASSIGNMENT OPERATOR",yytext);}

\<= |
\>= |
\< |
== |
\>
{if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}
%%
int main(int argc,char **argv)
{
if (argc > 1)
{
FILE *file;
file = fopen(argv[1],"r");
if(!file)
{
printf("could not open %s \n",argv[1]);
exit(0);
}
yyin = file;
}
yylex();
printf("\n\n");
return 0;
}
int yywrap()
{
return 0;
}

Input:
$vi var.c
#include<stdio.h>
main()
{
int a,b;
}

Output:
$lex lex.l
$cc lex.yy.c
$./a.out var.c
#include<stdio.h> is a PREPROCESSOR DIRECTIVE
FUNCTION
main (
)
BLOCK BEGINS
int is a KEYWORD
a IDENTIFIER
b IDENTIFIER
BLOCK ENDS

11.Aim:

Compute FIRST() and FOLLOW() for the given language

Program:
a) Computation of FIRST(x)
#include<stdio.h>
#include<string.h>
char res[10] = {"
"},first[10];
int l=0,count=0,j,term=0;
FILE *fp1;
main()
{
FILE *fp;
int i=0,k=0,n,a[10],set,tem[10]={0},t;
char ch,s;
printf("ENTER THE PRODUCTIONS .. \n");
fp = fopen("input.txt","w");
while(( ch = getchar() )!= EOF)
putc(ch,fp);
fclose(fp);
fp = fopen("input.txt","r");
/* calculation of production starting variables */
while(!(feof(fp)))
{
ch = fgetc(fp); if(feof(fp)) break; first[l++] = ch;
count++;
a[i++] = count;
while(ch!='\n') { count++; ch=fgetc(fp); }
count++;
}
rewind(fp);
n=l;
clrscr();
j=0; l=0;
while(!(feof(fp)))
{
ch = fgetc(fp);
if(feof(fp)) break;
while(ch != '\n' )
{
ch =fgetc(fp);
if(count==1) /* it comes the string after > or | */
{
if( ((ch >= 'a')&& ( ch <= 'z')) || ch == '+' || ch== '-' || ch=='*'||ch=='/'||
ch=='^'||ch==')'||ch=='('||(ch=='^')||(ch=='#'))

{
if(term!=1 || ch!='#')
unione(ch,j++);
if( (term==1) && (ch=='#') ) term=2; /* term=1 represents it is a subproduction,term=2 means that sub-production has nullvalue*/
count=0; }
else
{ tem[++k] = ftell(fp); set=1; } /* if a non-terminal occurs */
}
if( ch == '>' || ch == '|')
{ count =1; }
if(set==1) /* its a non-terminal production */
{
for(i=0;i<n;i++)
{
fseek(fp,a[i]-1,0); s= fgetc(fp);
if(s==ch) { term=1; break; }
}
count=0;
set=0;
}
}/* while loop for ch!='\n' */
if(tem[k]!=0)
/* for retrieving previous production */
{
t = tem[k]-1; tem[k--]= 0;
fseek(fp,t,0);
if(term==2) count=1;
fgetc(fp); ch=fgetc(fp);
if( ( k==0 && term==2) && ( ch=='\n' || ch=='|'))
unione('#',j++);
fseek(fp,t,0);
}
else
j=print();
}
getch();
}
unione(char ch,int j)
/* for removing duplicates */
{
int i;
for(i=0;i<j;i++)
if(res[i]==ch)
return;
res[++j] = ch;
}
print()
/* for the printing */
{
static int k=0;
if(k==0)

{fp1 = fopen("output.txt","w"); k=1; }


printf(" First[%c] ==",first[l]); fputc(first[l++],fp1);
for(j=0;res[j]!='\0';j++)
{ printf("%c",res[j]);
fputc(res[j],fp1); }
printf("\n");
for(j=0;res[j]!='\0';j++)
res[j]=' ';
count=0; /* it indicates the start of another production */
term=0;
fputc('\n',fp1);
return(0);
}

Input:
Enter the no of productions 2

Enter the productions


E AA
A +

Output:
FIRST(E) = {+}
FIRST(A) = {+}

Program:

b) Computation of FOLLOW(A)
#include<stdio.h>
#include<conio.h>
char ch,first[10],stack[10];
int i,j,k;
main()
{
FILE *fp;
clrscr();
fp=fopen("in.txt","w");
printf("Enter the productions \n");
while(((ch=getchar())!='@'))
putc(ch,fp);
fclose(fp);
fp=fopen("in.txt","r");
i=0;
while(!(feof(fp)))
{
ch=fgetc(fp);
if(feof(fp))
break;
first[i++]=ch;
while(ch!='\n')
ch=fgetc(fp);
}
rewind(fp);
i=0;j=0;
while(first[i]!='\0')
{
ch=fgetc(fp);
if(ch==first[i])
stack[j]='$';
else
while(!(feof(fp)))
{
while(ch!='>')
ch=fgetc(fp);
while(ch!=first[i])
{
if(feof(fp))
goto down;
ch=fgetc(fp);
}
ch=fgetc(fp);
stack[j]=ch;
down:
j++;
}

print();
i++;
}
getch();
}
print()
{
printf("FOLLOW(%c)={",first[i]);
for(k=0;stack[k]!='\0';k++)
printf("%c",stack[k]);
printf("}\n");
}

Input:
Enter the productions
S->aAc
A->b

Output:
FOLLOW(s)={$}
FOLLOW(A)={c}

Aim:

Design Predictive Parser for the given language

Program:

/* Execute first.c and follow.c before compiling this */


#include<stdio.h>
char ter[20];
main()
{
FILE *fp,*fp1,*fp2;
char ch,prod[20],ch1,term1[20],ch2;
int i=0,set=0,j,k=0,row=2;;
fp = fopen("input.txt","r");
ch = fgetc(fp);
/* placing the terminals */
while( !(feof(fp)) )
{
if( set==1 )
if( (ch >= 'a')&&(ch<='z')||(ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')||
(ch=='^')||ch==')'||ch=='(')
unione(ch);
ch=fgetc(fp); if(ch=='>') set=1; if(ch=='\n') set=0;
}
clrscr();
/* include $ into terminals */
for(i=0;ter[i]!='\0';i++); ter[i++] = '$'; ter[i] = '\0';
for(i=0,j=10;ter[i]!='\0';i++,j+=10)
{ gotoxy(j,row);
printf("\t%c",ter[i]); }
printf("\n================================================
===============================");
row++;
printf("\n\n"); row++;
rewind(fp);
j=0; set=1;
while( !(feof(fp)) )
{
ch= fgetc(fp); if(feof(fp)) break;
if(set==1) { printf("%c",ch);}
prod[j++] = ch;
k=0;
if(set==2)
{
if( (ch >= 'a')&&(ch<='z')||(ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')||
(ch=='^')||(ch=='(') || ch==')')
term1[k++] = ch;
/* if # then follow */

else if(ch=='#')
{
fp2 = fopen("cfollow.txt","r");
while(!(feof(fp2)) )
{
ch2 = fgetc(fp2);
if(ch2==prod[0])
{ ch2 = fgetc(fp2);
while(ch2!='\n')
{ term1[k++] = ch2; ch2 = fgetc(fp2); }
}
} /* while */
}
else
{ fp1 = fopen("output.txt","r");
while(!(feof(fp1)) )
{
if( ch == (ch1=fgetc(fp1)) )
{ set =3; fgetc(fp1); }
if( set==3)
{
while( ch1!='\n' && ch1!='|')
{ if(feof(fp1)) break; ch1=fgetc(fp1);
if( ch1!='\n' && ch1!='|') term1[k++] = ch1; }
term1[k]='\0';
break;
}
}
}
while(ch!='\n' && ch!='|' )
{ ch=fgetc(fp); if( ch!='\n' && ch!='|' ) prod[j++]=ch;}
prod[j]='\0';
/* placing the productions in the respect terminals */
for(k=0,row=row+1;term1[k]!='\0';k++)
for(i=0;ter[i]!='\0';i++)
if(term1[k]==ter[i])
{
gotoxy((i+1)*10+5,row);
for(j=0;prod[j]!='\0';j++)
printf("%c",prod[j]); }
printf("\n");
while(k>0)
{ term1[k] = '\0'; k--; }
}
if(ch=='|')

{ while(prod[j]!='>')
prod[j--]='\0';
j++; set=2; }
else if(ch=='\n')
{ while(j>0) prod[j--]='\0';
printf("------------------------------------------------------------------------------\n");
row++; set=1;
}
if(ch=='>') set=2;

}
}
unione(char ch)
{
static int i=0;
int j=0;
for(;j<i;j++)
if(ter[j] == ch)
break;
if(j>=i)
{ ter[j]=ch; i++; }
}

Input:
E->TA
A->+TA|#
T->FB
B->*FB|#
F->(E)|i
Output:
E (i
A +#
T (i
B *#
F (i

follow.txt
E )$
A )$
T +)$
B +)$
F*+)$
+
*
(
)
i
$
================================================
E->
E->TA
E->TA
-----------------------------------------------------------------------A->
A->TA
A->#
A->#
-----------------------------------------------------------------------T->
T->FB
T->FB
-----------------------------------------------------------------------B->
B-># B->*FB
B->#
B->#
-----------------------------------------------------------------------F->
F(E)
F->i

You might also like