Skip to content

Instantly share code, notes, and snippets.

@h2onda
Created January 24, 2012 03:34
Show Gist options
  • Select an option

  • Save h2onda/1667625 to your computer and use it in GitHub Desktop.

Select an option

Save h2onda/1667625 to your computer and use it in GitHub Desktop.
daemon program sample
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
/* exit session leader */
switch (fork()) {
case -1:
perror("fork() failed.");
_exit(1);
case 0:
break;
default:
/* exit parent process */
_exit(0);
}
/* daemonize */
int nochdir = 1;
int noclose = 1;
if (daemon(!nochdir, !noclose)) {
perror("daemon() failed.");
_exit(1);
}
/* close std{in,out,err} */
fclose(stdin);
fclose(stdout);
fclose(stderr);
/* exec command */
if (system("/bin/sleep 100 < /dev/null")) {
perror("system() failed.");
_exit(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment