C语言守护进程


#include <stdlib.h>

void daemon_init(){
    int pid, i;

    if(pid = fork())
        exit(0);
    else if(pid < 0)
        exit(1);
    else
        setsid();

    if(pid = fork())
        exit(0);
    else if(pid < 0)
        exit(1);
    else
        chdir("/tmp");
        umask(0);

    for(i = 0; i < 64; i++){
        close(i);
    }

}

main(){
    daemon_init();
}