Consider this code of prog.c:
#include <sys/time.h>
#include <stdio.h>
#define SIZE 10000
int main(){
char buffer[SIZE];
int pipefd[2];
pipe(pipefd);
while (1){
write(pipefd[1], buffer, SIZE);
read(pipefd[0], buffer, SIZE);
}
return 0;
}
When i run it in a bash with the following:
time ./prog
The output would be:
real 0m2.672s
user 0m0.693s
sys 0m1.978s
As you can see user time + sys time = (approximately) real time.
And here I'm wondering, why doesn't the reading and writing into a pipe take I/O time which would manifest by a real time much bigger than the sum of user and sys times.
pipe
is basically a simple in-memory buffer. What you do when you write and read in the same process is simple copying to and from the buffer. More specifically, since it's all done in memory there's no actual I/O or device access.