1

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.

4
  • 3
    A pipe created by 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. Commented Jan 22, 2020 at 8:52
  • so it's just a copy/past in RAM zone, not an actual write/read into I/O device? so the spent time is basically in the kernel side where it does the copying. Commented Jan 22, 2020 at 8:54
  • @OSEMATOUATI Yes that's correct. Commented Jan 22, 2020 at 8:54
  • @Someprogrammerdude yes, I was wrong. I didn't know it . Thanks for the teaching Someprogrammerdude Commented Jan 22, 2020 at 9:02

1 Answer 1

1

With the help of the comment section, it appears that the write/read are done in buffer corresponding to the pipe itself, there is no actual read/write into an I/O device.

This copy/past of buffer zone is done by syscall write/read where the kernel spend it time coping from the buffer of variable to the buffer of the pipe.

That's why the sum of user and sys time are the actual real time.

Not the answer you're looking for? Browse other questions tagged or ask your own question.