Do pipes know when they have to "wait" for all the data?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Do pipes know when they have to "wait" for all the data?
# 1  
Old 04-12-2010
Do pipes know when they have to "wait" for all the data?

Hi,
I was wondering if pipes ("|"), or rather the command that follow them, know when they're supposed to wait for all the data?

For instance, if you take this:

cat my_file | sort | uniq

for uniq to work well, it needs to have rows sorted, but for lines to be sorted properly, it needs my_file in its entirety. So, will "uniq" wait until it has the entire file before starting to process? Smilie

I've tried a couple of manual test and it looks like it does, but I suspect it's just that my input file is too small and that therefore all results arrive so quickly that uniq has everything it needs already.

Any hindsight would be welcome!
Thanks,

Anthony
# 2  
Old 04-12-2010
In general terms, yes. If there is too much data stuck because the other process does not take it out again quickly enough, the original process automatically blocks to guarantee that all data is processed:

Quote:
A sending program may produce 5000 bytes per second, and a receiving program may only be able to accept 100 bytes per second, but no data are lost. Instead, the output of the sending program is held in a buffer, or queue. When the receiving program is ready to read data, the operating system sends it data from the buffer, then removes that data from the buffer. If the buffer fills up, the sending program is suspended (blocked) until the receiving program has had a chance to read some data and make room in the buffer. - Pipeline
Smilie
# 3  
Old 04-12-2010
More importantly, "sort" will wait until it has all the records before passing them sorted to "uniq".
The "uniq" program does not work on the whole file, it just looks at consecutive records. This is why it needs sorted input.
# 4  
Old 04-12-2010
Thank you both.

So what I understand from what you say, is that doing this:

cat my_file | sort > tmp
uniq <tmp

and

cat my_file | sort | uniq

is equivalent in terms of the result it produces (not in terms of processing time of course). Can I safely assume this is always the case?
# 5  
Old 04-12-2010
One never wants to assume, but it's pretty safe to bank on it. Take for example, piping sqlplus output through tr or sed to assist in formatting: the end-point in the redirection may receive content in spurts as the pages are released, but the process and/or resource itself is always gated and not released until completion.

Last edited by curleb; 04-12-2010 at 01:58 PM.. Reason: clarification
# 6  
Old 04-12-2010
Thanks a lot guys, that's very helpful Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

calling a shell script in background and wait using "wait" in while loop

Hi, I am facing a strange issue, when i call a script from my while loop in background it doesnt go in background, despite the wait i put below the whil loop it goes forward even before the process put in background is completed. cat abc.txt | while read -u4 line do #if line contains #... (2 Replies)
Discussion started by: mihirvora16
2 Replies

5. HP-UX

echo "selall;info;wait;infolog" | /usr/sbin/cstm problem

Hello, On a HP-UX 10.20 server I've executed something similar to this command: # echo 'selall;info;wait;infolog;view;done' | /usr/sbin/cstm But it returns sometype of "argument list too long" error. I suppose there is a way to fix it by using xargs but I can't figure it out. Any... (7 Replies)
Discussion started by: asanchez
7 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Programming

Pipes connecting 3 processes in a "circle"

I am trying to get a better understanding of pipes and processes. I have code in which I link 3 processes A,B,C. I have A->B->C but how would I go about getting C->A? Here is my code: #include <stdio.h> #include <stdlib.h> #include <unistd.h> main() { pid_t A, B, C; int fd; int fd2; ... (1 Reply)
Discussion started by: tfarmer4
1 Replies

8. Shell Programming and Scripting

Don't wait for "read line"

Hi I am writing a bash script on Solaris, that should take n arguments, either appended to the script or taken as output from the last command (similar to grep). What I don't want is that the script waits for user input. In other words: Possibility 1: script.sh arg1 arg2 arg3 ...Possibility... (4 Replies)
Discussion started by: g000ze
4 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question