9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] Pipes staying after sending note
@ 2023-07-08 15:46 Philip Silva via 9fans
  2023-07-09  0:03 ` [9fans] " Anthony Martin
  0 siblings, 1 reply; 2+ messages in thread
From: Philip Silva via 9fans @ 2023-07-08 15:46 UTC (permalink / raw)
  To: 9fans


I'm trying to understand how pipes work when terminating a forked process. It seems when sending kill to the forked process, connected pipes don't always break. At least a subsequent write might work. Is it possible to make it reliably fail anyway or is it necessary to close the file descriptors from the calling process?

#include <u.h>
#include <libc.h>
#include <stdio.h>

void sendnote(int cpid) {
        int fd;
        char fn[20], buf[20];

        sprintf(fn, "/proc/%d/note", cpid);
        if ((fd = open(fn, OWRITE)) <= 0) {
                printf("open %s\n", fd);
        if (write(fd, "kill", 5) == 0) {
                printf("write error\n");

int main() {
        int cpid, infd[2], outfd[2], n;
        char outbuf[20], inbuf[20];
        char *args[] = {"/bin/cat", NULL};


        if ((cpid = fork()) != 0) {

                n = write(infd[0], "test", 4);
                printf("check process: wrote %d bytes\n", n);

                // close(infd[0]);  // <--- uncomment to make write fail
                // close(outfd[0]);

                n = write(infd[0], "test2", 5);
                printf("write: wrote %d bytes\n", n);

                n = read(outfd[0], outbuf, 20);
                printf("outbuf=%s (n=%d)\n", outbuf, n);
                return 0;
        dup(infd[1], 0);
        dup(outfd[1], 1);
        exec("/bin/cat", args);
        return 0;

Will output:

check process: wrote 4 bytes
write: wrote 5 bytes
outbuf=test (n=4)
6.out 82435: main

Closing the FDs after sending the note:

check process: wrote 4 bytes
write: wrote -1 bytes
outbuf= (n=-1)
6.out 82423: main

On the other hand instead of closing just adding sleep(1) after sendnote() produces:

check process: wrote 4 bytes
6.out 82595: sys: write on closed pipe pc=0x202603

(That's more or less also reproducing a part of Go's os/exec TestContextCancel)

Greetings, Philip

9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T3392001a02ee7ec8-Ma261d9fb098775daa526c6b3
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-07-09  0:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-08 15:46 [9fans] Pipes staying after sending note Philip Silva via 9fans
2023-07-09  0:03 ` [9fans] " Anthony Martin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).