OS & CD Lab Manual JOHN
OS & CD Lab Manual JOHN
& TECH
GARIVIDI
Vizianagaram Dist (AP)
Estd 2001
OPERATING SYSTEM
LABORATORY MANUAL
Bachelor of Technology
IN
COMPUTER SCIENCE ENGINEERING
Prepared By
Mrs. M. JOHN TIMOTHY
Assistant Professor, CSE
AVANTHI’S ST. THERESSA INSTITUTE OF ENGG. & TECH
GARIVIDI
Vizianagaram Dist (AP)
Estd 2001
CERTIFICATE
………………................................course during………………….……………………….
Course Outcomes:
By the end of the course, student will be able to
• Implement various scheduling, page replacement algorithms and algorithms related to deadlocks
• Design programs for shared memory management and semaphores
• Determine predictive parsing table for a CFG
• Apply Lex and Yacc tools
• Examine LR parser and generating SLR Parsing table
INDEX
PAGE
EXERCISE EXPERIMENT NAME DATE RREMARKS
NO.
Simulate the following CPU Scheduling Algorithms
2
a) FCFS
4
EXE - 1
b) SJF
6
c) Priority
8
d) Round Robin
EXE - 3
Simulate Bankers algorithm for Deadlock Avoidance 14
EXE - 6
Simulate Paging Technique of Memory Management 27
Part B
Design a lexical analyzer for given language .the lexical analyzer should
29
EXE - 7 ignore redundant spaces, tabs and new lines.
Implement the lexical analyzer using JLex, flex or other lexical analyzer
EXE - 8
32
generating tools.
EXE - 9
Design predictive parser for the given language 35
EXE - 10
Design a LALR bottom up parser for the given language 40
Convert the BNF rules into Yacc form and write code to generate abstract
EXE - 11 42
syntax tree.
PART A
a) FCFS:
AIM: A program to simulate the FCFS CPU scheduling algorithm
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
char pn[10][10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,n;
int totwt=0,tottat=0;
clrscr();
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Process Name, Arrival Time & Burst Time:");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0;i<n;i++)
{
if(i==0)
{
star[i]=arr[i];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
else
{
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
}
printf("\nPName Arrtime Burtime Start TAT Finish");
for(i=0;i<n;i++)
OUTPUT:
Input:
Output:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name, arrival time & execution time:");
flushall();
scanf("%s%d%d",pn[i],&at[i],&et[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(et[i]<et[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
st[i]=at[i];
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
OUTPUT:
Input:
Output:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name,arrivaltime,execution time & priority:");
flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
{
OUTPUT:
Input:
Output:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int et[30],ts,n,i,x=0,tot=0;
char pn[10][10];
clrscr();
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the time quantum:");
scanf("%d",&ts);
for(i=0;i<n;i++)
{
printf("enter process name & estimated time:");
scanf("%s %d",pn[i],&et[i]);
}
printf("The processes are:");
for(i=0;i<n;i++)
printf("process %d: %s\n",i+1,pn[i]);
for(i=0;i<n;i++)
tot=tot+et[i];
while(x!=tot)
{
for(i=0;i<n;i++)
{
if(et[i]>ts)
{
x=x+ts;
printf("\n %s -> %d",pn[i],ts);
et[i]=et[i]-ts;
}
else
if((et[i]<=ts)&&et[i]!=0)
{
x=x+et[i];
printf("\n %s -> %d",pn[i],et[i]);
et[i]=0;}
}
}
printf("\n Total Estimated Time:%d",x);
getch();
}
Output:
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p2 -> 3
MVT:
AIM: A program to simulate the MVT.
PROGRAM:
#include <stdio.h>
int main() {
int m = 0, m1 = 0, m2 = 0, p, count = 0, i;
if (m1 <= m) {
count = count + m1;
if (count <= m) {
printf("Memory allocated for process %d is: %d\n", i + 1, m1);
m2 = m - count;
printf("Remaining memory is: %d\n", m2);
m = m2;
} else {
printf("There is no further memory remaining.\n");
}
} else {
printf("Memory is not allocated for process %d.\n", i + 1);
}
PROGRAM:
#include <stdio.h>
int main() {
int m, p, s, p1;
int m1[4], i, f, f1 = 0, f2 = 0, fra1, fra2;
s = m / p;
printf("Each partition size is: %d\n", s);
if (m1[i] <= s) {
printf("Process is allocated in partition %d\n", i + 1);
fra1 = s - m1[i];
printf("Internal fragmentation for process is: %d\n", fra1);
f1 = f1 + fra1;
} else {
printf("Process not allocated in partition %d\n", i + 1);
fra2 = s;
f2 = f2 + fra2;
printf("External fragmentation for partition is: %d\n", fra2);
}
}
printf("\nProcess\tMemory\tAllocated Memory\n");
for (i = 0; i < p1; i++)
printf("%5d\t%5d\t%5d\n", i + 1, s, m1[i]);
f = f1 + f2;
printf("\nThe total number of fragmentation is: %d\n", f);
return 0;
}
}
PROGRAM:
#include <stdio.h>
int main() {
int n, r, i, j, k, p, u = 0, s = 0, m;
int block[10], run[10], active[10], newreq[10];
int max[10][10], resalloc[10][10], resreq[10][10];
int totalloc[10], totext[10], simalloc[10];
block[p] = 0;
run[p] = 0;
return 0;
}
#include <stdio.h>
int main() {
int n, r, i, j, k, p, u = 0, s = 0, m;
int block[10], run[10], active[10], newreq[10];
int max[10][10], resalloc[10][10], resreq[10][10];
int totalloc[10], totext[10], simalloc[10];
block[p] = 0;
run[p] = 0;
return 0;
}
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main() {
int cl[10][10], al[10][10], av[10], i, j, k, m, n, c, ne[10][10], flag = 0;
clrscr();
if (flag == 1) {
for (j = 0; j < n; j++) {
av[j] += al[i][j];
al[i][j] = 0;
}
}
}
flag = 1;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (al[i][j] != 0) {
flag = 0;
break;
}
}
if (flag == 0) {
break;
}
}
if (flag == 0) {
printf("\nUnsafe state\n");
} else {
printf("\nSafe state\n");
printf("Available matrix:\n");
for (i = 0; i < n; i++) {
printf("\t%d", av[i]);
}
}
getch();
}
a) FIFO:
AIM: A program to simulate FIFO Page Replacement Algorithm
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main() {
int a[5], b[20], p = 0, q = 0, m = 0, h, k, i, q1 = 1, j, u;
char f = 'F';
clrscr();
p = 0;
for (k = 0; k < q - 1; k++) {
if (b[i + 1] == a[k])
Enter numbers: 5 3 2 4 5 3 2 4 6 5 7 8
5 5
3 5 3
2 5 3 2 -->F
4 4 3 2
5 5 3 2
3 3 3 2 -->F
2 3 2 2
4 4 2 2
6 6 2 2 -->F
5 6 5 2
7 7 5 2
8 8 5 2 -->F
No of faults: 4
b) LRU:
AIM: A program to simulate LRU Page Replacement Algorithm
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main() {
int g = 0, a[5], b[20], p = 0, q = 0, m = 0, h, k, i, q1 = 1, j, u;
char f = 'F';
clrscr();
p = 0;
g = 0;
if (q1 == 3) {
for (k = 0; k < q - 1; k++) {
if (b[i + 1] == a[k])
p = 1;
}
OUTPUT:
1 1
2 1 2
3 1 2 3 -->F
4 4 2 3
1 4 2 1
2 4 2 1
5 5 2 1 -->F
1 5 2 1
2 5 2 1
3 3 2 1 -->F
4 4 2 1
5 4 2 1
No of faults: 3
c) Optimal:
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main() {
int pn[12], m[3] = {0, 0, 0}, m1[3], i, j, k;
int flag, f, pf = 0, z;
clrscr();
j = 0;
for (i = 0; i < 3; i++) {
while (j < 12) {
flag = 0;
for (k = 0; k < 3; k++) {
if (m[k] == pn[j]) {
flag = 1;
j++;
i--;
}
if (flag == 1)
break;
}
if (flag == 0) {
m[j] = pn[j];
flag = 1;
}
j++;
if (flag == 1)
break;
}
}
OUTPUT:
Enter pages: 2 3 4 2 1 3 7 5 4 3 2 5
No of faults: 6
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main() {
int np, ps, i;
int *sa;
clrscr();
getch();
// Free allocated memory to avoid memory leaks
for (i = 0; i < np; i++) {
free(sa[i]);
}
free(sa);
}
OUTPUT:
PART B
PROGRAM:
#include<string.h>
#include<ctype.h>
#include<stdio.h>
void keyword(char str[10])
{
if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||
strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",str)==0||
strcmp("double",str)==0||strcmp("static",str)==0||strcmp("switch",str)==0||
strcmp("case",str)==0)
printf("\n%s is a keyword",str);
else
printf("\n%s is an identifier",str);
}
main()
{
FILE *f1,*f2,*f3;
char c,str[10],st1[10];
int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;
printf("\nEnter the c program");/*gets(st1);*/
f1=fopen("input","w");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen("input","r");
f2=fopen("identifier","w");
f3=fopen("specialchar","w");
while((c=getc(f1))!=EOF)
{
if(isdigit(c))
{
tokenvalue=c-'0';
c=getc(f1);
while(isdigit(c))
{
tokenvalue*=10+c-'0';
c=getc(f1);
}
num[i++]=tokenvalue;
ungetc(c,f1);
}
else if(isalpha(c))
{
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);
}
OUTPUT:
Enter the C program
a+b*c
Ctrl-D
PROGRAM:
\( 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;
}
$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*/
PROGRAM:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 128
#define NONE -1
#define EOS '\0'
#define NUM 257
#define KEYWORD 258
#define ID 259
#define DONE 260
#define MAX 999
char lexemes[MAX];
char buffer[SIZE];
int lastchar=-1;
int lastentry=0;
int tokenval=DONE;
int lineno=1;
int lookahead;
struct entry
{
char *lexptr;
int token;
}symtable[100];
struct entry
keywords[]={"if",KEYWORD,"else",KEYWORD,"for",KEYWORD,"int",KEYWORD,
"float",KEYWORD,"double",KEYWORD,"char",KEYWORD,"struct",KEYWORD,"ret
urn",KEYWORD,0,0};
void Error_Message(char *m)
{
fprintf(stderr,"line %d, %s \n",lineno,m);
exit(1);
}
int look_up(char s[ ])
{
int k;
for(k=lastentry;k>0;k--)
if(strcmp(symtable[k].lexptr,s)==0)
return k;
return 0;
}
OUTPUT:
Program for recursive decent parsing
Enter the expression And place ; at the end
Press Ctrl-Z to terminate
a+b*c;
Identifier: a
Identifier: b
Identifier: c
Arithmetic Operator: *
Arithmetic Operator: +
2*3;
Number: 2
Number: 3
Arithmetic Operator: *
+3;
line 5,Syntax error
Ctrl-Z
<parser.l>
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
[0-9]+ {yylval.dval=atof(yytext);
return DIGIT;
}
\n|. return yytext[0];
%%
<parser.y>
%{
/*This YACC specification file generates the LALR parser for the program
considered in experiment 4.*/
#include<stdio.h>
%}
%union
{
double dval;
}
%%
| DIGIT
;
%%
int main()
{
yyparse();
}
yyerror(char *s)
{
printf("%s",s);
}
Output:
$lex parser.l
$yacc –d parser.y
$cc lex.yy.c y.tab.c –ll –lm
$./a.out
2+3
5.0000*/
PROGRAM:
<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]+)
%%
if return IF;
else return ELSE;
while return WHILE;
int |
char |
float return TYPE;
{identifier} {strcpy(yylval.var,yytext);
return VAR;}
{number} {strcpy(yylval.var,yytext) ;
return NUM;}
\< |
\> |
\>= |
\<= |
== {strcpy(yylval.var,yytext);
return RELOP;}
[ \t] ;
\n LineNo++;
. return yytext[0];
%%
< int.y>
%{
#include<string.h>
CODE: BLOCK
| STATEMENT CODE
| STATEMENT
;
STATEMENT: DESCT ';'
| ASSIGNMENT ';'
| CONDST
| WHILEST
;
DESCT: TYPE VARLIST
;
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();
Output:
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;
}
}
Output:
$lex int.l
$yacc –d int.y
$gcc lex.yy.c y.tab.c –ll –lm
$./a.out test.c
0 < a b to
1 == to FALSE 5
2 + a b t1
3 = t1 a
4 GOTO 5
5 < a b t2
6 == t2 FALSE 10
7 + a b t3
8 = t3 a
9 GOTO 5
10 <= a b t4
11 == t4 FALSE 15
12 - a b t5
13 = t5 c
14 GOTO 17
15 + a b t3
16 = t6 c
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int label[20];
int no=0;
int main()
{
FILE *fp1,*fp2;
char fname[10],op[10],ch;
char operand1[8],operand2[8],result[8];
int i=0,j=0;
printf("\n Enter filename of the intermediate code");
scanf("%s",&fname);
fp1=fopen(fname,"r");
fp2=fopen("target.txt","w");
if(fp1==NULL || fp2==NULL)
{
printf("\n Error opening the file");
exit(0);
}
while(!feof(fp1))
{
fprintf(fp2,"\n");
fscanf(fp1,"%s",op);
i++;
if(check_label(i))
fprintf(fp2,"\nlabel#%d",i);
if(strcmp(op,"print")==0)
{
fscanf(fp1,"%s",result);
fprintf(fp2,"\n\t OUT %s",result);
}
if(strcmp(op,"goto")==0)
{
fscanf(fp1,"%s %s",operand1,operand2);
fprintf(fp2,"\n\t JMP %s,label#%s",operand1,operand2);
label[no++]=atoi(operand2);
}
if(strcmp(op,"[]=")==0)
{
fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n\t STORE %s[%s],%s",operand1,operand2,result);
{
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);
Output:
Input:
$vi int.txt
=t1 2
[]=a 0 1
[]=a 1 2
[]=a 2 3
*t1 6 t2
+a[2] t2 t3
-a[2] t1 t2
/t3 t2 t2
uminus t2 t2
STORE t1,2
STORE a[0],1
STORE a[1],2
STORE a[2],3
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
LOAD a[t2],R0
LOAD t1,R1
SUB R1,R0
STORE R0,t2
LOAD t3,R0
LOAD t2,R1
DIV R 1,R0
STORE R0,t2
LOAD t2,R1
STORE R1,t2
LOAD t2,R0
JGT 5,label#11
Label#11: OUT t2
JMP t2,label#13
Label#13: STOR E t3,99
LOAD 25,R1
STORE R1,t2
LOAD t2,R0
LOAD t3,R1
LOAD t1,R1
STORE R1,t1
LOAD t1,R0
LOAD t3,R1
ADD R1,R0
STORE R0,t4
OUT t4