1. SS &os Progs
1a
%{
#include<stdio.h>
int sc=0,lc=0,wc=0,cc=0;
%}
%%
[^ t n]+ {wc++;cc+=yyleng;}
n {lc++;}
" " {sc++;}
.;
%%
yywrap()
{
return 1;
}
main()
{
yyin=fopen("nag.txt","r");
yylex();
fclose(yyin);
printf("character count=%dn",cc);
printf("word count=%dn",wc);
printf("space count=%dn",sc);
printf("line count=%dn",lc);
}
============================output==================================
nag.txt
------nagarjun
atria coll
5th sem
lex lab
student
------------------------------------------------------------------yadavakings@ubuntu:~$ vi 1a.l
yadavakings@ubuntu:~$ lex 1a.l
yadavakings@ubuntu:~$ cc lex.yy.c
yadavakings@ubuntu:~$ ./a.out
character count=36
word count=8
space count=3
line count=5
1b.
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
2. %{
#include<stdio.h>
int flag=0,n=0,cmt=0;
%}
%%
"//".* {cmt++;}
"/*".*"*/" {cmt++;}
"/*".* {flag=1;n++;}
"n" {if (flag==1)n++;else echo;}
.*"*/"{if(flag==1) {cmt+=n;flag=0;n=0;}}
. {if (flag==0)ECHO;}
%%
yywrap()
{
return 1;
}
main()
{
yyin=fopen("nag.c","r");
yyout=fopen("out.txt","w");
yylex();
fclose(yyin);
fclose(yyout);
printf("number of comment lines= %d",cmt);
}
=============================output==========================================
==========
nag.c
---#include<stdio.h>
//nagarjun
/*atria collstudent*/
/*5th sem*/
int a,b,c;
a=b+c;
-------------------------------------------------------------------------yadavakings@ubuntu:~$ vi 1b.l
yadavakings@ubuntu:~$ lex 1b.l
yadavakings@ubuntu:~$ cc lex.yy.c
yadavakings@ubuntu:~$ ./a.out
number of comment lines= 3
out.txt
------#include<stdio.h>
int a,b,c;
a=b+c;
2a.
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
3. %{
#include<stdio.h>
int id=0,op=0,bc=0,i=0,j=0,x,flag=0;
char opa[10];
int ida[10];
%}
%%
"(" {bc++;flag=1;}
")" {if(flag==1) {bc--;flag=0;} else bc--;}
"+"|"-"|"*"|"/" {op++;opa[i++]=*yytext;}
[0-9]+ {id++;ida[j++]=atoi(yytext);}
. ;
%%
yywrap()
{
return 1;
}
main()
{
printf("enter the expression");
yylex();
if((flag!=0)||(bc<0)||(id-op)!=1)
printf("invalid expression");
else
{
printf("valid expression");
printf("n list of operators are");
for(x=0;x<i;x++)
printf("%c",opa[x]);
printf("n list of identifiers");
for(x=0;x<j;x++)
printf("%d",ida[x]);
}
}
=========================output==============================================
=================
yadavakings@ubuntu:~$ vi 2a.l
yadavakings@ubuntu:~$ lex 2a.l
yadavakings@ubuntu:~$ cc lex.yy.c
yadavakings@ubuntu:~$ ./a.out
enter the expression 1+
invalid expression
enter the expression (1+2*3/4-9)
valid expression
list of operators are +*/list of identifiers 12349
2b.
%{
#include<stdio.h>
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
4. int flag=0;
%}
%%
"so"|"and"|"because"|"or" {flag=1;}
. ;
%%
yywrap()
{
return 1;
}
main()
{
printf("enter the sentence");
yylex();
if(flag==1)
printf("compound sentence");
else
printf("simple sentence");
}
==================================output=====================================
====
yadavakings@ubuntu:~$ vi 2b.l
yadavakings@ubuntu:~$ lex 2b.l
yadavakings@ubuntu:~$ cc lex.yy.c
yadavakings@ubuntu:~$ ./a.out
enter the sentence bus and me
compound sentence
enter the sentence Nagarjun yadav
simple sentence
3a.
%{
#include<stdio.h>
int id=0,flag=0;
%}
%%
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
5. int|float|char|double {flag=1;}
[0-9][_a-zA-Z] ;
";" {flag=0;}
[_a-zA-Z]+ {if(flag==1)
.1 n; id++;}
%%
yywrap()
{
return 1;
}
main()
{
yyin=fopen("naga.txt","r");
yylex();
fclose(yyin);
printf("number of identifiers=%d",id);
}
============================output===========================
naga.txt
-------int a,b,c;
char d,e;
yadavakings@ubuntu:~$
yadavakings@ubuntu:~$
yadavakings@ubuntu:~$
yadavakings@ubuntu:~$
vi 3a.l
lex 3a.l
cc lex.yy.c
./a.out
number of identifiers=5
yacc
4a.
%{
#include<stdio.h>
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
6. int valid=0;
%}
%token NUM
%left '+''-'
%left '*''/'
%left '('')'
%%
E:E'+'E|E'-'E|E'*'E|E'/'E|'('E')'|'-'E|NUM
%%
void yyerror()
{
valid=1;
}
int yywrap()
{
return 1;
}
int main()
{
printf("enter an expressionn");
yyparse();
if(!valid)
printf("valid expression");
else
printf("invalid expression");
}
%{
#include"y.tab.h"
%}
%%
[0-9]+
{return NUM;}
.
{return yytext[0];}
%%
================================output================================
yadavakings@ubuntu:~$ vi 4a.l
yadavakings@ubuntu:~$ lex 4a.l
yadavakings@ubuntu:~$ yacc -d 4a.y
yadavakings@ubuntu:~$ cc y.tab.c lex.yy.c
yadavakings@ubuntu:~$ ./a.out
enter an expression (1+2)
valid expression
enter an expression (1
invalid expression
4b.
%{
#include<stdio.h>
int valid=0;
%}
%token ALPHA
%%
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
7. E: ALPHA
%%
void yyerror()
{
valid=1;
}
int yywrap()
{
return 1;
}
int main()
{
printf("enter an expressionn");
yyparse();
if(!valid)
printf("valid expression");
else
printf("invalid expression");
}
%{
#include"y.tab.h"
%}
%%
[a-zA-Z][a-zA-Z0-9]*
{return ALPHA;}
.
{return yytext[0];}
%%
============================output===========================-yadavakings@ubuntu:~$ vi 4b.l
yadavakings@ubuntu:~$ lex 4b.l
yadavakings@ubuntu:~$ yacc -d 4b.y
yadavakings@ubuntu:~$ cc y.tab.c lex.yy.c
yadavakings@ubuntu:~$ ./a.out
enter an expression naga
valid expression
enter an expression 12123
invalid expression
enter an expression a123
valid expression
5a.
%{
#include<stdio.h>
int valid=1;
%}
%token NUM
%left'+''-'
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
8. %left'*''/'
%left'('')'
%nonassoc UMINUS
%%
expr:e{if(valid)printf("result%d",$$);return 0;}
e:e'+'e{$$=$1+$3;}
|e'-'e{$$=$1-$3;}
|e'*'e{$$=$1*$3;}
|e'/'e{if($3==0)valid=0;else $$=$1/$3;}
|'-'e%prec UMINUS{($$=-$2);}
|'('e')'{$$=$2;}
|NUM{$$=$1;}
%%
yyerror()
{
valid=0;
}
int yywrap()
{
return 1;
}
int main()
{
printf("enter expression");
yyparse();
}
%{
#include"y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return NUM;}
. {return yytext[0];}
n {return 0;}
%%
======================output=======================================
yadavakings@ubuntu:~$ vi 5a.l
yadavakings@ubuntu:~$ lex 5a.l
yadavakings@ubuntu:~$ yacc -d 5a.y
yadavakings@ubuntu:~$ cc y.tab.c lex.yy.c
yadavakings@ubuntu:~$ ./a.out
enter expression (1+2)
result 3
enter expression (1-2)
result -1
enter expression (1+2)*(3*9)
result 81
5b.
%{
#include<stdio.h>
int valid=1;
%}
%token A
%token B
%%
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
9. S:A S B
|A B ;
%%
int yyerror()
{
valid=0;
}
int yywrap()
{
return 1;
}
int main()
{
printf("enter a stringn");
yyparse();
if(valid==0)
printf("n invalid string n");
else
printf("valid stringn");
}
%{
#include"y.tab.h"
%}
%%
a {return A;}
b {return B;}
. {return yytext[0];}
n {return 0;}
%%
===========================output=================================
yadavakings@ubuntu:~$ vi 5b.l
yadavakings@ubuntu:~$ lex 5b.l
yadavakings@ubuntu:~$ yacc -d 5b.y
yadavakings@ubuntu:~$ cc y.tab.c lex.yy.c
yadavakings@ubuntu:~$ ./a.out
enter a string
ab
valid string
enter a string
aaabbb
valid string
enter a string
aab invalid string
6a.
%{
#include<stdio.h>
int valid=1,c=0;
%}
%token A
%token B
%%
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
10. S:A S{c++;}
|B ;
%%
int yyerror()
{
valid=0;
}
int yywrap()
{
return 1;
}
int main()
{
printf("enter a stringn");
yyparse();
if(valid==0||c<10)
printf("n invalid string n");
else
printf("valid stringn");
}
%{
#include"y.tab.h"
%}
%%
a {return A;}
b {return B;}
. {return yytext[0];}
n {return 0;}
%%
===============================output================================
yadavakings@ubuntu:~$ vi 6a.l
yadavakings@ubuntu:~$ lex 6a.l
yadavakings@ubuntu:~$ yacc -d 6a.y
yadavakings@ubuntu:~$ cc y.tab.c lex.yy.c
yadavakings@ubuntu:~$ ./a.out
enter a string
aaaaaaaaaaaaaaaaaaaab
valid string
enter a string
aaaaabbbbbbbbbbbbbbbbbbbbbbb
invalid string
Unix
7a.
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
11. x=$#
while [ $x -ne 0 ]
do
eval echo $$x
x=` expr $x - 1 `
done
========================output======================
yadavakings@ubuntu:~$ vi 7a.sh
yadavakings@ubuntu:~$ sh 7a.sh boy good is nagarjun
nagarjun
is
good
boy
yadavakings@ubuntu:~$ sh 7a.sh a b c
c
b
a
7b.
#include<stdio.h>
#include<string.h>
main()
{
char cmd[20];
int status;
if(fork()==0)
{
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
13. cat file2
fi
================================output==============================
yadavakings@ubuntu:~$ vi 8a.sh
yadavakings@ubuntu:~$ sh 8a.sh
common permissions
yadavakings@ubuntu:~$ chmod 222 file2
yadavakings@ubuntu:~$ sh 8a.sh
cmp: file2: Permission denied
different permissions
permissions
total 100
-rwx------rw-------rwx------rw-------rw-r--r--rw-r--r--rw-------rw-r--r--rw-r--r--rw-r--r--rw-------rw-------rwxr-xr-x
drwxr-xr-x
drwxr-xr-x
-rw-r--r--rwxrwxrwx
--w--w--wdrwxr-xr-x
-rw-r--r--rw-r--r--rw-r--r-drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
permissions
cat: file2: Permission denied
8b.
#include<sys/stat.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
int main()
{
char *cmd = "vi 8b.txt";
char *fname = "8b.txt";
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
14. char *s1 = "ABCDEFGHIJKLMNOP";
char *s2 = "abcdefghijklmnop";
int fd;
fd = creat(fname, s.IREAD | s.IWRITE);
if(fd==-1)
{
printf("error in file");
exit(1);
}
if(write(fd,s1,16)!=16)
printf("error");
lseek(fd,48,SEEK_SET);
if(write(fd,s2,16)!=16)
{
printf("error");
}
printf("contents of the file aren");
system(cmd);
}
====================================output===================================
=====
yadavakings@ubuntu:~$ vi 8b.c
yadavakings@ubuntu:~$ cc 8b.c
8b.c: In function ‘main’:
8b.c:12: error: ‘s’ undeclared (first use in this function)
8b.c:12: error: (Each undeclared identifier is reported only once
8b.c:12: error: for each function it appears in.)
sorry no output for this.....
check and text me....!
9a.
for i
do
{
echo "echo $i"
echo "cat>$i<end of $i"
cat $i
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
15. echo "end of file"
}
done
output..........................
yadavakings@ubuntu:~$ vi 9A.SH
yadavakings@ubuntu:~$ sh 9A.SH 1.txt 2.txt>out.sh
yadavakings@ubuntu:~$ cat out.sh
echo 1.txt
cat>1.txt<end of 1.txt
nagarjun is a good boy...
end of file
echo 2.txt
cat>2.txt<end of 2.txt
nagarjun is a bad boy...
end of file
yadavakings@ubuntu:~$ ls 1.txt
1.txt
yadavakings@ubuntu:~$ ls 2.txt
2.txt
yadavakings@ubuntu:~$ rm 1.txt
yadavakings@ubuntu:~$ rm 2.txt
yadavakings@ubuntu:~$ cat out.sh
echo 1.txt
cat>1.txt<end of 1.txt
nagarjun is a good boy...
end of file
echo 2.txt
cat>2.txt<end of 2.txt
nagarjun is a bad boy...
end of file
9b.
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
int main()
{
pid_t ppid,mpid,pid,status=0;
pid=fork();
if(pid<0)
{
printf("error");
exit(0);
}
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
16. if(pid==0)
{
mpid=getpid();
ppid=getppid();
printf("i am a child and my pid is%d n",mpid);
printf("i am a parent and my pid is %d n",ppid);
exit(1);
}
pid=waitpid(pid,&status,0);
mpid=getpid();
printf("i am a parent with pid is %d n ,my child pid is %d n",mpid,pid);
}
====================================output===================================
====
yadavakings@ubuntu:~$ vi 9b.c
yadavakings@ubuntu:~$ cc 9b.c
yadavakings@ubuntu:~$ ./a.out
i am a child and my pid is4756
i am a parent and my pid is 4755
i am a parent with pid is 4755
,my child pid is 4756
OS
10.Round robin
#include<stdio.h>
int main()
{
int et[30],ts,n,i,x=0,tot=0;
char pn[10][10];
printf("enter the number of process:");
scanf("%d",&n);
printf("enter the time quantu:");
scanf("%d",&ts);
for(i=0;i<n;i++)
{
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
17. printf("enter the process name and estimated time:");
scanf("%s %d",pn[i],&et[i]);
}
printf("the process 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);
}
=====================================output==================================
========
yadavakings@ubuntu:~$ vi 10.c
yadavakings@ubuntu:~$ cc 10.c
yadavakings@ubuntu:~$ ./a.out
enter the number of process:5
enter the time quantu:10
enter the process name and estimated time:p1 15
enter the process name and estimated time:p2 25
enter the process name and estimated time:p3 30
enter the process name and estimated time:p4 5
enter the process name and estimated time:p5 20
the process are:process 1:p1
process 2:p2
process 3:p3
process 4:p4
process 5:p5
p1->10
p2->10
p3->10
p4->5
p5->10
p1->5
p2->10
p3->10
p5->10
p2->5
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
18. p3->10
total estimated time
10.sjrf
#include<stdio.h>
struct proc
{
int id, arrival,burst,rem,wait,finish,turnaround;
float ratio;
}
process[10];
struct proc temp;
int no;
int chkprocess(int);
int nextprocess();
void srtf(int);
main()
{
int n;
printf("nn enter the number of the processes");
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
19. scanf("%d",&n);
srtf(n);
}
int chkprocess(int s)
{
int i;
for(i=1;i<=s;i++)
{
if(process[i].rem!=0)
return 1;
}
return 0;
}
int nextprocess()
{
int min,l,i;
min=1000;
for(i=1;i<=10;i++)
{
if(process[i].rem!=0 && process[i].rem<min)
{
min=process[i].rem;
l=i;
}
}
return l;
}
void srtf(int n)
{
int i,j,k,time=0;
float tavg,wavg;
for(i=1;i<=n;i++)
{
process[i].id=i;
printf("nn enter the arrival time for the process %d:",i);
scanf("%d",&(process[i].arrival));
printf("enter the burst time for process %d:",i);
scanf("%d",&(process[i].burst));
process[i].rem=process[i].burst;
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(process[i].arrival>process[j].arrival)
{
temp=process[i];
process[i]=process[j];
process[j]=temp;
}
}
}
no=0;
j=1;
while(chkprocess(n)==1)
{
if(process[no+1].arrival==time)
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
21. enter the arrival time for the process 3:2
enter the burst time for process 3:68
enter the arrival time for the process 4:24
enter the burst time for process 4:24
---SHORTEST REMAINING TIME FIRST-tavg=73.749992
wavg=33.249992
11. fib
#include<stdio.h>
#include<omp.h>
int fib(int n)
{
if(n<2)
return n;
else
return fib(n-1)+fib(n-2);
}
int main()
{
int fibu[100],i,j,n;
printf("please enter the series limt n");
scanf("%d",&n);
omp_set_num_threads(2);
#pragma omp parallel
{
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
22. #pragma omp critical
if(omp_get_thread_num()==1)
{
printf("nthere are %d threadsn",omp_get_num_threads());
printf("n thread %d generating numbers
n",omp_get_thread_num());
for(i=0;i<n;i++)
fibu[i]=fib(i);
}
else
{
printf("nthread %d printing numbers...n",omp_get_thread_num());
for(j=0;j<n;j++)
printf("%dt",fibu[j]);
}
}
return 0;
}
=====================================output==================================
===
yadavakings@ubuntu:~$ cc -fopenmp 11.c
yadavakings@ubuntu:~$ vi 11.c
yadavakings@ubuntu:~$ cc -fopenmp 11.c
yadavakings@ubuntu:~$ ./a.out
please enter the series limt
5
there are 2 threads
thread 1 generating numbers
thread 0 printing numbers...
0
1
1
2
3
12.bankers algo
#include<stdio.h>
#include<omp.h>
int main()
{
int Max[10][10],need[10][10],alloc[10]
[10],avail[10],completed[10],safeSequence[10];
int p,r,i,j,process,count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d",&p);
for(i=0;i<p;i++)
completed[i]=0;
printf("nnEnter the no of resources :");
scanf("%d",&r);
printf("nnEnter the Max Matrix for each process : ");
for(i=0;i<p;i++)
{
printf("nFor process %d : ", i + 1);
for(j=0;j<r;j++)
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
23. scanf("%d", &Max[i][j]);
}
printf("nnEnter the allocation for each process : ");
for(i=0;i<p;i++)
{
printf("nFor process %d : ",i + 1);
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
}
printf("nnEnter the Available Resources : ");
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];
do
{
printf("n Max matrix:tAllocation matrix:n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
printf("%d ", Max[i][j]);
printf("tt");
for(j=0;j<r;j++)
printf("%d ", alloc[i][j]);
printf("n");
}
process = -1;
for(i=0;i<p;i++)
{
if(completed[i]==0)
{
process=i;
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
{
process=-1;
break;
}
}
}
if(process!=-1)
break;
}
if(process!=-1)
{
printf("nProcess %d runs to completion", process + 1);
safeSequence[count]=process+1;
count++;
for(j=0;j<r;j++)
{
avail[j]+=alloc[process][j];
alloc[process][j]=0;
Max[process][j]=0;
completed[process]=1;
}
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
24. }
}
while(count!=p&&process!=-1);
if(count==p)
{
printf("nThe system is in a safe state!!n");
printf("Safe Sequence : < ");
for(i=0;i<p;i++)
printf("%d ", safeSequence[i]);
printf(">n");
}
else
printf("nThe system is in an unsafe state!!");
}
================================output=====================================
yadavakings@ubuntu:~$ vi 12.c
yadavakings@ubuntu:~$ cc 12.c
yadavakings@ubuntu:~$ ./a.out
Enter the no of processes : 5
Enter the no of resources :3
Enter the Max Matrix for each process :
For process 1 : 7
5
3
For process 2 : 3 2
2
For process 3 : 9 0
2
For process 4 : 2 2
2
For process 5 : 4 3
3
Enter the allocation for each process :
For process 1 : 0 1
0
For process 2 : 2 0
0
For process 3 : 3 0
2
For process 4 : 2 1
1
For process 5 : 0 0
2
Enter the Available Resources : 3
Max matrix:
5 3
2 2
0 2
2 2
3 3
7
3
9
2
4
3
2
Allocation matrix:
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Process 2 runs to completion
Max matrix:
Allocation matrix:
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24
25. 7
0
9
2
4
5
0
0
2
3
3
0
2
2
3
0
0
3
2
0
1
0
0
1
0
0
0
2
1
2
Process 4 runs to completion
Max matrix:
Allocation matrix:
7 5 3
0 1 0
0 0 0
0 0 0
9 0 2
3 0 2
0 0 0
0 0 0
4 3 3
0 0 2
Process 1 runs to completion
Max matrix:
Allocation matrix:
0 0 0
0 0 0
0 0 0
0 0 0
9 0 2
3 0 2
0 0 0
0 0 0
4 3 3
0 0 2
Process 3 runs to completion
Max matrix:
Allocation matrix:
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
4 3 3
0 0 2
Process 5 runs to completion
The system is in a safe state!!
Safe Sequence : < 2 4 1 3 5 >
Nagarjun.E,
Atria institute of echnology,Ananadnagar-24
Page 24