0

As far as I know piping command A to command B will execute A and give it's output to B as input. While some commands are endless, like yes, and therefore execution time for these commands is until we break them. How is piping work for them?

example : yes | sudo dnf install pkg

1 Answers1

2

Although command A might produce an endless output, command B will only read a finite amount of it. When command B exits (or closes its input file descriptor), the pipe will be broken.

After that, any write into the pipe from command A will cause the kernel to send a SIGPIPE signal to command A. The default action of SIGPIPE is to terminate the process.

telcoM
  • 96,466
  • thanks for your answer, you mean both A and B will start execution simultaneously but B waits until receives any input from A? – Amir reza Riahi Jan 08 '21 at 19:30
  • 1
    Yes, both A and B will run in parallel. B may wait for input, or in some cases it might not read any input from the pipe at all (e.g. yes | echo 'This does not use the output of "yes" at all'). In cases where B won't read input from the pipe, A will run until the pipe's buffer capacity is full, and then A will wait until either B has read at least some of it, or the pipe is broken. Optionally, A may set O_NONBLOCK on the pipe, in which case the kernel will report a full buffer to A, instead of making it wait. See man 7 pipe for more information on pipes. – telcoM Jan 08 '21 at 20:02