http://www.joinc.co.kr/modules/moniwiki/wiki.php/Site/Code/C/CheckProcess
   +------+
   | 시작 |
   +------+
      | 
   +------------+
   | Pid        |
   | 파일 체크  |
   +------------+
      |
   +-----------------------+   Yes
   | Pid 파일이 존재하는가 | ---------> 종료
   +-----------------------+
      | NO
      |
   +------------------------+  Yes
   | 해당 PID의 /proc파일이 | --------> 종료
   | 존재하는가             |
   +------------------------+
      | NO
      |
   +------------------------+
   | 자신의 PID로           |
   | Pid 파일을 덮어씀      |
   +------------------------+
      |
      |
      |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int CheckProcess(char *pfile)
{
	FILE *fp;
	char pid[16];
	char process[256];
	int rtv = 0;
	fp = fopen(pfile, "r");
	if (fp == NULL)
	{
		return 0;
	}
	if (fgets(pid, sizeof(pid) - 1, fp) == NULL) 
	{
		fclose(fp);
		return 0;
	}
	fclose(fp);
	pid[strlen(pid)-1] = 0x00;
	sprintf(process, "/proc/%s/exe", pid);
	if (access(process, F_OK) == 0)
	{
		return atoi(pid);
	}
	return 0;
}
int WritePid(char *pfile)
{
	FILE *fp;
	char pid[16];
	sprintf(pid, "%d\n", getpid());
	fp = fopen(pfile,"w");
	fputs(pid, fp);
	fclose(fp);
}
int main(int argc, char **argv)
{
	int pid;
	if ((pid = CheckProcess("/tmp/test.pid")) == 0)
	{
		printf("Damon exe\n");
		WritePid("/tmp/test.pid");
		while(1)
		{
			sleep(1);
		}
	}
	else
	{
		printf("Damon is Exist %d\n", pid);
	}
}


