Search code examples
unixterminalfork

How to fork process into another virtual terminal


I need a way to fork current process into another virtual terminal. Actually I want to spawn my process' children into separate tmux panes, but knowing how to fork into another console will do, rest should be easy.


Solution

  • Moving single process into another terminal

    First let's see how to snatch a single process into another console. In Linux you can use reptyr for reattaching process to another terminal. It is rather easy.

    Let's imagine this is your main program:

    #!/usr/bin/perl
    
    print "My PID is $$\n"; # print current process PID;
    sleep 10;
    
    while(1){
      print ++$i, "\n";
      sleep 1;
    }
    

    It will print it's own PID and then start counting seconds after a while.

    You should run

    reptyr [PID-number]

    in new terminal (put PID printed by the program in the place of [PID-number]) and you will see that our main program will be moved into that new terminal.

    Quite easy

    Moving child process into another terminal

    If you try to do the trick with a newly forked process, reptyr will fail with following error:

    [-] Process 739989 (test2.pl) shares 739990's process group. Unable to attach.
    (This most commonly means that 739990 has sub-processes).
    Unable to attach to pid 739990: Invalid argument
    

    When you create a fork, it start sharing a group with a parent process (Link to the article explaining process group stuff is at the end of this article). For some reason you reptyr is not able to snatch process that belongs to a group. To use reptyr on that child process you should ungroup it, or more precisely set it to it's own group by calling setpgid($$, $$) that will assign current process to a group with an ID of process ID of current process. This will move child to an own group, and reptyr will be able to "snatch" it.

    #!/usr/bin/perl
    
    use POSIX;
    
    $pid = fork();
    if ($pid)
    {
      # parent process
      print "Child PID is $pid\n";
      sleep 10;
      while (1) {print ".\n"; sleep 1};
    } else
    {
      # child process
      (setpgid($$,$$) != -1)           || die "Can't set own group: $!";
      sleep 10;
      while (1) {print ++$i,"\n"; sleep 1};
    }
    

    Here parent process prints dots, and child process counts seconds. It also prints child PID at the beginning.

    If you do nothing, output of parent and child will be mixed in a terminal you've run it. But if you run

    reptyr [child-PID-number]

    in new terminal (put child PID printed by the program in the place of [child-PID-number]) you will see that output of parent program (dots) will remain in old terminal, and output of a child (numbers) will be moved to a new terminal.

    See Also

    For better understanding of process groups stuff you should read this article: https://blog.nelhage.com/2011/02/changing-ctty/