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

前端 未结 20 1731
难免孤独
难免孤独 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条回答
  •  萌比男神i
    2020-12-09 19:04

    If you need a script to have the PID of the program, you can still get it after a fork.

    When you fork, save the PID of the child in the parent process. When you exit the parent process, either output the PID to STD{OUT,ERR} or simply have a return pid; statement at the end of main(). A calling script can then get the pid of the program, although it requires a certain knowledge of how the program works.

提交回复
热议问题