1

My objective is to be able to determine how many bytes have been transferred into the write end of a pipe. Perhaps, one would need to access the f_pos member of the struct file structure from linux/fs.h associated with this pipe.

struct file snipfrom fs.h

Is it possible to access this value from a userspace program? Again, I'd just like to be able to determine (perhaps based on the f_pos value) how many bytes are stored in the kernel buffer backing the pipe.

I have a feeling this isn't possible and one has to keep reading until read(int fd, void *buf, size_t count) returns less bytes than count.. then at this point, all bytes have been "emptied out" I assume..

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Welcome to Stack Overflow! Here we want to have each question post described a **single problem**. But your post is about several problems at once: 1. `What structure is defined that keeps track of the buffer` - this is part of the **kernel**. 2. `There must be some kind of structure pointed to be the index referred to by a file descriptor` - kernel keeps mapping from file descriptors for **all files**, not only for *pipes*. 3. Bytes available in the pipe can be requested by `ioctl(fd, FIONREAD, &nbytes);`. Please, [edit] the question post and leave single question/problem in it. – Tsyvarev Feb 08 '17 at 08:10
  • Hello, this answered all my questions. I didn't actually notice you answered them until reading your comment more carefully. –  Feb 09 '17 at 01:38

1 Answers1

3

Amount of bytes available for read from the pipe can be requested by

ioctl(fd, FIONREAD, &nbytes);

Here fd is a file descriptor, and variable nbytes, where result will be stored, is int variable.

Taken from: man 7 pipe.


Amount of bytes available for write is a different story.

Community
  • 1
  • 1
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153