Ss Os Manual
Ss Os Manual
CODE: 10CSL58
Prepared By:
DEPARTMENT OF
INFORMATION SCIENCE AND ENGINEERING
PART - A
LEX and YACC Programs:
Design, develop, and execute the following programs using LEX:
3. Program to recognize and count the number of identifiers in a given input file.
PART B
UNIX Programming:
7. a) Non-recursive shell script that accepts any number of arguments and prints
them in the Reverse order, ( For example, if the script is named rargs, then
executing rargs A B C should produce C B A on the standard output).
b) C program that creates a child process to read commands from the standard
input and execute them (a minimal implementation of a shell – like program).
You can assume that no arguments will be passed to the commands to be
executed.
8. a) Shell script that accepts two file names as arguments, checks if the
permissions for these files are identical and if the permissions are identical,
outputs the common permissions, otherwise outputs each file name followed by
its permissions.
b) C program to create a file with 16 bytes of arbitrary data from the beginning
and another 16 bytes of arbitrary data from an offset of 48. Display the file
contents to demonstrate how the hole in file is handled.
9. a) Shell script that accepts file names specified as arguments and creates a shell
script that contains this file as well as the code to recreate these files. Thus if
the script generated by your script is executed, it would recreate the original
files(This is same as the “bundle” script described by Brain W. Kernighan and
Rob Pike in “ The Unix Programming Environment”, Prentice – Hall India).
Operating Systems:
10. Design, develop and execute a program in C / C++ to simulate the working of
Shortest Remaining Time and Round-Robin Scheduling Algorithms.
Experiment with different quantum sizes for the RoundRobin algorithm. In all
cases, determine the average turn-around time. The input can be read from key
board or from a file.
11. Using OpenMP, Design, develop and run a multi-threaded program to generate
and print Fibonacci Series. One thread has to generate the numbers up to the
specified limit and another thread has to print them. Ensure proper
synchronization.
12. Design, develop and run a program to implement the Banker’s Algorithm.
Demonstrate its working with different data values.
Instructions: In the examination, a combination of one LEX and one YACC problem
has to be asked from Part A for a total of 30 marks and one programming exercise
from Part B has to be asked for a total of 20 marks.
PART – A
LEX PROGRAMS:
1) a. Program to count the number of characters, words, spaces and lines in a given
input file.
1a.l
%{
#include<stdio.h>
int lines=0,words=0,chars=0,blanks=0;
%}
%%
[\t] blanks++;
[\n] lines++;
[^ \t\n]+ {words++;chars+=yyleng;}
%%
yywrap()
{
printf("charcter=%d,lines=%d,blanks=%d,words=%d\n",chars,lines,blanks,words);
}
OUTPUT:
%{
int count=0,flag=0;
%}
%%
"/*" {
if(!flag)
{
flag=1;
fprintf(yyout," ");
}
}
[^ "*/"] {
if(flag==1)
fprintf(yyout," ");
else
fprintf(yyout,"%s",yytext);
}
"*/" {
if(flag)
{
flag=0;
fprintf(yyout," ");
count++;
}
}
%%
OUTPUT:
input.c
main()
{
int a,b; /* int declaration */
float amt,avg; /* float declaration */
}
output.c
main()
{
int a,b;
float amt,avg;
}
%{
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int oprt=0,opnd=0,bracket=0;
char ident[20][20],operator[10];
%}
%%
[+] {operator[oprt]='+'; oprt++;}
[-] {operator[oprt]='-'; oprt++;}
[*] {operator[oprt]='*'; oprt++;}
[/] {operator[oprt]='/';oprt++;}
[(] {bracket++;}
[)] {bracket--;}
[a-zA-Z][a-zA-Z0-9]* {strcpy(ident[opnd],yytext); opnd++;}
%%
main()
{
int i=0;
printf("Enter the exppression: ");
yylex();
OUTPUT:
%{
#include<stdio.h>
int simple=1;
%}
word [a-zA-Z]+
space [ ]+
%%
{word}{space}"and"|"but"|"or"{space}{word} {simple=0;}
%%
yywrap()
{
if(!simple)
printf("sentence is compound\n");
else
printf("sentence is simple\n");
}
int main()
{
printf("ENTER A SENTENCE : ");
yylex();
return 0;
}
OUTPUT:
3a.l
%{
int ident=0;
%}
ID [a-zA-Z][a-zA-Z0-9_()]*
DECLN "int"|"double"|"unsigned"|"char"|"long"
%x DEFN
%%
{DECLN} {BEGIN DEFN;}
<DEFN>{ID}, {ident++;}
<DEFN>{ID}; ident++; {BEGIN 0;}
<*>\n ;
<*>. ;
%%
OUTPUT:
input.c
#include<stdio.h>
int i,ij;
double kld,uyt,t,y;
}
PART – A
YACC PROGRAMS:
4a.l
%{
#include "y.tab.h"
%}
%%
[a-zA-Z] {return A;}
[\t] ;
\n return 0;
. return( yytext[0]);
%%
4b.y
%{
#include<stdio.h>
%}
%token A
%left '+''-'
%left '*''/'
%%
str: s
s: s'+'s
| s'-'s
| s'*'s
| s'/'s
| A;
%%
main()
{
printf("\n Type a Simple exp : \n");
if(!yyparse())
printf("\n Valid Simple exp : \n");
return 0;
}
yyerror()
{
printf("\n Invalid simple expression \n");
exit(0);
}
OUTPUT:
%{
#include "y.tab.h"
%}
%%
[a-zA-Z][a-zA-Z0-9]* {return VAR;}
. return yytext[0];
\n return 0;
%%
4b.y
%{
#include<stdio.h>
%}
%token VAR
%%
str:VAR
%%
main()
{
printf("\n Print a Variable\n");
if(!yyparse())
printf("\n Valid variable\n");
}
yyerror()
{
printf("\n Invalid Variable \n");
}
OUTPUT:
[exam1@localhost ~]$ lex 4b.l
[exam1@localhost ~]$ yacc -d 4b.y
[exam1@localhost ~]$ cc lex.yy.c y.tab.c –ll
[exam1@localhost ~]$ ./a.out
Print a Variable
var
Valid variable
[exam1@localhost ~]$ ./a.out
Print a Variable
9var
Invalid Variable
5a.l
%{
#include "y.tab.h"
#include<stdlib.h>
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);
return NUM;}
\n {return 0;}
. {return yytext[0];}
%%
5a.y
%{
#include<stdio.h>
%}
%{
int valid=1;
%}
%token NUM
%left '+''-'
%left '*''/'
%%
str:s {printf("\n Expression result:%d\n",$$);}
s:s'+'s {$$=$1+$3;}
|s'-'s {$$=$1-$3;}
|s'*'s {$$=$1*$3;}
|s'/'s {if($3==0)
{printf("Divide by zero error");
exit(0);}
$$=$1/$3;}
|'('s')' {$$=$2;}
|NUM {$$=$1;}
|'-'s {$$=-$2;}
|'+'s {$$=$2;}
%%
main()
{
printf("Print a valid arithmetic expression\n");
yyparse();
}
yyerror()
{
printf("Invalid arithmetic expression\n");
}
OUTPUT:
Expression result:10
[5is063@localhost ~]$ ./a.out
Print a valid arithmetic expression
9*9
Expression result:81
5. b) Program to recognize strings ‘aaab’, ‘abbb’, ‘ab’ and ‘a’ using the grammar (anbn,
n>= 0).
5b.l
%{
#include "y.tab.h"
%}
%%
a return A;
b return B;
. return yytext[0];
\n return 0;
%%
5b.y
%{
#include<stdio.h>
%}
%token A B
%%
gra:Q
Q:AAM
| MBB
| A
| M;
M:AN
N:TB
T:M
|;
%%
main()
{
printf("\n Enter: \n");
if(!yyparse())
printf("\n Recognised \n");
}
yyerror()
{
printf("\n Not recognised \n");
}
OUTPUT:
Enter:
aabb
Recognised
[exam1@localhost ~]$ ./a.out
Enter:
abbaaba
Not recognised
[exam1@localhost ~]$
6.l
%{
#include"y.tab.h"
%}
%%
a return A;
b return B;
. return yytext[0];
\n return 0;
%%
6.y
%{
#include<stdio.h>
%}
%token A B
%%
start: Q B
Q:A A A A A A A A A D;
D:A D;
|;
%%
main()
{
printf("Enter the grammar\n");
if(!yyparse())
printf("VALID\n");
}
yyerror()
{
printf("Not Recognised\n");
}
OUTPUT:
PART B
UNIX Programming:
7. a) Non-recursive shell script that accepts any number of arguments and prints
them in the Reverse order, ( For example, if the script is named rargs, then
executing rargs A B C should produce C B A on the standard output).
7a.sh
c=$#
if [ $c -eq 0 ]
then
echo -e "Please pass the arguments"
else
while [ $c -ge 1 ]
do
eval echo \$$c
c=`expr $c - 1 `
done
fi
OUTPUT:
7. b) C program that creates a child process to read commands from the standard
input and execute them (a minimal implementation of a shell – like program).
You can assume that no arguments will be passed to the commands to be
executed.
7b.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int main()
{
char str[10];
int pid;
pid=fork();
if(!pid)
{
printf("Child Process\n");
printf("Enter the process\n");
scanf("%s",str);
system(str);
printf("Ending child process\n");
}
else
{
wait(0);
}
}
OUTPUT:
8. a) Shell script that accepts two file names as arguments, checks if the
permissions for these files are identical and if the permissions are identical,
outputs the common permissions, otherwise outputs each file name followed by
its permissions.
8a.sh
c=$#
if [ $c -ne 2 ]
then
echo -e "Error! exactly two filenames should be specified"
else
file1=$1
file2=$2
set -- `ls -l $file1`
f1p=$1
set -- `ls -l $file2`
f2p=$1
if [ "$f1p" = "$f2p" ]
then
echo -e "\n Permission are identical:$f1p"
else
echo -e "\n Permission are different\n $file1=$f1p\n $file2=$f2p"
fi
fi
OUTPUT:
8. b) C program to create a file with 16 bytes of arbitrary data from the beginning
and another 16 bytes of arbitrary data from an offset of 48. Display the file
contents to demonstrate how the hole in file is handled.
8b.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
char buf[]="ABCDEFGHIJKLMNOP";
char buf1[]="QRSTUVWXYZ012345";
int main()
{
int fd;
fd=creat("file1.hole",S_IRUSR|S_IWUSR|S_IWGRP|S_IROTH);
write(fd,buf,16);
lseek(fd,48,SEEK_SET);
write(fd,buf1,16);
exit(0);
}
OUTPUT:
ABCDEFGHIJKLMNOP^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@QRSTUVWXYZ012
345
9. a) Shell script that accepts file names specified as arguments and creates a shell
script that contains this file as well as the code to recreate these files. Thus if
the script generated by your script is executed, it would recreate the original
files(This is same as the “bundle” script described by Brain W. Kernighan and
Rob Pike in “ The Unix Programming Environment”, Prentice – Hall India).
9a.sh
#/bin/sh
if [ $# -eq 0 ]
then
echo "enter the valid input"
exit
fi
for i in $*
do
echo "cat>$i<<end">>bundle.sh
cat $i>>bundle.sh
echo "end">>bundle.sh
done
OUTPUT:
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
printf("In Child.....\n");
printf("child id=%d\n",getpid());
printf("Parent id=%d\n",getppid());
}
else
{
pid=wait(0);
printf("In Parent.....\n");
printf("Child id=%d\n",pid);
printf("Parent id=%d\n",getpid());
}
}
OUTPUT:
Operating Systems:
10. Design, develop and execute a program in C / C++ to simulate the working of
Shortest Remaining Time and Round-Robin Scheduling Algorithms.
Experiment with different quantum sizes for the RoundRobin algorithm. In all
cases, determine the average turn-around time. The input can be read from key
board or from a file.
#include<stdio.h>
#include<stdlib.h>
struct proc
{
int id;
int arrival;
int burst;
int rem;
int wait;
int finish;
int turnaround;
float ratio;
}
main()
{
int n,tq,choice;
int bt[10],st[10],i;
for(; ;)
{
printf("Enter the choice \n");
printf(" 1. Round Robin\n 2. SRT\n 3. Exit \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Round Robin scheduling algorithm\n");
printf("Enter number of processes:\n");
scanf("%d",&n);
printf("Enter burst time for sequences:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i]; //service time
}
printf("Enter time quantum:");
scanf("%d",&tq);
roundrobin(n,tq,st,bt);
break;
case 2:
printf("\n \n ---SHORTEST REMAINING TIME NEXT---\n \n ");
printf("\n \n Enter the number of processes: ");
scanf("%d", &n);
srtf(n);
break;
case 3: exit(0);
} // end of switch
} // end of for
} //end of main()
return l;
} // end of nextprocess
void srtf(int n)
{
int i,j,k,time=0;
float tavg=0,wavg=0;
for(i = 1; i <= n; i++)
{
process[i].id = i;
printf("\n\nEnter the arrival time for 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) // sort arrival time of a process
{
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}
no = 0;
j = 1;
while(chkprocess(n) == 1)
{
if(process[no + 1].arrival == time)
{
no++;
if(process[j].rem==0)
process[j].finish=time;
j = nextprocess();
}
if(process[j].rem != 0) // to calculate the waiting time of a process
{
process[j].rem--;
for(i = 1; i <= no; i++)
{
if(i != j && process[i].rem != 0)
process[i].wait++;
}
}
else
{
process[j].finish = time;
j=nextprocess();
time--;
k=j;
}
time++;
}
process[k].finish = time;
printf("\n\n\t\t\t---SHORTEST REMAINING TIME FIRST---");
printf("\n\n Process Arrival Burst Waiting Finishing turnaround Tr/Tb \n");
printf("%5s %9s %7s %10s %8s %9s\n\n", "id", "time", "time", "time",
"time", "time");
for(i = 1; i <= n; i++)
{
process[i].turnaround = process[i].wait + process[i].burst; // calc of turnaround
process[i].ratio = (float)process[i].turnaround / (float)process[i].burst;
printf("%5d %8d %7d %8d %10d %9d %10.1f ", process[i].id,
process[i].arrival, process[i].burst, process[i].wait, process[i].finish,
process[i].turnaround, process[i].ratio);
tavg=tavg+ process[i].turnaround; //summation of turnaround time
wavg=wavg+process[i].wait; // summation of waiting time
printf("\n\n");
}
tavg=tavg/n; // average turnaround time
wavg=wavg/n; // average wait time
printf("tavg=%f\t wavg=%f\n",tavg,wavg);
} // end of srtf
OUTPUT:
1 0 8 9 17 17 2.1
2 1 4 0 5 4 1.0
3 2 9 15 26 24 2.7
4 3 5 2 10 7 1.4
tavg=13.000000 wavg=6.500000
Enter the choice
1. Round Robin
2. SRT
3. Exit
11. Using OpenMP, Design, develop and run a multi-threaded program to generate
and print Fibonacci Series. One thread has to generate the numbers up to the
specified limit and another thread has to print them. Ensure proper
synchronization.
11.c
#include<stdio.h>
#include<omp.h>
#include<stdlib.h>
int j,id,i=0;
int n,tid,threads;
printf("Enter the no. range\n");
scanf("%d",&n);
omp_set_num_threads(2);
printf("%d\t",Fibnumber[i]);
printf("\n\n");
}
}
}
}
OUTPUT:
12. Design, develop and run a program to implement the Banker’s Algorithm.
Demonstrate its working with different data values.
#include<stdio.h>
struct process
{
int all[6],max[6],need[6],finished,request[6];
}p[10];
int avail[6],sseq[10],ss=0,check1=0,check2=0,n,pid,work[6];
int nor,nori;
void main()
{
int safeseq(void);
int ch,i=0,j=0,k,pid,ch1;
int violationcheck=0,waitcheck=0;
do
{
printf("\n\n\t 1. Input");
printf("\n\n\t 2. New Request");
printf("\n\n\t 3. Safe State or Not");
printf("\n\n\t 4. print");
printf("\n\n\t 5. Exit");
printf("\n\n\t Enter ur choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\n\t Enter number of processes : ");
scanf("%d",&n);
printf("\n\n\t Enter the Number of Resources : ");
scanf("%d",&nor);
printf("\n\n\t Enter the Available Resouces : ");
for(j=0;j<n;j++)
{
for(k=0;k<nor;k++)
{
if(j==0)
{
printf("\n\n\t For Resource type %d : ",k);
scanf("%d",&avail[k]);
}
p[j].max[k]=0;
p[j].all[k]=0;
p[j].need[k]=0;
p[j].finished=0;
p[j].request[k]=0;
}
}
for(i=0;i<n;i++)
{
printf("\n\n\t Enter Max and Allocated resources for P : ",i);
for(j=0;j<nor;j++)
{
printf("\n\n\t Enter the Max of resource %d : ",j);
scanf("%d",&p[i].max[j]);
printf("\n\n\t Allocation of resource %d :",j);
scanf("%d",&p[i].all[j]);
if(p[i].all[j]>p[i].max[j])
{
printf("\n\n\t Allocation should be less < or == max");
j--;
}
else
p[i].need[j]=p[i].max[j]-p[i].all[j];
avail[j]=avail[j]-p[i].all[j];
}
}
break;
case 2:
violationcheck=0;
waitcheck=0;
printf("\n\n\t Requesting process id :");
scanf("%d",&pid);
for(j=0;j<nor;j++)
{
printf("\n\n\t Number of Request for resource %d :",j);
scanf("%d",&p[pid].request[j]);
if(p[pid].request[j]>p[pid].need[j])
violationcheck=1;
if(p[pid].request[j]>avail[j])
waitcheck=1;
}
if (violationcheck==1)
printf("\n\n\t The Process Exceeds it's Max Need: Terminated");
else if(waitcheck==1)
printf("\n\n\t Lack of Resourcess : Process State - Wait");
else
{
for(j=0;j<nor;j++)
{
avail[j]=avail[j]-p[pid].request[j];
p[pid].all[j]=p[pid].all[j]+p[pid].request[j];
p[pid].need[j]=p[pid].need[j]-p[pid].request[j];
}
ch1=safeseq();
if(ch1==0)
{
for(j=0;j<nor;j++)
{
avail[j]=avail[j]+p[pid].request[j];
p[pid].all[j]=p[pid].all[j]-p[pid].request[j];
p[pid].need[j]=p[pid].need[j]+p[pid].request[j];
}
}
else if(ch1==1)
printf("\n\n\t Request Committed ");
}
break;
case 3:
if(safeseq()==1)
printf("\n\n\t The System is in safe state ");
else
printf("\n\n\t The System is Not in safe state ");
break;
case 4:
printf("\n\n\t Number of processes : %d",n);
printf("\n\n\t Number of Resources : %d",nor);
printf("\n\n\t Pid \t Max \t Allocated \t Need ");
for(i=0;i<n;i++)
{
printf("\n\n\t P%d : ",i);
for(j=0;j<nor;j++)
printf(" %d",p[i].max[j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%d",p[i].all[j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%d",p[i].need[j]);
}
printf("\n\n\t Available :");
for(i=0;i<nor;i++)
printf("%d",avail[i]);
break;
case 5:
break;
}
}while(ch!=5);
}
int safeseq()
{
int tj,tk,i,j,k;
ss=0;
for(j=0;j<nor;j++)
work[j]=avail[j];
for(j=0;j<n;j++)
p[j].finished=0;
for( tk=0;tk<nor;tk++)
{
for(j=0;j<n;j++)
{
if(p[j].finished==0)
{
check1=0;
for(k=0;k<nor;k++)
if(p[j].need[k]<=work[k])
check1++;
if(check1==nor)
{
for(k=0;k<nor;k++)
{
work[k]=work[k]+p[j].all[k];
p[j].finished=1;
}
sseq[ss]=j;
ss++;
}
}
}
}
check2=0;
for(i=0;i<n;i++)
if(p[i].finished==1)
check2++;
printf("\n\n\t");
if(check2>=n)
{
printf("\n\n\t The system is in safe state");
for( tj=0;tj<n;tj++)
printf("P%d,",sseq[tj]);
return 1;
}
else
1. Input
2. New Request
4. print
5. Exit
Enter ur choice : 1
Allocation of resource 0 :2
Allocation of resource 1 :2
Allocation of resource 2 :2
Allocation of resource 0 :1
Allocation of resource 1 :1
Allocation of resource 2 :1
Allocation of resource 0 :1
Allocation of resource 1 :1
Allocation of resource 2 :1
1. Input
2. New Request
4. print
5. Exit
Enter ur choice :3
1. Input
2. New Request
4. print
5. Exit
Enter ur choice :4
Number of processes : 3
Number of Resources : 3
P0 : 3 3 3 222 111
P1 : 2 2 2 111 111
P2 : 2 2 2 111 111
Available :666
1. Input
2. New Request
4. print
5. Exit
Enter ur choice :2
Requesting process id :2
Request Committed
1. Input
2. New Request
4. print
5. Exit
Enter ur choice :4
Number of processes : 3
Number of Resources : 3
P0 : 3 3 3 222 111
P1 : 2 2 2 111 111
P2 : 2 2 2 211 011
Available :566
1. Input
2. New Request
4. print
5. Exit
Enter ur choice :