Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How in portable C to seek forward when reading from a pipe

Tags:

c

pipe

seek

fseek

Since fseek() does not work on pipes what methods exist for simulating seeking forward? The naive approach is to use fread() and throw away the contents read into the memory buffer. For huge seeks to avoid huge buffers you would use the same buffer over and over with the final read using just a part of the buffer.

But is this the only approach? Is there another way which avoids the buffer and the potential multiple read?

like image 856
hippietrail Avatar asked Apr 27 '11 15:04

hippietrail


1 Answers

Seeking doesn't make sense on pipes because the input is produced dynamically (not stored on disk). The lseek kernel system call is not implemented for pipes.

Also have in mind that a pipe is essentially a producer-consumer buffer of a limited, fixed size. When it gets full, the producer is suspended until the consumer reads the oldest data.

like image 123
Blagovest Buyukliev Avatar answered Nov 05 '22 16:11

Blagovest Buyukliev