zoukankan      html  css  js  c++  java
  • GNU DAEMON THREAD <1>

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

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

      

  • 相关阅读:
    01、MySQL_简介
    算法—打擂台法
    第10章 对文件的输入输出
    第9章 用户自己建立数据类型
    Spring Cloud
    JUC
    Swagger Learing
    JUC
    Spring Data
    SpringCloud
  • 原文地址:https://www.cnblogs.com/unixshell/p/3339033.html
Copyright © 2011-2022 走看看