1

Hi I would like to know if its possible to count the size of bytes of a pipe's content like the read function but without using read (I don't want to lose it's content just know the size). I tried using

struct stat sb;
fstat(fd,&sb);

and then using sb.st_size but it doesn't seem to give me the right size with file descriptors of pipes

  • If it's a pipe, how would any answer prevent more bytes being written to the pipe? – Andrew Henle Dec 10 '21 at 01:53
  • 1
    You can't. Why do you need to know this? – Barmar Dec 10 '21 at 01:54
  • I'm looking for its size at a certain moment like if i'd do a read but without clearing the data inside – BrokenStarz111 Dec 10 '21 at 01:55
  • @Barmar to solve the problem that i have at the previous question XD – BrokenStarz111 Dec 10 '21 at 01:56
  • I really don't understand this project that you and asmazizou are working on, but it seems to be way too complicated. – Barmar Dec 10 '21 at 01:57
  • 3
    *I'm looking for its size at a certain moment* That bug is so common it even has [it's own Wikipedia page](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use) Whatever number you get is instantly wrong. – Andrew Henle Dec 10 '21 at 01:59
  • Or you're asking [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) questions. Instead of asking questions about how to read and write pipes, ask a question about the high-level result you're trying to achieve. – Barmar Dec 10 '21 at 02:00
  • @Barmar I guess its basically a program that simulates the tee command for N processes – BrokenStarz111 Dec 10 '21 at 02:01
  • So you write `command1 : command2 : command3` and it's supposed to do `command1 | command2` and also `command1 | command3` (but only run `command1` once, of course)? – Barmar Dec 10 '21 at 02:04
  • almost command1|command2|command3 you also need to record the size that's passed in a .txt so in the .txt you would have nb bytes of command1 output, nbbytes of command2 output and so on – BrokenStarz111 Dec 10 '21 at 02:07
  • There's some kind of conservation law going on here, of unmet expectations about reading data. Over at https://stackoverflow.com/questions/70298250 we had Hercules imagining that reading a packet will consume it, when it doesn't. But here BrokenStarz111 wants to read a pipe, *without* consuming it. – Steve Summit Dec 10 '21 at 02:09
  • @SteveSummit it says page not found – BrokenStarz111 Dec 10 '21 at 02:11
  • Yeah, sorry, I know, it got deleted. But don't worry, it wouldn't have helped you. I was just kibitzing. – Steve Summit Dec 10 '21 at 02:12
  • 1
    @BrokenStarz111: if you want to record information about data as it passes through from one process to the next, you need to read it, record what you want recorded, and then write it to a different pipe for the next process. If you know how to construct pipes, that's just one more brick in the wall. – rici Dec 10 '21 at 05:23
  • 1
    So it sounds like maybe what you really want is to know *whether* there is data in a given pipe, so you can tell whether it will block if you try to read. The correct solution to this is `select(2)` or `poll(2)`, which also let you multiplex the pipes, and wait for input on any one of them without wasting CPU on a loop. – Nate Eldredge Dec 10 '21 at 06:01
  • 1
    Looking at the last questions you posted, it seems there is some misunderstanding how a byte stream like a pipe work. A pipe can only have a single consumer. You cannot let two consumers read from the same pipe. Any duplication has to be done by other means. Even "tee" is connected by two independent pipes by `some_process | tee file | other_process`, and it additionally duplicates the received data to write it to the file. – the busybee Dec 10 '21 at 10:24

0 Answers0