Sponsored Content
Full Discussion: How does pipe work?
Top Forums UNIX for Dummies Questions & Answers How does pipe work? Post 302268790 by Perderabo on Tuesday 16th of December 2008 10:03:04 AM
Old 12-16-2008
Quote:
Originally Posted by siba.s.nayak
Here A must finish before B starts. Because The output of A is the input for B.
That is not correct. It is undefined whether A or B starts first. They might start at the exactly the same time if there are multiple cpu's. A pipe can hold an undefined but finite amount of data.

If B tries to read from the pipe, but no data is available, B will wait until the data arrives. If B was reading from a disk, B might have the same problem and need to wait until a disk read finishes. A closer analogy would be reading from a keyboard. There, B would need to wait for a user to type. But in all of these cases, B has started a "read" operation and must wait until it finishes.

If A tries to write to the pipe, and the pipe is full, A must wait for some room in the pipe to become free. A could have the same problem if A was writing to a terminal. A terminal has flow control and can moderate the pace of data. In any event, to A, it has started a "write" operation and will wait until the write operation finishes.

A and B are behaving as co-processes, although not all co-processes will be communicating with a pipe. Neither is in full control of the other.

In a case, like:
A | sort
The sort command cannot output anything until it reads all of the data. So the sort command will do that, just as it would if it was reading from a file. Many other programs strive to read and write data if they can. This allows them to be used in long pipelines with data continuously flowing though the entire pipeline.
This User Gave Thanks to Perderabo For This Post:
 

10 More Discussions You Might Find Interesting

1. Linux

By angle-brackets/"pipe" button doesn't work?

How can I configure it? I have a swedish keyboard with swedish keyboard setting. Everything works perfectly (едц) except that button. What can be wrong? /Richard ++ NOTE: It seems like the computer notices the input but that the button isn't assigned to anything (the keyboard-cursor stops).... (1 Reply)
Discussion started by: riwa
1 Replies

2. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

3. Shell Programming and Scripting

How to Avoid intermediate files when pipe does nt work

problem with piping one output to another.Would like to avoid the intermediate file creation.The piping does nt work on places where files have been created and goes in an endless loop. sed -e "s/^\.\///g" $LINE1| sed -e "s/_\(\)/kkk\1/g" > $file1 tr -s '_' ' ' < $file1| \ sort -n -k... (1 Reply)
Discussion started by: w020637
1 Replies

4. Shell Programming and Scripting

awk's getline < "-" seems not work for pipe

Hi all, I have an gawk script to get user's input, So I use getline name < "-" (or getline name < "/dev/stdin") in my script They both work fine when my script deals with files. But it is broken for pipes. When I try "some command | my awk script", the variable name just gets an empty... (17 Replies)
Discussion started by: qiulang
17 Replies

5. Shell Programming and Scripting

Replace pipe with Broken Pipe

Hi All , Is there any way to replace the pipe ( | ) with the broken pipe (0xA6) in unix (1 Reply)
Discussion started by: saj
1 Replies

6. Shell Programming and Scripting

pipe to grep doesn't work in bash script

Hi, I'm trying to write a script that checks gvfs to see if a mount exists so I can run it from network-manager's status hooks. I thought I'd pipe the output of gvfs-mount -l to grep for the particular mounts I care about. When I do this in a bash script: cmnd="gvfs-mount -l | grep -i... (4 Replies)
Discussion started by: kcstrom
4 Replies

7. Shell Programming and Scripting

How to ignore Pipe in Pipe delimited file?

Hi guys, I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file for eg: file 1: xx|yy|"xyz|zzz"|zzz|12... using below awk command awk 'BEGIN {FS=OFS="|" } print $3 i would get xyz But i want as : xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

8. Shell Programming and Scripting

Why does bc work with 'here string' and not via pipe in this example?

Hi! I actually got it running, but I still would like to understand, why and how, since I am a beginner in bash scripting. I Need floating numbers and thus use bc in my bash script. Here it is: #!/bin/bash num1="10^-15" | bc -l #power function piped to bc - DOES NOT WORK echo $num1... (4 Replies)
Discussion started by: McHale
4 Replies

9. Shell Programming and Scripting

Would pipe work better with this command

Hi again, have a script that I would like run, but before I can run it I need to strip out the windows \r end of lines. I have put the command into a text file and set the command to run every 10 seconds the coomand I use to do this is while sleep 10; do... (15 Replies)
Discussion started by: Paul Walker
15 Replies

10. IP Networking

Discussion at work, would a router work pluging a cable in wan1 and lan1?

hi all. and sorry for the random question, but this sparkled a raging flame-war at work and i want more points of view situation a router, with linux of some sort, dhcp client requesting for ip in wan1 (as usual with wan ports) dhcp server listening in lan1, and assigning ip (as usual... (9 Replies)
Discussion started by: broli
9 Replies
PIPE(2) 						      BSD System Calls Manual							   PIPE(2)

NAME
pipe -- create descriptor pair for interprocess communication SYNOPSIS
#include <unistd.h> int pipe(int *fildes); DESCRIPTION
The pipe() function creates a pipe, which is an object allowing unidirectional data flow, and allocates a pair of file descriptors. The first descriptor connects to the read end of the pipe, and the second connects to the write end, so that data written to fildes[1] appears on (i.e., can be read from) fildes[0]. This allows the output of one program to be sent to another program: the source's standard output is set up to be the write end of the pipe, and the sink's standard input is set up to be the read end of the pipe. The pipe itself persists until all its associated descriptors are closed. A pipe whose read or write end has been closed is considered widowed. Writing on such a pipe causes the writing process to receive a SIGPIPE signal. Widowing a pipe is the only way to deliver end-of-file to a reader: after the reader consumes any buffered data, reading a widowed pipe returns a zero count. RETURN VALUES
On successful creation of the pipe, zero is returned. Otherwise, a value of -1 is returned and the variable errno set to indicate the error. ERRORS
The pipe() call will fail if: [EMFILE] Too many descriptors are active. [ENFILE] The system file table is full. [EFAULT] The fildes buffer is in an invalid area of the process's address space. SEE ALSO
sh(1), read(2), write(2), fork(2), socketpair(2) HISTORY
A pipe() function call appeared in Version 6 AT&T UNIX. 4th Berkeley Distribution June 4, 1993 4th Berkeley Distribution
All times are GMT -4. The time now is 06:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy