Posts tagged ‘background’

Creating a daemon in C

#include 
#include 
#include 

/* based on code lighttpd:server.c source file */

void daemonize(void) {
        signal(SIGTTOU, SIG_IGN);
        signal(SIGTTIN, SIG_IGN);
        signal(SIGTSTP, SIG_IGN);
        if (0 != fork()) exit(0);

        if (-1 == setsid()) exit(0);

        signal(SIGHUP, SIG_IGN);

        if (0 != fork()) exit(0);

        if (0 != chdir("/")) exit(0);
}

int main(){

	int i;
        printf("Perhaps you want do something before go to background\n");
	scanf("%d", &i);

	daemonize();

	for (;;){
		sleep(10);
	}

}