Professional Documents
Culture Documents
com
R
O
D
L
W
U
T
N
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
TABLE OF CONTENTS
(As per syllabus)
S.N
o
1
2
3
Wee
k
TOPIC
Page
No.
System Requirements
Lab Objectives
5
8
4
5
D
L
R
O
Convert the BNF rules into Yacc form and write code to
generate abstract syntax tree.
A program to generate machine code
W
U
T
N
10
11
12
13
14
15
16
17
18
www.jntuworld.com
11
16
18
25
www.jntuworld.com
CD LAB PROGRAMS
System Requirements
1. Intel based destktop PC of 166MHz or faster processor with
at least 64 MB RAM and 100 MB free disk space.
2. C++ compiler and JDK kit.
D
L
R
O
W
U
T
N
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
Lab Objectives
1. To provide an Understanding of the language translation peculiarities by
designing complete translator for mini language.
2. To provide an understanding of the design aspect of operating system.
D
L
R
O
W
U
T
N
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
D
L
R
O
W
U
T
N
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{
putc(c,f2);
c=getc(f1);
}
putc(' ',f2);
ungetc(c,f1);
}
else if(c==' '||c=='\t')
printf(" ");
else if(c=='\n')
lineno++;
else
putc(c,f3);
}
fclose(f2);
fclose(f3);
fclose(f1);
printf("\nThe no's in the program are");
for(j=0;j<i;j++)
printf("%d",num[j]);
printf("\n");
f2=fopen("identifier","r");
k=0;
printf("The keywords and identifiersare:");
while((c=getc(f2))!=EOF)
{
if(c!=' ')
str[k++]=c;
else
{
str[k]='\0';
keyword(str);
k=0;
}
}
fclose(f2);
f3=fopen("specialchar","r");
printf("\nSpecial characters are");
while((c=getc(f3))!=EOF)
printf("%c",c);
printf("\n");
fclose(f3);
printf("Total no. of lines are:%d",lineno);
R
O
D
L
W
U
T
N
}
6
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
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
D
L
R
O
W
U
T
N
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
D
L
R
O
W
U
T
N
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
ECHO;
{if(!COMMENT)printf("\n\t%s is an ASSIGNMENT OPERATOR",yytext);}
\<= |
\>= |
\< |
== |
\> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}
D
L
%%
R
O
W
U
T
N
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
Input:
$vi var.c
#include<stdio.h>
main()
{
int a,b;
}
Output:
$lex lex.l
$cc lex.yy.c
$./a.out var.c
R
O
D
L
W
U
main (
)
T
N
BLOCK BEGINS
int is a KEYWORD
a IDENTIFIER
b IDENTIFIER
BLOCK ENDS
10
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
D
L
R
O
W
U
T
N
11
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
len=strlen(s);
if(lastentry+1>=MAX)
Error_Message("Symbpl table is full");
if(lastchar+len+1>=MAX)
Error_Message("Lexemes array is full");
lastentry=lastentry+1;
symtable[lastentry].token=tok;
symtable[lastentry].lexptr=&lexemes[lastchar+1];
lastchar=lastchar+len+1;
strcpy(symtable[lastentry].lexptr,s);
return lastentry;
}
/*void Initialize()
{
struct entry *ptr;
for(ptr=keywords;ptr->token;ptr+1)
insert(ptr->lexptr,ptr->token);
}*/
int lexer()
{
int t;
int val,i=0;
while(1)
{
t=getchar();
if(t==' '||t=='\t');
else if(t=='\n')
lineno=lineno+1;
else if(isdigit(t))
{
ungetc(t,stdin);
scanf("%d",&tokenval);
return NUM;
}
else if(isalpha(t))
{
while(isalnum(t))
{
buffer[i]=t;
t=getchar();
i=i+1;
if(i>=SIZE)
Error_Message("Compiler error");
}
buffer[i]=EOS;
if(t!=EOF)
D
L
R
O
W
U
T
N
12
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
ungetc(t,stdin);
val=look_up(buffer);
if(val==0)
val=insert(buffer,ID);
tokenval=val;
return symtable[val].token;
}
else if(t==EOF)
return DONE;
else
{
tokenval=NONE;
return t;
}
}
}
void Match(int t)
{
if(lookahead==t)
lookahead=lexer();
else
Error_Message("Syntax error");
}
void display(int t,int tval)
{
if(t=='+'||t=='-'||t=='*'||t=='/')
printf("\nArithmetic Operator: %c",t);
else if(t==NUM)
printf("\n Number: %d",tval);
else if(t==ID)
printf("\n Identifier: %s",symtable[tval].lexptr);
else
printf("\n Token %d tokenval %d",t,tokenval);
}
void F()
{
//void E();
switch(lookahead)
{
case '(' : Match('(');
E();
Match(')');
break;
case NUM : display(NUM,tokenval);
Match(NUM);
break;
R
O
D
L
W
U
T
N
13
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
case ID : display(ID,tokenval);
Match(ID);
break;
default : Error_Message("Syntax error");
}
}
void T()
{
int t;
F();
while(1)
{
switch(lookahead)
{
case '*' : t=lookahead;
Match(lookahead);
F();
display(t,NONE);
continue;
case '/' : t=lookahead;
Match(lookahead);
display(t,NONE);
continue;
default : return;
}
}
}
void E()
{
int t;
T();
while(1)
{
switch(lookahead)
{
case '+' : t=lookahead;
Match(lookahead);
T();
display(t,NONE);
continue;
case '-' : t=lookahead;
Match(lookahead);
T();
display(t,NONE);
continue;
default : return;
D
L
R
O
W
U
T
N
14
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
}
}
}
void parser()
{
lookahead=lexer();
while(lookahead!=DONE)
{
E();
Match(';');
}
}
main()
{
char ans[10];
printf("\n Program for recursive decent parsing ");
printf("\n Enter the expression ");
printf("And place ; at the end\n");
printf("Press Ctrl-Z to terminate\n");
parser();
}
Output:
R
O
D
L
W
U
T
N
15
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
D
L
<parser.y>
%{
R
O
/*This YACC specification file generates the LALR parser for the program
considered in experiment 4.*/
#include<stdio.h>
%}
%union
{
double dval;
}
W
U
T
N
%%
line: expr '\n'
{
printf("%g\n",$1);
}
;
expr: expr '+' term
| term
;
term: term '*' factor
| factor
;
factor: '(' expr ')'
{$$=$1 + $3 ;}
{$$=$1 * $3 ;}
{$$=$2 ;}
16
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
| DIGIT
;
%%
int main()
{
yyparse();
}
yyerror(char *s)
{
printf("%s",s);
}
Output:
D
L
$lex parser.l
$yacc d parser.y
$cc lex.yy.c y.tab.c ll lm
$./a.out
2+3
5.0000
R
O
W
U
T
N
17
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
5) Convert The BNF rules into Yacc form and write code to
generate abstract syntax tree.
<int.l>
%{
#include"y.tab.h"
#include<stdio.h>
#include<string.h>
int LineNo=1;
%}
identifier [a-zA-Z][_a-zA-Z0-9]*
number [0-9]+|([0-9]*\.[0-9]+)
%%
D
L
return IF;
return ELSE;
return WHILE;
int |
char |
float
return TYPE;
R
O
W
U
{identifier} {strcpy(yylval.var,yytext);
return VAR;}
{number}
\< |
\> |
\>= |
\<= |
==
T
N
{strcpy(yylval.var,yytext);
return NUM;}
{strcpy(yylval.var,yytext);
return RELOP;}
[ \t] ;
\n LineNo++;
. return yytext[0];
%%
18
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
<int.y>
%{
#include<string.h>
#include<stdio.h>
struct quad
{
char op[5];
char arg1[10];
char arg2[10];
char result[10];
}QUAD[30];
struct stack
{
int items[100];
int top;
}stk;
D
L
int Index=0,tIndex=0,StNo,Ind,tInd;
extern int LineNo;
%}
R
O
%union
{
char var[10];
}
%token <var> NUM VAR RELOP
%token MAIN IF ELSE WHILE TYPE
W
U
T
N
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
| CONDST
| WHILEST
;
DESCT: TYPE VARLIST
;
VARLIST: VAR ',' VARLIST
| VAR
;
ASSIGNMENT: VAR '=' EXPR{
strcpy(QUAD[Index].op,"=");
strcpy(QUAD[Index].arg1,$3);
strcpy(QUAD[Index].arg2,"");
strcpy(QUAD[Index].result,$1);
strcpy($$,QUAD[Index++].result);
}
;
R
O
D
L
W
U
T
N
CONDST: IFST{
Ind=pop();
sprintf(QUAD[Ind].result,"%d",Index);
Ind=pop();
sprintf(QUAD[Ind].result,"%d",Index);
}
| IFST ELSEST
;
IFST: IF '(' CONDITION ')' {
strcpy(QUAD[Index].op,"==");
strcpy(QUAD[Index].arg1,$3);
strcpy(QUAD[Index].arg2,"FALSE");
strcpy(QUAD[Index].result,"-1");
push(Index);
Index++;
20
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
}
BLOCK {
strcpy(QUAD[Index].op,"GOTO");
strcpy(QUAD[Index].arg1,"");
strcpy(QUAD[Index].arg2,"");
strcpy(QUAD[Index].result,"-1");
push(Index);
Index++;
}
;
ELSEST: ELSE{
tInd=pop();
Ind=pop();
push(tInd);
sprintf(QUAD[Ind].result,"%d",Index);
}
BLOCK{
Ind=pop();
sprintf(QUAD[Ind].result,"%d",Index);
}
;
D
L
R
O
W
U
T
N
21
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
strcpy(QUAD[Index].op,"GOTO");
strcpy(QUAD[Index].arg1,"");
strcpy(QUAD[Index].arg2,"");
strcpy(QUAD[Index].result,"-1");
push(Index);
Index++;
}
;
%%
extern FILE *yyin;
int main(int argc,char *argv[])
{
FILE *fp;
int i;
if(argc>1)
{
fp=fopen(argv[1],"r");
if(!fp)
{
printf("\n File not found");
exit(0);
}
yyin=fp;
}
yyparse();
printf("\n\n\t\t ----------------------------""\n\t\t Pos Operator Arg1 Arg2 Result" "\n\t\t
--------------------");
for(i=0;i<Index;i++)
{
printf("\n\t\t %d\t %s\t %s\t %s\t
%s",i,QUAD[i].op,QUAD[i].arg1,QUAD[i].arg2,QUAD[i].result);
}
printf("\n\t\t -----------------------");
printf("\n\n");
return 0;
}
void push(int data)
{
stk.top++;
if(stk.top==100)
{
printf("\n Stack overflow\n");
exit(0);
}
stk.items[stk.top]=data;
}
D
L
R
O
W
U
T
N
22
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
int pop()
{
int data;
if(stk.top==-1)
{
printf("\n Stack underflow\n");
exit(0);
}
data=stk.items[stk.top--];
return data;
}
void AddQuadruple(char op[5],char arg1[10],char arg2[10],char result[10])
{
strcpy(QUAD[Index].op,op);
strcpy(QUAD[Index].arg1,arg1);
strcpy(QUAD[Index].arg2,arg2);
sprintf(QUAD[Index].result,"t%d",tIndex++);
strcpy(result,QUAD[Index++].result);
}
yyerror()
{
printf("\n Error on line no:%d",LineNo);
}
D
L
R
O
W
U
Input:
$vi test.c
main()
{
int a,b,c;
if(a<b)
{
a=a+b;
}
while(a<b)
{
a=a+b;
}
if(a<=b)
{
c=a-b;
}
else
{
c=a+b;
T
N
23
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
}
}
Output:
$lex int.l
$yacc d int.y
$gcc lex.yy.c y.tab.c ll lm
$./a.out test.c
Pos
Operator
Arg1
Arg2
<
==
t1
GOTO
<
==
to
FALSE
FALSE
t2
10
b
t3
t3
10
J
<=
t4
11
==
t4
FALSE
15
12
13
t5
D
L
t1
R
O
W
U
T
N
to
t2
Result
GOTO
14
GOTO
15
16
t6
t5
c
17
t3
c
___________________________________________________
24
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
D
L
R
O
W
U
T
N
25
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
{
fscanf(fp1,"%s %s",operand1,result);
fprintf(fp2,"\n\t LOAD -%s,R1",operand1);
fprintf(fp2,"\n\t STORE R1,%s",result);
}
switch(op[0])
{
case '*': fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n \t LOAD",operand1);
fprintf(fp2,"\n \t LOAD %s,R1",operand2);
fprintf(fp2,"\n \t MUL R1,R0");
fprintf(fp2,"\n \t STORE R0,%s",result);
break;
case '+': fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n \t LOAD %s,R0",operand1);
fprintf(fp2,"\n \t LOAD %s,R1",operand2);
fprintf(fp2,"\n \t ADD R1,R0");
fprintf(fp2,"\n \t STORE R0,%s",result);
break;
case '-': fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n \t LOAD %s,R0",operand1);
fprintf(fp2,"\n \t LOAD %s,R1",operand2);
fprintf(fp2,"\n \t SUB R1,R0");
fprintf(fp2,"\n \t STORE R0,%s",result);
break;
case '/': fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n \t LOAD %s,R0",operand1);
fprintf(fp2,"\n \t LOAD %s,R1",operand2);
fprintf(fp2,"\n \t DIV R1,R0");
fprintf(fp2,"\n \t STORE R0,%s",result);
break;
case '%': fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n \t LOAD %s,R0",operand1);
fprintf(fp2,"\n \t LOAD %s,R1",operand2);
fprintf(fp2,"\n \t DIV R1,R0");
fprintf(fp2,"\n \t STORE R0,%s",result);
break;
case '=': fscanf(fp1,"%s %s",operand1,result);
fprintf(fp2,"\n\t STORE %s %s",operand1,result);
break;
case '>': j++;
fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n \t LOAD %s,R0",operand1);
fprintf(fp2,"\n\t JGT %s,label#%s",operand2,result);
label[no++]=atoi(result);
break;
R
O
D
L
W
U
T
N
26
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
D
L
R
O
W
U
T
N
Input:
$vi int.txt
=t1 2
[]=a 0 1
[]=a 1 2
[]=a 2 3
*t1 6 t2
+a[2] t2 t3
27
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
-a[2] t1 t2
/t3 t2 t2
uminus t2 t2
print t2
goto t2 t3
=t3 99
uminus 25 t2
*t2 t3 t3
uminus t1 t1
+t1 t3 t4
print t4
Output:
Enter filename of the intermediate code: int.txt
STORE t1,2
STORE a[0],1
STORE a[1],2
STORE a[2],3
R
O
D
L
LOAD t1,R0
LOAD 6,R1
ADD R1,R0
STORE R0,t3
LOAD a[2],R0
LOAD t2,R1
ADD R1,R0
STORE R0,t3
W
U
T
N
LOAD a[t2],R0
LOAD t1,R1
SUB R1,R0
STORE R0,t2
LOAD t3,R0
LOAD t2,R1
DIV R1,R0
STORE R0,t2
LOAD t2,R1
STORE R1,t2
LOAD t2,R0
JGT 5,label#11
28
www.jntuworld.com
www.jntuworld.com
CD LAB PROGRAMS
Label#11: OUT t2
JMP t2,label#13
Label#13: STORE t3,99
LOAD 25,R1
STORE R1,t2
LOAD t2,R0
LOAD t3,R1
MUL R1,R0
STORE R0,t3
LOAD t1,R1
STORE R1,t1
LOAD t1,R0
LOAD t3,R1
ADD R1,R0
STORE R0,t4
OUT t4
D
L
R
O
W
U
T
N
29
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
PART - B
OPERATING SYSTEMS
Experiment : 1
Simulate the following CPU Scheduling Algorithms
a) FCFS b) SJF c) Round Robin d) Priority
CPU SCHEDULING
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
CPU Scheduler
Selects from among the processes in memory that are ready to execute,
and allocates the CPU to one of them
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the
short-term scheduler; this involves:
switching context
switching to user mode
jumping to the proper location in the user
program to restart that program
Dispatch latency time it takes for the dispatcher to stop one process
and start another running
SCHEDULING CRITERIA
Waiting time amount of time a process has been waiting in the ready
queue
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Burst Time
24
3
3
24
27
30
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
If the two process having same burst time, consider the first coming
process in to consideration
Add the previous burst time value to wait variable
Continue till all the process are executed
Step 4: Calculate the average waiting time = wat variable / no.of process
Arrival Time
Burst Time
P1
0.0
P2
2.0
P3
4.0
P4
3.0
P1
0
P3
8
P2
P4
12
16
Process
Arrival Time
Burst Time
P1
0.0
P2
2.0
P3
4.0
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
P4
3.0
P1
0
P2
2
P3
4
P2
5
P4
7
P1
11
10
www.jntuworld.com
16
www.jntuworld.com
www.jwjobs.net
Process
aaa
Bbb
Ccc
Ddd
Eee
Burst time
4
3
2
5
1
11
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
INPUT :
Expected Output :
p_name
p_time w_time
aaa
bbb
ccc
ddd
10
eee
11
12
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Priority Scheduling
A priority number (integer) is associated
with each process
The CPU is allocated to the process with
the highest priority (smallest integer
highest priority)
Preemptive
nonpreemptive
13
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
ii.
Indexed allocation
In linked allocation it is difficult to maintain FAT so instead of that method
indexed allocation method is used. Indexed allocation method solves all the
problems in the linked allocation by bringing all the pointers together into one
location called index block.
Linked Allocation
Linked allocation of disk space overcomes all the problems of contiguous
allocation. In linked allocation each file is a linked list of disk blocks where the
disk blocks may be scattered anywhere on the disk. The directory contains a
pointer to the first and last blocks of the file.
Disadvantages : Space required to maintain pointers.
14
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Specification
Input Step-1
o Memory Size (memsize)
o Partition Size (parsize)
o
Process Step-1
15
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Partition
Number
Partition
Size
Process
Number
scheduled
3
Program Size
Internal
Fragment
Size
5
Process
Number
Process
Size Pratition
Number
Allocated
2
Partition
Size
Internal
Fragment
Size
Allocated
Flag
Process Step-2
o Allocate processes to Pratitions
o If the process Size is>Partition size Allocated Flag = o (over Size)
o If no partition exists Allocated flag =A and calculate the internal fragment
size. Update the process row and Pratition row
Process Step-3
o Print the Process table and Partition table
o
Process step-4
o This step is done a LOOP till the user wants to exist
o Input 1 =Continue,2=Stop
16
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
o If 2 Break
o Read the Size of the Program>partition size then Reject
o If there is a unallocated partition, allocate the a partition and update the
partition and process table
o If no partition is available, select the partition with maximum
fragmentation and update the process table and partition table
o Print process table and partition Table.
17
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
o
Process
Number
Process
Size
Partition
Size
Partition
Number
Allocation
3
External
Fragmentt
Size
5
Allocated
Flag
6
External
Fragment
Size
5
Process Step-4
o
o
o
o
o
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
19
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Files
Test
Data
Cont
Single-level directory
20
www.jntuworld.com
Record
www.jntuworld.com
www.jwjobs.net
In the two-level directory structure, each user has his own user file directory
(UFD). Each UFD has a similar structure but lists only the files of a single user.
Cat Do
Test
User1
User2
Data
User3
User3
Test
Data
Spell
Start
First
Bin
programme
Final
Count
21
www.jntuworld.com
Hex
www.jntuworld.com
www.jwjobs.net
Cat
Do
Test
Record List
Deadlock: Avoidance
unsafe
deadlock
safe
22
www.jntuworld.com
final
www.jntuworld.com
www.jwjobs.net
Bankers Algorithm
Banker: Example
Initially:
Available
A B
10 5
C
7
Later snapshot:
Max
Allocation
Need
Available
P0
P1
P2
P3
P4
ABC
753
322
902
222
433
ABC
010
200
302
211
002
23
www.jntuworld.com
ABC
743
122
600
011
431
ABC
332
www.jntuworld.com
www.jwjobs.net
Safety Algorithm
STEP 1:initialize
Work :=Available;
For I = 1,2.n
Finish[i] = false
STEP 2: find I such that both
a. finish[i] is false
b. need I <=work
if no such I, goto STEP 4
STEP 3:
Work := work+ allocation i
Finish [i] =true
Goto STEP 2
STEP 4:
If finish[i] = true for all I, system is in safe state
Banker: Safety Example
Using the previous Example, P1, P2, P3, P4,P0 satisfies criteria
Max
Allocation
= need
<=
work
Available
ABC
ABC
ABC
ABC
P1
322
200
122
332
P3
222
211
011
P4
433
002
431
P2
902
302
600
P0
753
010
743
7<<<initial system
24
www.jntuworld.com
332
532
743
745
10 4 7
10 5
www.jntuworld.com
www.jwjobs.net
ABC
Need
ABC
P0
753
010
743
23
0<<<
P1
322
3 0 2<<<
0 2 0<<<
P2
902
302
600
P3
222
211
011
P4
433
002
431
Is this safe? Yes: P1, P3, P4, P0, P2
Can P4 get (3, 3, 0)? No,(3,3,0) > (2, 3, 0) Available
Can P0 get (0, 2, 0)? (0,2, 0) < (2,3, 0) Available
Pretend: Available goes to (2, 1. 0)
But All needs greater than Available than Available IMPLIES NOT SAFE
Expected Output :
enter the no of processes::4
enter the max types of resources::3
enter the claim for the processes::
enter the allocation for the processes::
P1 P2 P3 P4
P1 P2 P3 P4
R1 3 6 3 4
R1 1 6 2 0
R2 2 1 1 2
R2 0 1 1 0
R3 2 3 4 2
R3 0 2 1 2
25
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Deadlock: Prevention
Break no preemption
Process 1 request resources already allocated to process
process I forfeits its current resources
if process 2 is waiting for other resources: process 2 forfeits
used for resources whose state is easily saved / restored
CPU registers and memory space
Break circular wait
Process request resources in increasing
Order
Expected Output :
enter the no of processes::4
enter the max types of resources::3
26
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
P1 P2 P3 P4
P1 P2 P3 P4
R1 3 6 3 4
R1 1 6 2 0
R2 2 1 1 2
R2 0 1 1 0
R3 2 3 4 2
R3 0 2 1 2
27
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
7
7
0
7
0
1
7
0
1
2
2
0
1
3
2
3
4
0
2
3
0
4
4
3
0
2
4
2
0
3
4
2
3
0
0
2
3
1
0
1
3
2
0
1
2
7
7
1
2
0
7
0
2
1
7
0
1
LFU algorithm:
The least frequently used (LFU) page replacement algorithm requires that the
page with the smallest count be replaced. The reason for this selection is that an
actively used page should have a large reference occurs.
28
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
29
www.jntuworld.com