0% found this document useful (0 votes)
24 views

Pipe For Performance

The document examines the behavior of commands connected through pipes in Linux/Unix shells. It shows that commands run concurrently when connected by pipes, allowing faster execution than sequential execution. An experiment uses a C++ program to find primes between ranges. Piping three instances of the program to separately process input ranges is faster than one instance processing the full range, demonstrating the concurrent execution enabled by pipes.

Uploaded by

ramchik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Pipe For Performance

The document examines the behavior of commands connected through pipes in Linux/Unix shells. It shows that commands run concurrently when connected by pipes, allowing faster execution than sequential execution. An experiment uses a C++ program to find primes between ranges. Piping three instances of the program to separately process input ranges is faster than one instance processing the full range, demonstrating the concurrent execution enabled by pipes.

Uploaded by

ramchik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exploiting the pipe operator of Linux / Unix shell

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.

The following C++ program is used in this document for illustration :


/* filename : prime.C
i/p : a range [ low, high]
o/p : all prime numbers in the range
method :
a) Check for each number n, whether any number i, 2 <= i <= floor(sqrt(n)), is a factor of n
b) if no such i exists, n is prime and composite otherwise
*/
#include <iostream>
using namespace std;

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

$ g++ prime.C -o prime

Execute the command : $ time ./prime < inp > out

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.

real 0m0.495s real 0m0.492s real 0m0.525s


user 0m0.479s user 0m0.472s user 0m0.473s
sys 0m0.016s sys 0m0.020s sys 0m0.052s

• 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.

Inp1 : 100000 200000


Inp2 : 200001 300000
Inp3 : 300001 400000

Execute the following command

$ 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.

real 0m0.279s real 0m0.316s real 0m0.274s


user 0m0.705s user 0m0.723s user 0m0.697s
sys 0m0.064s sys 0m0.037s sys 0m0.064s

The following observations can be made.

• 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.

What can be summarized from the above experimentation ?

How does one explain the observed behavior ?

**** end of document *****

CS213m/SB/2018/3

You might also like