bug-bash
[Top][All Lists]
Advanced

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

Re: Examples of concurrent coproc usage?


From: felix
Subject: Re: Examples of concurrent coproc usage?
Date: Wed, 17 Apr 2024 11:22:55 +0200

Some other way of thinking:

> On 3/14/24 5:58 AM, Carl Edquist wrote:
> > ...
> > But if you try...
> > 
> >      $ coproc WC { wc; }
> >      $ coproc CAT { cat; }
> >      $ exec {WC[1]}>&-
> >      $ read -u ${WC[0]} X
> > 
> >      # HANGS

To prevent  `exec {WC[1]}>&-` to close both FD, just because subpid is
ended, I open a *backup FD*:

  coTest() { 
    local X WC CAT bakWcOut wcPid
    coproc WC { exec wc; }
    wcPid=$!
    coproc CAT { exec cat; }
    echo foo exec bar >&${WC[1]}  # Not really usefull for demo
    exec {bakWcOut}>&${WC[0]}
    exec {WC[1]}>&-
    wait $wcPid
    read -u ${bakWcOut} X
    echo $X
    exec {bkWc}>&-
    exec {CAT[1]}>&-
    wait
    ls -l /dev/fd/
  }

This function's output look good (on my Debian's bash 5.2.15(1)-release):

  $ coTest 
  [1] 1606
  bash: warning: execute_coproc: coproc [1606:WC] still exists
  [2] 1607
  [1]-  Done                    coproc WC { exec wc; }
  1 3 13
  [2]+  Done                    coproc CAT { exec cat; }
  total 0
  lrwx------ 1 user user 64 Apr 17 11:12 0 -> /dev/pts/3
  lrwx------ 1 user user 64 Apr 17 11:12 1 -> /dev/pts/3
  lrwx------ 1 user user 64 Apr 17 11:12 2 -> /dev/pts/3
  lr-x------ 1 user user 64 Apr 17 11:12 3 -> /proc/1608/fd

-- 
 Félix Hauri  -  <felix@f-hauri.ch>  -  http://www.f-hauri.ch



reply via email to

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