尝试写一个简单的守护进程
/** @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! "); exit(EXIT_FAILURE); break; case 0: //fprintf(stdout,"child is here! "); for(;;) { sleep(3); } break; default: //fprintf(stdout,"child pid is [%d] ", 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! "); }else { fprintf(stderr, "create thread failed! "); exit(EXIT_FAILURE); } return tid; }