Why does GNU script use two forks instead of select and one fork?

I just realized that the GNU linux “script” binary uses two forks instead of one. It can simply use select instead of doing the first fork (). Why use two forks?

Is it simply because the choice did not exist at the time it was encoded, and someone did not have the motivation to transcode it, or is there a good reason?

man 1 script: http://linux.die.net/man/1/script

script source: http://pastebin.com/raw.php?i=br8QXRUT

+4
source share
1 answer

The key is in the code to which I added a few comments.

child = fork();
sigprocmask(SIG_SETMASK, &unblock_mask, NULL);

if (child < 0) {
    warn(_("fork failed"));
    fail();
}
if (child == 0) {
            /* child of first fork */
    sigprocmask(SIG_SETMASK, &block_mask, NULL);
    subchild = child = fork();
    sigprocmask(SIG_SETMASK, &unblock_mask, NULL);

    if (child < 0) {
        warn(_("fork failed"));
        fail();
    }
    if (child) {
                    /* child of second fork runs 'dooutput' */
        if (!timingfd)
            timingfd = fdopen(STDERR_FILENO, "w");
        dooutput(timingfd);
    } else
                    /* parent of second fork runs 'doshell' */
        doshell();
} else {
    sa.sa_handler = resize;
    sigaction(SIGWINCH, &sa, NULL);
}
    /* parent of first fork runs doinput */
doinput();

, :

  • dooutput()
  • doshell()
  • doinput()

, , , select(). select() UNIX, , select() . . doshell() , exec fds. , , . dooutput() doinput() select() , -, select .. fork() ( UNIX CoW), , select(), fork() ? IE " ?"

+3

Source: https://habr.com/ru/post/1530288/


All Articles