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

Hello,

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");
        }
        close(fd);
        return;
}

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

        pipe(infd);
        pipe(outfd);

        if ((cpid = fork()) != 0) {
                close(infd[1]);
                close(outfd[1]);

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

                sendnote(cpid);
                // 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);
                wait();
                return 0;
        }
        dup(infd[1], 0);
        close(infd[0]);
        close(infd[1]);
        dup(outfd[1], 1);
        close(outfd[0]);
        close(outfd[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

* [9fans] Re: Pipes staying after sending note
  2023-07-08 15:46 [9fans] Pipes staying after sending note Philip Silva via 9fans
@ 2023-07-09  0:03 ` Anthony Martin
  0 siblings, 0 replies; 2+ messages in thread
From: Anthony Martin @ 2023-07-09  0:03 UTC (permalink / raw)
  To: 9fans

Philip Silva via 9fans <9fans@9fans.net> once said:
>         if ((cpid = fork()) != 0) {
>                 close(infd[1]);
>                 close(outfd[1]);
>
>                 n = write(infd[0], "test", 4);
>                 printf("check process: wrote %d bytes\n", n);
>
>                 sendnote(cpid);
>                 // 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);
>                 wait();
>                 return 0;

Why aren't you waiting for the process to die
after sending it the note? It must be running
to see that is has a note pending. At the time
of your second write, the scheduler may not
have selected the child process to run.

The sequence could look like:

child: read from empty pipe, go to sleep
parent: write to pipe
parent: write kill to /proc/$cpid/note
parent: write to pipe
child: wakeup, see note, die

Cheers,
  Anthony

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T3392001a02ee7ec8-Mfe03d0d140063db1ea509f2a
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).