/* * ISA 563, Spring 2011 * Copyleft by Muhammad Abdulla */ #include #include #include #include #include /* * simple_daemon.c -- demonstrates basic steps to daemonize a program */ int daemonize() { pid_t pid; if ( (pid = fork()) < 0 ) { // fork error return -1; } else if ( pid != 0 ) { // parent should exit exit(0); } // child continues from here on // become a session leader setsid(); // change working directory to / chdir("/"); // clear out file creation masks umask(0); return 0; } int main(int argc, char *argv[]) { daemonize(); // do some hard work in the background while(1) { sleep(1); } }