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

前端 未结 20 1711
难免孤独
难免孤独 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 19:06

    The way it's typically done on Unix-like OSes is to fork() at the beginning and exit from the parent. This won't work on Windows, but is much more elegant than launching another process where forking exists.

    0 讨论(0)
  • 2020-12-09 19:09
    /**Deamonize*/
    
    pid_t pid;
    pid = fork(); /**father makes a little deamon(son)*/
    if(pid>0)
    exit(0); /**father dies*/
    while(1){
    printf("Hello I'm your little deamon %d\n",pid); /**The child deamon goes on*/
    sleep(1)
    }
    
    /** try 'nohup' in linux(usage: nohup <command> &) */
    
    0 讨论(0)
提交回复
热议问题