C - create Process Daemon


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #include <stdio.h>
  2. #include <unistd.h>
  3.  
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9. /*
  10. * Funzione che mi crea un demone
  11. */
  12.  
  13. int pid;
  14.  
  15. // create - fork 1
  16. if(fork()) return 0;
  17.  
  18. // it separates the son from the father
  19. chdir("/");
  20. setsid();
  21. umask(0);
  22.  
  23. // create - fork 2
  24. pid = fork();
  25.  
  26. if(pid)
  27. {
  28. printf("Daemon: %d\n", pid);
  29. return 0;
  30. }
  31.  
  32. /****** Codice da eseguire ********/
  33. FILE *f;
  34.  
  35. f=fopen("/tmp/coa.log", "w");
  36.  
  37. while(1)
  38. {
  39. fprintf(f, "ciao\n");
  40. fflush(f);
  41. sleep(2);
  42. }
  43. /**********************************/
  44. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.