9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Philip Silva via 9fans" <9fans@9fans.net>
To: 9fans <9fans@9fans.net>
Subject: [9fans] Pipes staying after sending note
Date: Sat, 08 Jul 2023 15:46:23 +0000	[thread overview]
Message-ID: <abToz2TrFbB_rDZLToX8KYmdmHCsdFYT4ZADL09J-GIQHz_IwpO_zPS65WI0SAxidGFcDtMn0_wHe3a7Bq3rbugbMz5SKyjXyxCMAOOr9t0=@protonmail.com> (raw)


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

             reply	other threads:[~2023-07-08 15:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-08 15:46 Philip Silva via 9fans [this message]
2023-07-09  0:03 ` [9fans] " Anthony Martin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='abToz2TrFbB_rDZLToX8KYmdmHCsdFYT4ZADL09J-GIQHz_IwpO_zPS65WI0SAxidGFcDtMn0_wHe3a7Bq3rbugbMz5SKyjXyxCMAOOr9t0=@protonmail.com' \
    --to=9fans@9fans.net \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).