Session 8 : Operating System Programming Concepts : Lab 8 - Pipe & Shell Programming
Lab 8 : Pipe & Shell Programming
Q?What is pipe
/>pipe is used to connect a data flow from one process to another
/>generally, we attach the output of one process to the input of
another.
cmd1 | cmd2 : the standard
output from cmd1 is fed to cmd2 as its standard input
Q?What is a pipe call
/>it provides the means of passing data between two programs
int pipe(int file_descriptor[2]);
/>parameters are passed as an array of two integer file descriptors.
/>on failure it returns -1 → errno
Example:pipe across a fork
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int
main(){
int
data_processed;
int
file_pipes[2];
// file
descriptors
const
char
some_data[] = "a
b c";
char
buffer[BUFSIZ
+ 1];
//
bufsiz
is chosen on each system to make stream i/o efficient
pid_t
fork_result;
memset(buffer,"\0",sizeof(buffer));
if(pipe(file_pipes)==0){
fork_result
= fork();
if(fork_result
== -1){
fprintf(stderr,"Fork
Failure");
exit(EXIT_FAILURE);
}
if(fork_result==0){
//read
from the pipe
data_processed
= read(file_pipes[0],buffer,BUFSIZ);
//
read blocks until data arrives
printf("Read
%d bytes: %s \n",data_processed,buffer);
exit(EXIT_SUCCESS);
}else{
//write
to a pipe
data_processed
= write(file_pipes[1],some_data,strlen(some_data));
printf("Wrote
%d bytes \n",data_processed);
}
}
exit(EXIT_SUCCESS);
}
Q?
What is a shell
/> it
is a program that acts as interface between you and UNIX system
/>
bash
(Bourne-again shell)
is the most popular Unix shell used.
Q?
How do you create a script
/>
we
use the text editor (ex: gedit for linux ) to create the file
containing the commands
#!/bin/bash
echo
$0
echo
$#
exit
0
Note
: * Comments starts with #
and continue to the end of a line
/>
first line (#!/bin/bash) is a special comment
/>
#! chars tell the sytem that /bin/bash is the program to be used to
execute this file
Q?
How do we make the script executable
/> We
have to add the permission to execute for the user to make the script
executable
/>
chmod +x myscript
/>
./myscript
Q?
How do we use the variables in shell
/>
we do not declare variables in the shell before we use them : we
create them when we first
use
them
/>
by default all variables are considered and stored as strings
/>
even if we assign numerical values to the variables they are
considered as strings cos shell will convert them in order to operate
on them as required.
Example
:
salutation=“Hello” # initialization / creation
echo $salutation # usage
myvar=7+13
echo $myvar
Note *: the variables behaves as per the quotes used
Example:
#!/bin/bash
myvar="hi
there"
#
always be careful there should be no space while creating variable
echo
"hello
world"
echo
'$myvar'
#
no subsitution will take place
echo
\$myvar #
remove the special meaning of $ character
echo
"$myvar"
#
it is replaced with its value
echo
$myvar
Output :
hello world
$myvar
$myvar
hi there
hi there
Example:
Environment Variables
#!/bin/bash
#
when a
shell
scripts
starts, some
variables
are
initialized
from
values in
#
the
environment
and
they are called Enviroment Variables
echo
$HOME #
home
directory
of
the current
user
echo
$PATH #
colon
separate
list
of
directories
to search for
commands
echo
$PS1 #
primary
and
secondary
commands
prompts
echo
$PS2 #
primary and
secondary
commands
prompts
echo
$IFS #
input
field
separator
echo
$0 #
the
name
of
shell
scripts
echo
$# # the
number
of
parameters
passed
echo
$$ # the
process
ID of
the shell
scripts
Result:
/home/niranjank
/home/niranjank/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
./test.sh
0
5288
Example
: Parameters Variables
#!/bin/bash
#
if
the script
is invoked
with
parameters,
some
additional
variables are
#
created
message="List
of Parameters"
echo
$message
echo
$@ #
list
of
parameters
echo
"first
parameter is"
echo
$1
# the
first
parameters
given
to
the
script
echo
"script
name is"
echo
$0
exit
0
Result
:
niranjank@niranjank-Compaq-510
~/Desktop/os/lab/lab8
$
./test.sh 1 2 3 4 5
List of
Parameters
1 2 3 4
5
first
parameter is
1
script
name is
./test.sh
Example:
Conditions
#!/bin/bash
#
conditions if
else
else
if
bash syntax
example
if
[ "$a"
-eq 0 ];
then
apples="3"
elif
[ "$a"
gt
0 ]; then #
else
if
condition
apples="5" #
statements
else
echo
"Unknown
parameters"
fi
# end if
Example:
Conditions
#!/bin/bash
#
conditions if
else
else
if
bash syntax
example
#
string comparision
if
[ string1 = string2 ];
then
echo
"Strings
are equal"
elif
[ string1 != string2 ]; then
# else
if
condition
echo
"Strings
are not equal" #
statements
elif
[
-n
string
]; then
echo
"string
is not null"
elif
[ -z string
]; then
echo
"string
is null(empty string)"
#
arithmetic comparision
elif
[
exp1 -eq
exp2
]; then
echo
"exps
are equal"
elif
[ exp1 -ne exp2
]; then
echo
"exps
are not equal"
elif
[ exp1 -gt exp2
]; then
echo
"exp1
is greater than exp2"
elif
[ exp1 -ge exp2
]; then
echo
"exp1
is greater than or equal to exp2"
elif
[ exp1 -lt exp2
]; then
echo
"exp1
is less than exp2"
elif
[ exp1 -le exp2
]; then
echo
"exp1
is less than or equal exp2"
elif
[ ! exp]; then
echo
"exp
is false "
else
echo
"Unknown
parameters"
fi
# end if
Q?
For Loop basic syntax
#!/bin/bash
for
VARIABLE
in
1 2 3 4 ... N #
for variable in values
do
command1
command2
commandN
done
OR
for
VARIABLES in
file1
file2
file3
do
command1
on
$VARIABLE
command2
commandN
done
OR
for
OUTPUT in
$(Linux-Or-Unix-Command-Here)
do
command1
on $OUTPUT
command2
on $OUTPUT
commandN
done
Example:
#!/bin/bash
for
file
in `ls *.txt`
do
echo
$file
done
While
Syntax:
#!/bin/bash
while
[ condition ]
do
statements
done
Example:
#!/bin/bash
i=1
while
[ $i
-le
20 ]
do
echo
"Here
we go again"
i=$(($i+1))
#i++
#
equivalent to
i=`expr
$i
+ 1`
<-- usually
slower
done
Some
important Unix Command
cut
command
/>
remove selected section
$ cut
-c2 text.txt
$ cut
-c9- text.txt
$ cut
-c1-3 text.txt
$ cut
-d':' -f1 text.txt
tr
command
/>
translate or subsitute or delete character
$ tr
a b {text from stdinput} : subsitute all occurrence of 'a' with 'b'
$ tr
a-z C {text from stdinput} : subsitute all lowercase char with 'C'
uniq
command
$
uniq input.txt
Laboratory
8
Learning
goals: in this laboratory activity you will learn how to
synchronize two processes by
using
pipes. You will also improve your knowledge about the Bash shell.
Moreover, you will
practice
using the grep, find, uniq, wc,tr, and cut commands.
Exercise
1
Write a
C program in which a parent process creates two child processes, a
producer and a
consumer.
The producer reads lines of text from stdin and writes them to a
pipe. The consumer
process
reads data from the pipe and writes them to stdout, converting text
lines to capital letters.
Introducing
the “end” string terminates both the child processes and the
parent process.
Exercise
2
Consider
the content the following file:
Then:
1. Sort
data in reverse order by first field(Product name)
2. Sort
data by second field(Quantity number)
3.
Calculate the total number of Books (hint: use “paste -sd+ | bc”
to sum a column of
numbers)
4. Print
just the list of the products in capital letters(each kind of product
has to appear just
one
time!) - hint → type “man tr”
Exercise
3
Write a
Bash script reading three command line parameters: a directory, a
function name and the
name of
an output file. The script has to search for all the files in the
directory which contain the
function
passed as the second parameter. For each occurrence of the function
it has to print the
name of
the file, the matching line number and the matching line itself.
Results must be sorted
according
to the filename first and then according to the line number and
written to the output file
whose
name is specified as the third command line parameter.
Exercise
4
Write a
Bash script that given a filename as a command line parameter prints
the length of the
longest
line in the file and the total number of lines of the file. Suppose
each line does not contain
spaces.
Exercise
5 (sync - review exercise)
Consider
the following fragment of code
int
main () {
int
i;
i =
0;
while
(i<=2 && fork()){
if
(!fork()) {
fprintf
(stdout, "Running Exec ..."); fflush (stdout);
execlp
("echo", "echo", "i*i", (char *) 0);
}
i++;
fprintf
(stdout, "Running System ..."); fflush (stdout);
system
("echo i+i");
}
}
return
(0);
Without
running it:
1. Draw
the process creation tree
2. Try
to figure out the output of the program for different executions
Then
execute the program and compare with your results!
Summary
At the
end of this laboratory activity you should understand how to
synchronize two
processes
by means of a pipe. You should also have improved your understanding
of find,
grep, wc, cut, sort,tr, and uniq commands.
Comments
Post a Comment