setsid(); for (i=getdtablesize();i>=0;--i) close(i); i=open("/dev/null",O_RDWR);  dup(i);  dup(i);  umask(027);
deamonize (){  int pidfile;  char str[10];    switch (fork ())    {    case 0:      if ((pidfile=open(PID_FILE,O_RDWR|O_CREAT,0640))<0) {      	perror("Failed to create pid file");	exit(-1);      }      if (lockf(pidfile,F_TLOCK,0)<0) {	      perror("Failed to lock pid file");	      exit(-1); /* can not lock */      }      sprintf(str,"%d\n",getpid());      write(pidfile,str,strlen(str)); /* record pid to lockfile */            loop ();      break;    case -1:      perror ("Failed to deamonize");      exit (-1);    default:      break;    }}