监护程序(进程所属组号)

#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


static    void    sig_child(int signo)
{
    pid_t    pid;
    int        stat;

    while((pid=waitpid(-1,&stat,WNOHANG))>0);
   
    return;
}

int main()
{
    signal(SIGCHLD,sig_child);
   

    int npid = -1;
    npid = fork();   
    if(npid == 0){    //child
        clearfile();
    }else{        //parent
        while(1){
            sleep(3);
            if(getpgid(npid) < 0){
                kill(npid,9);
                npid = fork();
                if(npid == 0){
                   
                    clearfile();
                }
            }
        }
    }
}

void clearfile()
{
    struct stat statbuf;
        char szpath[] ="/home/jzb/source/myprint.log";
    int nret;
    int fd ;
        while(1){
                nret = stat(szpath,&statbuf);

                if(statbuf.st_size >= 1024*1024*1024){
                        fd = open(szpath,O_CREAT|O_TRUNC);
                        close(fd);
                }

                sleep(1);
        }
}

 

 

主程序通过while循环不断的检测进程所属是否有效(通常情况下,当进程终止时,所属的进程组号为-1,为此,判断进程是否在运行)。如果检测到子进程结束,则将重启开启子进程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值