Created
January 24, 2012 03:34
-
-
Save h2onda/1667625 to your computer and use it in GitHub Desktop.
daemon program sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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