守护进程

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
#include <signal.h>

#define MAXFILE 65535
#define KEY_PROJ_ID   'C'
#define SERVER_ID 1

int _running;
pid_t a[10];
int ret_end;
int pid_end;

void sigterm_handler(int arg)
{
    _running = 0;
}

void sigchld_handler(int signo)
{
    int i;
    for(i = 0; i < 10; i++)
    {
        pid_end = waitpid(a[i], &status, WNOHANG);
    }
    if(WIFEXITED(status))
    {
        ret_end = WEXITSTATUS(status);
    }
    printf("\n\n\n\nprocess %d decode status is %d\n\n\n\n",pid_end,ret_end);
}

void daemonize(char *name)
{
    pid_t pid, sid; 
    FILE *pidfile;

    /* already a daemon */
    if ( getppid() == 1 ) return;

    /* Fork off the parent process */
    pid = fork();
    if (pid < 0) { 
        exit(EXIT_FAILURE);
    }    
    /* If we got a good PID, then we can exit the parent process. */
    if (pid > 0) { 
        exit(EXIT_SUCCESS);
    }    

    pidfile = fopen(name, "w");
    if (pidfile) {
        fprintf(pidfile, "%d\n", getpid());
        fclose(pidfile);
    }    


    /* At this point we are executing as the child process */

    /* Change the file mode mask */
    umask(0);

    /* Create a new SID for the child process */
    sid = setsid();
    if (sid < 0) { 
        exit(EXIT_FAILURE);
    }    
    /* Change the current working directory.  This prevents the current
       directory from being locked; hence not being able to remove it. */
    if ((chdir("/")) < 0) { 
        exit(EXIT_FAILURE);
    }    

    /* Redirect standard files to /dev/null */
    freopen( "/dev/null", "r", stdin);
    freopen( "/dev/null", "w", stdout);
    freopen( "/dev/null", "w", stderr);
}

int fun(char *filename)
{
    if(strcmp(filename,"qwe") == 1)
        return 0;
    else
        return 1;
}

int main(int argc, char **argv)
{
    pid_t pid;
    key_t key;
    int msgid;
    int my_id;
    int count;
    int ret;

    //制作成守护进程
    daemonize("./pid_name");

    //挂起终止信号处理函数,控制台命令kill触发
    signal(SIGTERM, sigterm_handler);
    //挂起结束信号处理函数
    signal(SIGCHLD, sigchld_handler);
    
    //创建消息队列编号,当前路径,字符C
    key = ftok(".", KEY_PROJ_ID);
    //根据消息队列编号创建消息队列
    msgid = msgget(key, IPC_CREAT | 0666);
    
    //主业务程序和本程序使用这个消息队列交互
    while(_running)
    {    
        ret = msgrcv(msgid, &rcv_buf, sizeof(rcv_buf), SERVER_ID, 0);

        if(count < 10)
        {
            count++;
            pid = fork();
            
            if(pid < 0)
            {
                printf("fork err\n");
            }
            
            if(pid > 0)
            {
                count++;
                a[count] = pid;
            }
            
            //在子进程中向服务程序通过消息队列发消息
            if(pid == 0 )
            {
                my_id = count;
                snd_buf.dst = SERVER_ID;
                snd_buf.srx = my_id;
                snd_buf.data = "FORK SUCCESS";
                msgsnd(msgid, &snd_buf, sizeof(snd_buf) + strlen(), 0);
                ret = fax_decode(rcv_buf.data);
                if(ret == 0)
                    exit(0);
                if(ret == 1)
                    exit(7);
            }
        }
        else
        {
            snd_buf.dst = SERVER_ID;
            snd_buf.srx = my_id;
            snd_buf.data = "NO FREE PROCESS";
            msgsnd(msgid, &snd_buf, sizeof(snd_buf) + strlen(snd_buf.data), 0);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值