Skip to content

Instantly share code, notes, and snippets.

@miyucy
Created October 14, 2009 09:46
Show Gist options
  • Save miyucy/209924 to your computer and use it in GitHub Desktop.
Save miyucy/209924 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
void write_pid_file(const char* pid_file)
{
// getcwd
FILE* fp = fopen(pid_file, "w");
if(fp)
{
fprintf(fp, "file(%d):%d\n", __LINE__, (int)getpid());
fclose(fp);
}
}
int main(int argc, char *argv[])
{
char pid_file[PATH_MAX];
strncat(getcwd(pid_file, PATH_MAX), "/pid", strlen("/pid"));
fprintf(stdout, "stdout(%d):%d\n", __LINE__, (int)getpid());
fprintf(stderr, "stderr(%d):%d\n", __LINE__, (int)getpid());
daemon(0, 0);
fprintf(stdout, "stdout(%d):%d\n", __LINE__, (int)getpid());
fprintf(stderr, "stderr(%d):%d\n", __LINE__, (int)getpid());
write_pid_file(pid_file);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment