How can a C/C++ program put itself into background?

前端 未结 20 1762
难免孤独
难免孤独 2020-12-09 18:16

What\'s the best way for a running C or C++ program that\'s been launched from the command line to put itself into the background, equivalent to if the user had launched fro

20条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 18:55

    A process cannot put itself into the background, because it isn't the one in charge of background vs. foreground. That would be the shell, which is waiting for process exit. If you launch a process with an ampersand "&" at the end, then the shell does not wait for process exit.

    But the only way the process can escape the shell is to fork off another child and then let its original self exit back to the waiting shell.

    From the shell, you can background a process with Control-Z, then type "bg".

提交回复
热议问题