grisu48

daemon.c

Dec 14th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.61 KB | None | 0 0
  1. /* This is my template to fork a deamon process in *nix */
  2.  
  3. static void fork_daemon(void) {
  4.     pid_t pid = fork();
  5.     if(pid < 0) {
  6.         fprintf(stderr, "Fork daemon failed\n");
  7.         exit(EXIT_FAILURE);
  8.     } else if(pid > 0) {
  9.         // Success. The parent leaves here
  10.         exit(EXIT_SUCCESS);
  11.     }
  12.    
  13.     /* Fork off for the second time */
  14.     /* This is needed to detach the deamon from a terminal */
  15.     pid = fork();
  16.     if(pid < 0) {
  17.         fprintf(stderr, "Fork daemon failed (step two)\n");
  18.         exit(EXIT_FAILURE);
  19.     } else if(pid > 0) {
  20.         // Success. The parent again leaves here
  21.         exit(EXIT_SUCCESS);
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment