help running strace

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

I know that grep nor sort are aware of the pipe, they just input/output to the stdin/stdout... the pipe just connect them, that's how pipes works as far as I know... 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 relation? 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.

strace has absolutely no idea how to set up pipes for anything, it only runs one process. The strace bit only goes as far as 'grep blah myfile', after which point the shell breaks your statement in half. sort is run by itself without the benefit of an strace.

How about:

strace grep blah myfile 2> grep.strace | strace sort 2> sort.strace

The relation between what's written into a pipe and what's read from a pipe is identical. You have

write(1, "blah blah and blah cause of blah"..., 85) = 85

on grep's side, and on sort's side you'll have something like

read( 0, "blah blah and blah cause of blah"..., 32768) = 85

The pipe waits until the writing process has written an '\n' somewhere, or the pipe's buffer is totally filled, before the other process stops waiting and gets to read any of it.

It makes more sense now, I never thought of it as you painted it. Thanks a lot Corona.

Use strace to monitor what happens when the user creates a pipe as follows:

 grep <pattern> <file> | sort

According to their man pages, both grep(1) and sort(1) process data on a line-by-line basis. Keeping this in mind, answer the following questions in a plain text file called answers.txt:

  1. How does the grep process output data into the pipe according to the strace output? Please explain the behaviour you observe.
  2. How does the sort process read data from the pipe according to the strace output? Please explain the behaviour you observe.
  3. Does sort write the final result on a line-by-line basis according to the strace output? Please explain the behaviour you observe.

Hint: please make sure that you use appropriate input file for grep, i.e. one that is large enough, and an appropriate search pattern, which matches often enough.

klam/jason296 - Please read the forum rules - you'll see that homework questions are not permitted.

You made a very clever attempt at disguising your homework question at first. I'm sure the CS tutors at York University can help you further.

Cheers
ZB