1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

daemonize

1
Last updated at Posted at 2018-07-18

やりかた

  • fork
    親プロセスは終了

  • setsid で端末を切り離す

  • カレントディレクトリを / にする。

  • stdin, stdout, stderrをクローズ

簡易版

void daemonize(){
   switch (fork()) {
    case -1:
        return (-1);
    case 0:
        break;
    default:
        exit(EXIT_SUCCESS);
    }

    if (setsid() == -1)
        return (-1);

    if (nochdir == 0) {
        if(chdir("/") != 0) {
            return (-1);
        }
    }
  // reset mask
  umask(0);
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?