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

/** @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;
}

  

相关文章:

  • 2021-10-29
  • 2021-12-27
  • 2021-12-14
  • 2022-12-23
  • 2021-06-22
  • 2021-11-04
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-31
  • 2021-08-11
  • 2021-12-17
  • 2021-11-06
  • 2022-01-03
  • 2022-02-10
相关资源
相似解决方案