尝试写一个简单的守护进程

/** @File daemon.c
*
* Build a daemon process for game
*
*/ #include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "daemon.h" int create_daemon()
{ pid_t pid;
pid=fork(); switch(pid)
{
case -1:
//fprintf(stderr, "fork child failed!\n");
exit(EXIT_FAILURE);
break;
case 0:
//fprintf(stdout,"child is here!\n");
for(;;)
{
sleep(3);
}
break;
default:
//fprintf(stdout,"child pid is [%d]\n", pid);
break;
} return 0;
}

  创建线程

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h> pthread_t create_thread( void* pFunc)
{ pthread_t tid;
if(pthread_create(&tid, NULL, (void*)pFunc, NULL) == 0)
{
fprintf(stdout, "create thread success!\n");
}else
{
fprintf(stderr, "create thread failed!\n");
exit(EXIT_FAILURE);
} return tid;
}

  

05-10 23:41