Академический Документы
Профессиональный Документы
Культура Документы
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:
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:
Output:
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:
Output:
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:
Output:
./prob4.sh soumya
Searching all possible directories....
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:
Output:
Solution:
Output:
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:
Solution:
Output:
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`
while [ $i -le $# ]
do
a=`echo $* | cut -d " " -f $i`
work_done $a
i=`expr $i + 1`
done
Output:
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.
C- program used:
#include <stdio.h>
int main(){
int t1,t2;
t1=time();
t2=time();
while(t2-t1<45000000){
t2=time();
return 0;
}
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.
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:
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>
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';
}
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++;
number_partitions=size;
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;
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
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
Solution:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NO_OF_FRAMES 4
int no_of_requests;
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));
no_of_requests=n;
return a;
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);
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
LRU
FIFO