When using setpgrp vi (and other tty programs) work completely different than if setpgrp is not used. Example:

perl -MIPC::Open3 -e '$pid= open3("<&STDIN", ">&STDOUT", ">&STDERR", qw(perl -e),q(exec qw(bash -c),qq(vi foo))); wait'

That works great and calls vi foo. But add setpgrp:

perl -MIPC::Open3 -e '$pid= open3("<&STDIN", ">&STDOUT", ">&STDERR", qw(perl -e),q(setpgrp;exec qw(bash -c),qq(vi foo))); wait'

and then it does not work so well.

Tested on GNU/Linux (Mint), FreeBSD, OpenBSD, Solaris, HPUX, AIX, Dragonfly. All give similar behaviour.

Why? Can I somehow create a process group and still spawn tty tools like vi?


The above is part of a possible extension of GNU Parallel that will allow to kill the process groups instead of processes, and is thus a tiny corner of the full program. An answer to just run vi foo is thus not a useful answer.

  • 1
    Making a process a session leader detaches it from the current terminal, yes. I strongly recommend you R. Stevens' book Advanced Programming in the UNIX® Environment. – lcd047 May 12 '15 at 19:38
  • Neat project, but I think pkill -g already covers this. – Otheus May 12 '15 at 22:44
  • @lcd047 I can't be sure, it's been 20+ years, but the edition of Stevens' that I read didn't cover process groups because they had yet to be invented at the time of his writing. – Otheus May 12 '15 at 22:46
  • 1
    @Otheus: Ah, that brings back memories. :) I have the first edition here, from 1993, and section 9.4 (p. 243) is named "Process Groups". Looking at the TOC, almost everything there is still relevant today, the only exceptions being the chapter about modems, and to some extent the references to STREAMS. Which I suppose pretty much reflects the state of UNIX today. – lcd047 May 13 '15 at 4:09
  • Awesome. Thanks for invalidating my memory :) – Otheus May 13 '15 at 6:56

From setpgrp man page from Darwin/MacOS (BSD-based):

If the calling process is not already a session leader, setpgrp() sets the process group ID of the calling process to that of the calling process. Any new session that this creates will have no controlling terminal.

There's your answer.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.