Clobbered
Clobbered

Reputation: 15

Using "tee" makes the stdout slow

I have a program which does a lot of work. I want to log all the console prints into a file. So i used a tee with my executable.

I implemented a tee which reads from stdin and writes to stdout and a file.

exec run.sh | tee loglink

But what i could see is, time to get the login for my program, which used to be 3 mins now is taking 6 mins.

What is the reason for this delay? I commented file operation part of my tee, still seeing the same delay. Is it the pipeline that is causing this issue?

Adding code,

char ch;
fd = open(file_name, O_WRONLY | O_APPEND, 0664);
while(read(STDIN_FILENO, &ch, 1) > 0)
{
    write(STDOUT_FILENO, &ch, 1); //write to console
    fflush(stdout);
    write(fd, &ch, 1); //write to the file
}

Upvotes: 1

Views: 2109

Answers (1)

Zang MingJie
Zang MingJie

Reputation: 5275

read(STDIN_FILENO, &ch, 1)

You only read 1 byte per read call. it is very very slow, please increase the buffer, and read as more as you can per read call

Upvotes: 1

Related Questions