# Solved: ..... strace and a pipe



## klam (Apr 21, 2006)

OK so I wanted to know how does grep outputs to the pipe and how sort reads from it. So I run a strace over "grep blah myfile | sort" and this is what I got:

*open("myfile", O_RDONLY|O_LARGEFILE) = 3*
fstat64(3, {st_mode=S_IFREG|0600, st_size=84, ...}) = 0
*read(3, "blah blah and blah cause of blah"..., 32768) = 84*
fstat64(1, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fe8000
read(3, "", 32768) = 0
close(3) = 0
*write(1, "blah blah and blah cause of blah"..., 85) = 85*
close(1) = 0
munmap(0xb7fe8000, 4096) = 0
exit_group(0) = ?
Process 14533 detached
blah blah and blah cause of blah
find blah myfile blah grep it.
this is my blah file

Obviously grep nor sort are aware of the pipe they just input/output to the stdin/stdout... the pipe just connect them. But what I don't see in this code is how grep is outputing to the pipe (stdout) and how is sort reading from the pipe (stdin). Is there anyway to check that realtion? All I can tell from the code I bolded is:

MyFile -> Grep --------------------------> stdout

Unless "read(3, "", 32768)" has anything to do with the sort process.
Thanks in advance.


----------



## O111111O (Aug 27, 2005)

Here you go, it's a great story. In my mind, one of the most elegant innovations for computing ever.

http://www.softpanorama.org/Scripting/pipes.shtml


----------



## klam (Apr 21, 2006)

Not quite what I was looking for but thanks anyways... :up:


----------



## AGCurry (Jun 15, 2005)

klam said:


> *
> open("myfile", O_RDONLY|O_LARGEFILE) = 3*
> fstat64(3, {st_mode=S_IFREG|0600, st_size=84, ...}) = 0
> *read(3, "blah blah and blah cause of blah"..., 32768) = 84*
> ...


No, "read( 3, ...)" is from grep. File descriptor 3 is input from myfile. Sort is not telling you anything about itself, but grep is telling you a little and the "|" is telling you quite a bit.

File descriptor 1 is stdout. fstat64 checks on the status of stdout; then, mmap2 maps an area of memory to stdout. That's how the pipe is working - as a memory map, which sort reads from.


----------

