You are closing p1.stdout
in the parent process, thus leaving dmesg as the only process with that file descriptor open. If you didn’t do this, even when dmesg closed its stdout, you would still have it open, and a SIGPIPE
would not be generated. (The OS basically keeps a reference count, and generates SIGPIPE
when it hits zero. If you don’t close the file, you prevent it from ever reaching zero.)