Pipe For Performance
Pipe For Performance
The pipe operator of the shell is used to connect two or more shell commands through a pipe.
Essentially the output produced by the command at the left end of a pipe is buffered as input to the
command at the right end of the pipe. If exec1 and exec2 are two executable files, then they are
connected through a pipe using the pipe operator “|” as shown below.
$ ./exec1 | ./exec2
What is not at all obvious is that both the commands connected through a pipe are run concurrently.
The behaviour of commands connected through a a pipe is examined through a series of experiments.
int main()
{
int low, high;
int primecount = 0, factcount;
cout << " give 2 integers as range low and high ";
cin >> low >> high;
for ( int num = low; num <= high; num++)
{ factcount = 0;
for ( int i = 2; i*i <= num ; i++ )
if ( num % i == 0 ) factcount++; // efficient ???
if (factcount == 0)
primecount ++; cout << " Prime No. " << primecount << " is : " << num << endl;
}
cout << " There are " << primecount << " primes in the range [" << low << " , "
<< high << "]" << endl;
return 0;
}
CS213m/SB/2018/1
Experiment 1 :
Executable file, prime, is produced from prime.C which finds and reports all primes in the range from
100000 to 400000. Create a file named as “inp” with the following content :
100000
400000
The command above uses shell i/o redirection operators : < for input file redirection and > for output
file redirection. The executable prime reads inout from file “inp” and writes its output in the file “out”.
The display on the screen is given below.
Run1 : Result of execution of prime for three runs are given below.
• The figures in the 3 columns reveal that all the 3 measures of run time depends on the actual
workload in the OS and can vary within a certain range.
• It is therefore customary to run an executable with the same input lare number of times and then
take the mean time as its representative run time.
• For this experiment we shall consider the real time as our desired run time. Note that real time
is like wall clock time while user and sys give the time spent by the executable in user mode
and in kernel mode respectively.
• The contents of out show there are 24268 primes in [100000, 400000] with 100003 and 399989
being the least and largest primes.
Experiment 2 :
Three input files, inp1, inp2 and inp3, are created with contents as shown below.
$ time (./prime < inp1 > out1 | ./prime < inp2 >out2 | ./prime < inp3 > out3)
CS213m/SB/2018/2
The result of three runs of the above shell command is as follows.
• The same executable prime is executed 3 times by connecting through two pipes. The first
process reads input from inp1 and writes its output to out1; next process reads input from inp2
and writes its output to out2, while the last process process reads input from inp3 and writes its
output to out3.
• The contents of the files out1, out2 and out3 are as follows :
out1 : least prime : 100003 largest prime : 199999 Number of primes : 8392
out2 : least prime : 200009 largest prime : 299993 Number of primes : 8013
out3 : least prime : 300017 largest prime : 399989 Number of primes : 7863
Comparing the output with that of Experiment 1, we find that both the experiments have
produced essentially the same output.
• The real times of the three runs are 0.279s, 0.316s and 0.274s respectively for Experiment 2
while the same for Experiment 1 was 0.495, 0.492s and 0.525s.
• Experiment 2 produced the same result faster as compared to Experiment 1.
CS213m/SB/2018/3