help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: About using COPROC


From: Marco Ippolito
Subject: Re: About using COPROC
Date: Fri, 2 Apr 2021 19:15:47 -0300

> By adding a longer sleep time, the following script works. But why the
> coprocess is not halt to wait for its output to be taken? This
> behavior is counterintuitive.

Writes to stdout inside the coproc go into the kernel buffer for the pipe.

Try this snippet to prove it to yourself, as you see.. the write to /tmp/urgent
happens immediately:

    coproc {
        echo Hello, world
        echo "NOW $$" > /tmp/urgent
        sleep 5
        echo Done
    }
    my_pid=$$
    echo "Parent PID: $my_pid"
    declare -p COPROC_PID
    declare -p COPROC
    cat /tmp/urgent
    lsof -p "$my_pid" -a +E | grep FIFO
    cat <&"${COPROC[0]}"

You'll notice:

* Writing "Hello, world" did not block
* /tmp/urgent was written to immediately by the coproc
* /tmp/urgent can be read immediately by the parent
* Pipes are set by the kernel with matching fds
* The first coproc write can be read by the parent as soon as it happens
* The delayed coproc write is read by the parent after the sleep

Maybe you are thinking about named pipes, which block until the other side
consumes the stream?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]