Вы находитесь на странице: 1из 21

ASSIGNMENT I

Problem 1:-

Write a shell script that will assign the value Hi! to a variable called all. Then print
the value of all with no quotes, with single quotes and with double quotes.

Solution:

all=Hi!
echo -e "With no quotes :$all\n"
echo -e "With single quotes :'$all'\n"
echo -e "with double quotes :\"$all\"\n"
Output:

[root@localhost ~]# ./prob1.sh


With no quotes :Hi!

With single quotes :'Hi!'

with double quotes :"Hi!"

Problem 2 :-
Write a shell script that has two user created variables, var1 and var2.Ask for
values of the variable from the user and take in any values of the two variables and
then print them as:-
i)values of var1 followed by values of var2 separated by a comma
ii) values of var2 followed by values of var1 separated by the word “and”.

Solution:

echo -e "Enter variable 1 :\c"


read var1
if [ -z var1 ] ; then
echo -e "You did not entered variable. Exit\n" ; exit 1
fi
echo -e "Enter variable 2 :\c"
read var2
if [ -z var2 ] ; then
echo -e "You did not entered variable. Exit\n" ; exit 2
fi
echo -e "i) $var1 , $var2 "
echo -e "ii)$var2 and $var1 "

Output:

[root@localhost ~]# ./prob2.sh


Enter variable 1 :var1
Enter variable 2 :var2
i) var1 , var2
ii)var2 and var1

Problem 3:-
Write a shell script that reads command line arguments and prints the number of
arguments. Given this sequence of words as command line arguments “To the OS
Laboratory at Alpha Lab Welcome”, the script should echo back the information that
there are 8 arguments. Additionally it should echo back some of the arguments in the
following order:
“Welcome To the alpha Lab Os Laboratory”.

Solution:

echo -e "Number of arguments :$#"

echo -e "$8 $1 $2 $6 $7 $3 $4"

Output:

[root@localhost ~]# ./prob3.sh To the OS Laboratory at Alpha Lab Welcome


Welcome To the alpha Lab Os Laboratory

Problem 4:
Write a shell script that accepts a filename as an argument and performs the following
tasks for that file:
a. searches all possible directories and prints the pathname if the file exists otherwise
prints an error message and exits.
b. prints the access rights of the file.
c. prints the size of the file.
d. prints the date of creation and the last date of modification of the file.

Solution:

if [ $# -eq 0 -o $# -gt 1 ] ; then


echo -e "This program takes only one argument" ; exit 1
fi
if [ -z "$1" ] ; then
echo -e "Filename Null. Exit\n" ; exit 2
fi
echo -e "Searching all possible directories....\n"
find . -name "$1" 2>/dev/null >temp
if [ `cat temp | wc -l ` -eq 0 ] ; then
echo -e "No such file exists.\n" ; exit 3
fi
echo -e " Path Access Rights Size Modification Date\n"
for var in `cat temp` ; do
aright=`ls -l $var | tr -s ' ' |cut -d" " -f1`
size=`ls -l $var | tr -s ' ' |cut -d" " -f5`
mdate=`ls -l $var | tr -s ' ' |cut -d" " -f6,7`
printf "%19s %13s %6d %16s\n" $var $aright $size "$mdate"
done

Output:

./prob4.sh soumya
Searching all possible directories....

Path Access Rights Size Modification Date

./soumya -rw-r--r-- 45 Feb 7

Problem 5:- Write a shell script that accepts a file name as input .The program then
asks for a string of characters(i.e. any word ) from the user .The file will be
searched to find whether it contains the given word .If it does, the program will
display the no. of occurrences of the word. The program should also display the line
numbers where the word occurred .If the file does not contain the word, and then an
appropriate error message will be displayed.

Solution:

echo Enter a File Name


read fname
if [ -f $fname ]
then
echo Enter a Pattern to be Recognized
read pattern
if ! grep $pattern -n $fname
then echo Pattern NOT Exists
fi
else
echo File Does NOT Exist
fi

Output:

[root@localhost ~]# cat soumya


chloroform
aerobic
olfactory
caesious
Euonia
[root@localhost ~]# ./prob5.sh
Enter a File Name
soumya
Enter a Pattern to be Recognized
chloroform
1:chloroform
Problem 6:-
Extend the shell script written in problem 5 to perform the following task: user is
asked to enter 2 different patterns or words. The first pattern will be matched with
the contents of the file and replaced with the second pattern if a match occurs. If
the first pattern does not occur in the file, an appropriate error message will be
displayed.

Solution:

echo Enter a File Name


read fname
if [ -f $fname ]
then
echo Enter a Pattern to be Replaced
read pattern1
echo Enter another Pattern to be Inserted
read pattern2
if grep $pattern1 -c $fname
then
sed s/$pattern1/$pattern2/g $fname > aaa
rm $fname
mv aaa $fname
else
echo 1st Pattern NOT Exists
fi
else
echo File Does NOT Exist
fi

Output:

[root@localhost ~]# cat soumya


chloroform
aerobic
olfactory
caesious
Euonia
[root@localhost ~]# ./prob6.sh
Enter a File Name
soumya
Enter a Pattern to be Replaced
chloroform
Enter another Pattern to be Inserted
mercury
1
[root@localhost ~]# cat soumya
mercury
aerobic
olfactory
caesious
Euonia

Problem 7:
Write a shell script factor_table that will generate a table of factors for an integer
number given on the command line.

Solution:

if [ $# -eq 0 ] ; then
echo -e "No arguments." ; exit 1
fi
for no in "$@" ; do
i=2
flag=prime
num=`expr $no - 1`
result=" "
error=0
while [ $i -le $num ] ; do
rem=`expr $no % $i`
if [ $rem -eq 0 ] ;then
flag=comp
result=$result" "$i #appedning result
fi
i=`expr $i + 1`
done
if [ $error -eq 1 ] ; then
echo -e "$no is not an integer\n"
elif [ "$flag" != "comp" ] ; then
echo -e "$no is a prime number\n"
else
echo -e "Factors of $no :$result\n"
fi
done

Output:

[root@localhost ~]# ./prob7.sh 105


Factors of 105 : 3 5 7 15 21 35
Problem 8:- Write a shell script that takes as command line argument the name of a
C program and removes the comments from it.

Solution:

if [ $# -eq 0 -o $# -gt 1 ] ; then


echo -e "Improper number of arguments.\n" ;exit 1
fi
sed 's|\(^[^"]*\)\(/\*.*\*/\)\([^"]*$\)|\1\3|g' $1 > temp #include<stdio.h> /*r*/
sed 's|\(".*\)\(/\*.*\*/\)\([^"]*$\)|\1\3|g' temp >temp1 #/*r*/ printf("tt");
sed 's|\(^[^"]*\)\(/\*.*\*/\)\(.*"\)|\1\3|g' temp1 > temp # printf("tt"); /*r*/
sed 's|\(.*; *\)\(/\*.*\*/\)\(.*\)|\1\3|g' temp > temp1 #printf(";/*rr"); /*r*/
printf("rr");
sed 's|\(^[^"]*\)\(//\)\([^"]*$\)|\1|g' temp1 > temp #//comment
sed 's|\(".*\)\(//\)\([^"]*$\)|\1|g' temp1 > temp #//comment print("");
sed 's|\(^[^"]*\)\(//\)\(.*"\)|\1|g' temp > temp1 #printf("tt"); //cc
sed 's|\(.*; *\)\(//.*\)|\1|g' temp1 > temp #printf(";/*rr");
//comment out printf("rr");
mv temp $1
rm -f temp temp1
cat $1

Output:

[root@localhost ~]# cat test.c


int main()
{
int first,second,third; // declaration
third=first+second ;//adding
}
[root@localhost ~]# ./prob8.sh test.c
int main()
{
int first,second,third;
third=first+second ;
}

Problem 9:- Write a shell script that takes multiple filenames as command line
arguments and prints the frequency of occurrence of vowels in each file. The output
should clearly denote the frequency of occurrence of each vowel in each file.

Solution:

count_v(){
no=`grep -c $2 $1`

y=0
while [ $no -ne 0 ]
do
y=`expr $y + $no`
sed 's/'$2'/""/' $1 > doc2.txt
cp -f doc2.txt $1
no=`grep -c $2 $1`
done
echo $y
rm -f doc2.txt
rm -f doc1.txt
}
work_done(){
tr [aA] 'a' < $1 | tr [eE] 'e' | tr [iI] 'i' | tr [oO] 'o' | tr [uU] 'u' > doc3.txt
echo $1
no=`count_v doc3.txt a`

echo " freq of occurence of a: $no"


no=`count_v doc3.txt e`
echo " freq of occurence of e: $no"
no=`count_v doc3.txt i`
echo " freq of occurence of i: $no"
no=`count_v doc3.txt o`
echo " freq of occurence of o: $no"
no=`count_v doc3.txt u`
echo " freq of occurence of u: $no"
rm doc3.txt
}
if [ $# -eq 0 ] ; then
echo "Error:No files mentioned!"
fi
i=1

while [ $i -le $# ]
do
a=`echo $* | cut -d " " -f $i`
work_done $a
i=`expr $i + 1`
done

Output:

[root@localhost ~]# cat soumya


chloroform
aerobic
olfactory
caesious
Euonia
[root@localhost ~]# ./prob9.sh soumya
soumya
freq of occurence of a: 4
freq of occurence of e: 3
freq of occurence of i: 3
freq of occurence of o: 8
freq of occurence of u: 2
ASSIGNMENT – II
Problem 1:-

a) Find a linux command that will be able to show you the details of the currently
running processes with real time update of CPU usage in the system. Keep the command
executing in one window.
b) Write a program in C that runs for 45 seconds, uses the CPU but does not perform
any i/o. Compile and convert it into a process. Execute this in another window and see
the output of window (a). Now stop this using ctrl- z (before it automatically stops)
and see how the change is reflected in the other window.
c) Find another Linux command that will bring it back to running state again.

Linux command used to see the memory details : top

C- program used:
#include <stdio.h>
int main(){

int t1,t2;
t1=time();
t2=time();
while(t2-t1<45000000){

t2=time();

}//45 sec delay

return 0;
}

Command used to bring the program to ready state : fg

Problem 2:-

Develop a small linux shell called BCSE. The shell will display a prompt ,accept
user commands and execute them.
The overview and functions of BCSE are as follows:-

• At start the shell will display the prompt string hi- BCSE!!
• After displaying the prompt. it is ready to accept and execute a command.
• A command line has the syntax command[arguments]
• When a valid command is entered ,the shell will execute it.During excution ,the
shell may accept other commands, ie, commands may run in the foreground and
background.
• The shell should display error message when command is invalid, or when there
are problems with arguments or execution of the command.
• Once the command is completed(or error message displayed) , trhe shell will
redisplay the prompt and the whole cycle repeats.

The commands to be executed by the BCSE are as follows:-

i) dirnew directory-name
action : creates a directory
ii) fedit [filename] ([] denotes argument is optional)
action: the file will be opened with vi editor, and if no argument is given,
a new file will be opened by the editor.
iii) fcontent filename
action: prints the contents of a file on the screen
iv) info [filename]
action: displays essential information about the file ,comprising of
the full pathname of the file
the size of the file
last modification adte
name of creator
v) exitbcse action : this will quit the shell

Solution:

#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<wait.h>
#define BUFSIZE 200
#define ARGVSIZE 40
#define DELIM "\n\t\r "
int main()
{
char buf[BUFSIZE];
char *arg[ARGVSIZE];
char cmdline[BUFSIZE];
char cmd[10];
int i=0,argcount=0,returnval,bg=0,err;
pid_t pid;
for(;;)
{
for(i = 0; i < ARGVSIZE; i++)
arg[i] = "\0";
for(i = 0; i <= BUFSIZE; i++)
buf[i] = '\0';
for(i = 0; i <= BUFSIZE; i++)
cmdline[i] = '\0';
for(i=0;i<10;i++)
cmd[i]='\0';
bg=0;
printf("Hi-BCSE >");
fgets(buf,BUFSIZE,stdin);
arg[0]=strtok(buf,DELIM);
for(i=1;arg[i-1]!=NULL;i++)
{ arg[i]=strtok(NULL,DELIM);

}
argcount=i-2;
if(argcount>=0)
{
if(strcmp(arg[argcount],"&")==0) {bg=1;
arg[argcount]=NULL;argcount--;}
}
if(arg[0]==NULL)
continue;
if(strcmp(arg[0],"exitbcse")==0)
{
if(argcount==0) exit(0);
else
{ printf("exitbcse:No arguments required\n");continue; }
}
else if(strcmp(arg[0],"dirnew")==0)
{
strcpy(cmd,"mkdir");
strcpy(cmdline,"mkdir");
if(argcount==0)
{
printf("dirnew :Error: Arguments required\n");
continue;
}
}
else if(strcmp(arg[0],"fedit")==0)
{
strcpy(cmd,"vi");
strcpy(cmdline,"vi");
}
else if(strcmp(arg[0],"fcontent")==0)
{
strcpy(cmd,"cat");
strcpy(cmdline,"cat");
if(argcount==0)
{
printf("fcontent :Error: Arguments required\n");
continue;
}
}
else if(strcmp(arg[0],"info")==0)
{
strcpy(cmd,"ls");
for(i=argcount;i>=1;i--)
arg[i+1]=arg[i];
arg[1]="-l";
argcount++;
arg[argcount+1]=NULL;
strcpy(cmdline,"ls -l");
}
else
{
printf("BCSE:Invalid Command\n");
continue;
}
pid=fork();
if(pid==0)
{
arg[0]=cmd;
err=execvp(arg[0],arg);
if(err==-1) {err=0;_exit(0);}
}
else if(pid>0)
{
if(bg==0)
{ wait(NULL);}

}
}
return(0);
}

Output:

[root@localhost ~]# gcc -o bcse hibcse.c


[root@localhost ~]# ./bcse
Hi-BCSE >
Hi-BCSE >fcontent soumya
chloroform
aerobic
olfactory
caesious
Euonia
Hi-BCSE >fedit soumya
Hi-BCSE >info soumya
-rw-r--r-- 1 root root 52 Apr 14 12:44 soumya
Hi-BCSE >dirnew soumyava
Hi-BCSE >dirnew
dirnew :Error: Arguments required
Hi-BCSE >exitbcse
[root@localhost ~]#

ASSIGNMENT III

Problem 1:- Consider the following scenario and develop a program to simulate it.
Assume that each memory location is capable of holding only 1 byte.

Five programs are currently executing and following information pertaining to each
program is provided.
Name Size (bytes) Starting memory address
Program 1 300 450
Program 2 155 070
Program 3 66 002
Program 4 120 290
Program 5 400 950

(i) You have to accommodate a new program (60 bytes) in the system. Use (a) first fit
policy and (b) best fit policy. Show the original and modified memory map in each case.

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

#define MemSize 2048


#define MaxProcess 100

int ProcessListIndex=0;
char Memory[MemSize];

int ProcessList[MaxProcess][3];

int main(){

int nProcesses;
int i,j;
int pid;
int size;
int start_loc;
int number_partitions;
int choice;

int blockSize;
int bestBlockSize;int flag=0;
int firstBlockSize;
int BestFitBlock;

for(j=0;j<MemSize;j++){
Memory[j]='0';
}

printf("Enter the initial number of processes");


scanf("%d",&nProcesses);

for(i=0;i<nProcesses;i++){
printf("\n\n");
printf("\n Enter process id: ");
scanf("%d",&pid);
printf("\n Enter program size: ");
scanf("%d",&size);
printf("\n Enter starting location: ");
scanf("%d",&start_loc);
ProcessList[ProcessListIndex][0]=pid;
ProcessList[ProcessListIndex][1]=size;
ProcessList[ProcessListIndex][2]=start_loc;
ProcessListIndex++;
number_partitions=size;

for(j=0;j<number_partitions;j++){
Memory[start_loc+j]='1';
}

}
/***************ORIGINAL MEMORY MAP*****************************************/
printf("\n original memory map :\n ");

for(j=0,blockSize=0;j<MemSize;j++){
if(Memory[j]=='0'){
if(blockSize!=0){
printf("End:%d\n ",j-1);
}
blockSize=0;
}

else if(Memory[j]=='1'){
if(blockSize==0){
printf("\n Start:%d -",j);
}

blockSize++;

printf("\n Enter New process id: ");


scanf("%d",&pid);
printf("\n Enter program size: ");
scanf("%d",&size);

number_partitions=size;

printf("Enter your choice : \n 1.First Fit \n 2.Best Fit\n choice :");


scanf("%d",&choice);

switch(choice){
case 1: //FIRST FIT
flag=0;
for(i=0,blockSize=0;i<MemSize && flag==0;i++){

if(Memory[i]=='1')
blockSize=0;

if(Memory[i]=='0'){
blockSize++;
if(number_partitions==blockSize){
flag=1;
printf("Starting location : %d",(i-blockSize+1) );

}
}

}//end for

break;

case 2://BEST FIT

flag=0;
BestFitBlock=2048;
for(i=0,blockSize=0;i<MemSize && flag==0;i++){

if(Memory[i]=='1')
blockSize=0;

if(Memory[i]=='0' ){
blockSize++;
if(Memory[i+1]=='1' && (blockSize<BestFitBlock) &&
(blockSize>=number_partitions)){
BestFitBlock=blockSize;
start_loc=(i-blockSize+1);

}//end for

printf("Starting location : %d",start_loc );


break;

return 0;

}
Problem2:-
Write a program to simulate
i. Least Recently Used (LRU) page replacement algorithm
ii. First In First Out (FIFO) page replacement algorithm

for the following memory reference string:


1 2 3 4 1 2 5 7 1 3 7 5 2 3 4
Assume there are 4 frames in main memory.

Solution:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define NO_OF_FRAMES 4

int no_of_requests;

void lru(int *,int );

void fifo(int *,int );

int * get_page_requests();

int main()
{
int *page_request;

page_request=get_page_requests();
lru(page_request,no_of_requests);
fifo(page_request,no_of_requests);

return 1;

int * get_page_requests()
{ int *a,n,i;
printf("\n\n\tENTER NO. OF PAGE REQUESTS:");

scanf("%d",&n);

a=(int *)malloc(n*sizeof(int));

printf("\n\n\tenter the page requests one by one:");


for(i=0;i<n;i++)
{printf("\n\n\t\trequest no. %d:",i+1);
scanf("%d",&a[i]);
}

no_of_requests=n;

return a;

void fifo(int *a,int n)


{ int count,hit,miss;
int *frame;
int i,j;

frame=(int*)malloc(NO_OF_FRAMES*sizeof(int));

hit=0;
count=0;
miss=1;
for(i=0;i<NO_OF_FRAMES;i++)
frame[i]=-1;

printf("\n\n\tFIFO\n\n");
for(i=0;i<n;i++)

{
printf("\n\ncurrent frame:");
for(j=0;j<NO_OF_FRAMES;j++)
if(frame[j]!=-1)
printf("%d ",frame[j]);

printf("\tpage requested:%d",a[i]);

for(j=0;j<NO_OF_FRAMES;j++)
if(frame[j]==a[i])
{miss=0;break;}

if(!miss)
{printf("\t hit! ");hit++;}
else
{
if(count<=3)
{frame[count]=a[i];

count++;
printf("\tmiss!");
}
else
{printf("\tmiss!");
for(j=0;j<NO_OF_FRAMES-1;j++)
frame[j]=frame[j+1];

frame[NO_OF_FRAMES-1]=a[i];

}
miss=1;

}
printf("\n\n\t %d hits for %d requests",hit,n);

void lru(int *a,int n)


{ int count,hit,miss;
int *frame;
int i,j,use_index;

frame=(int*)malloc(NO_OF_FRAMES*sizeof(int));

hit=0;
count=0;
miss=1;
for(i=0;i<NO_OF_FRAMES;i++)
frame[i]=-1;
printf("\n\n\tLRU\n\n");

for(i=0;i<n;i++)

{
printf("\n\ncurrent frame:");
for(j=0;j<NO_OF_FRAMES;j++)
if(frame[j]!=-1)
printf("%d ",frame[j]);

printf("\tpage requested:%d",a[i]);

for(j=0;j<NO_OF_FRAMES;j++)
if(frame[j]==a[i])
{miss=0;use_index=j;break;}

if(!miss)
{printf("\t hit! ");
hit++;
for(j=use_index;j>0;j--)
frame[j]=frame[j-1];

frame[0]=a[i];
}
else
{
if(count<3)
{
for(j=count;j>0;j--) frame[j]=frame[j-1];
frame[0]=a[i];
count++;
}
else
{ if(frame[count]==-1) printf("\tmiss!");
else printf("\tmiss!");
for(j=count;j>0;j--) frame[j]=frame[j-1];
frame[0]=a[i];
}
}

miss=1;
}
printf("\n\n\t %d hits for %d requests",hit,n);

Output:
[root@localhost ~]# gcc -o fifolru fifolru.c
[root@localhost ~]# ./fifolru
ENTER NO. OF PAGE REQUESTS:15

enter the page requests one by one:

request no. 1:1

request no. 2:2

request no. 3:3


request no. 4:4

request no. 5:1

request no. 6:2

request no. 7:5

request no. 8:7

request no. 9:1

request no. 10:3

request no. 11:7

request no. 12:5

request no. 13:2

request no. 14:3

request no. 15:4

LRU

current frame: page requested:1 miss!

current frame:1 page requested:2 miss!

current frame:2 1 page requested:3 miss!

current frame:3 2 1 page requested:4 miss!

current frame:4 3 2 1 page requested:1 hit!

current frame:1 4 3 2 page requested:2 hit!

current frame:2 1 4 3 page requested:5 miss!


current frame:5 2 1 4 page requested:7 miss!

current frame:7 5 2 1 page requested:1 hit!

current frame:1 7 5 2 page requested:3 miss!

current frame:3 1 7 5 page requested:7 hit!

current frame:7 3 1 5 page requested:5 hit!

current frame:5 7 3 1 page requested:2 miss!

current frame:2 5 7 3 page requested:3 hit!

current frame:3 2 5 7 page requested:4 miss!

6 hits for 15 requests

FIFO

current frame: page requested:1 miss!

current frame:1 page requested:2 miss!

current frame:1 2 page requested:3 miss!

current frame:1 2 3 page requested:4 miss!

current frame:1 2 3 4 page requested:1 hit!

current frame:1 2 3 4 page requested:2 hit!

current frame:1 2 3 4 page requested:5 miss!

current frame:2 3 4 5 page requested:7 miss!

current frame:3 4 5 7 page requested:1 miss!

current frame:4 5 7 1 page requested:3 miss!

current frame:5 7 1 3 page requested:7 hit!

current frame:5 7 1 3 page requested:5 hit!

current frame:5 7 1 3 page requested:2 miss!

current frame:7 1 3 2 page requested:3 hit!

current frame:7 1 3 2 page requested:4 miss!

5 hits for 15 requests

Вам также может понравиться