1

I have this code to make a Daemon in C:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

void main() {
  FILE *fp= NULL;
  pid_t process_id = 0;
  pid_t sid = 0;
  // Create child process
  process_id = fork();
  // Indication of fork() failure
  if (process_id < 0)
  {
    printf("fork failed!\n");
    // Return failure in exit status
    exit(1);
  }
  // PARENT PROCESS. Need to kill it.
  if (process_id > 0)
  {
    printf("process_id of child process %d \n", process_id);
    // return success in exit status
    exit(0);
  }
  //unmask the file mode
  umask(0);
  //set new session
  sid = setsid();
  if(sid < 0)
  {
    // Return failure
    exit(1);
  }
  // Change the current working directory to root.
  chdir("/");
  // Close stdin. stdout and stderr
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);

  while (1)
  {
    // Do your thing
    usleep(2000);
  }
}

I can compile and run this as ./exampleOne and this will run as a daemon in the background forever.

Now reversely, what if I had the following example:

#include <stdio.h>
#include <stdlib.h

void main() {
  while (1)
  {

    // do your thing
    usleep(2000);
  }
}

And then ran it as ./exampleTwo &. This will also now run in the background as an infinite loop.

So what's the difference? The second one is so much simpler.

Not a real meerkat
  • 5,604
  • 1
  • 24
  • 55
Kousha
  • 32,871
  • 51
  • 172
  • 296
  • Why don't you create some script and run it in cron? :) – W.F. Apr 07 '15 at 17:23
  • Just a related thing: You should install a signal handler or an event to have a chance to terminate the demon process normally. _"So what's the difference?"_ The 1st example takes your demon process to the background automatically. – πάντα ῥεῖ Apr 07 '15 at 17:23
  • the difference is that exampleTwo will only respond to any input or events every 2 seconds. Will that be acceptable to you? – Richard Hodges Apr 07 '15 at 17:24
  • @WojciechFrohmberg, because I need the response to be faster than a 1minute duty cycle of a cron! – Kousha Apr 07 '15 at 17:24
  • @RichardHodges, I've put a 2s delay on the first example too. What's inside the `while` loop is not important. My question is more the conceptual difference between the two – Kousha Apr 07 '15 at 17:25
  • 1
    @WojciechFrohmberg It should rather be specified in the `/etc/inittab`. – πάντα ῥεῖ Apr 07 '15 at 17:25
  • @Kousha sorry I didn't see that last few lines of code. The purpose of daemonising a process is to detach it from the console from which it was started. This then means that the process won't receive a SIGHUP when the console is closed. If you don't do this your process will stop when you log out. – Richard Hodges Apr 07 '15 at 17:27

1 Answers1

3

One simple advantage of the fork/daemon approach is that the programmer decides where the fork will occur which allows the programmer to to provide a guarantee that when one returns to the shell with no errors, the daemon is up and running.

Running in the background returns to the shell immediately so you daemon may still be in the initialization phase and connection to it may well fail for a while.

doron
  • 27,972
  • 12
  • 65
  • 103