• Linux的僵尸进程处理2


    linux下的僵尸进程处理例子,同时也演示了管道通信的弊端

    /*************************************************************************
            > File Name: ft.c
            > Author: zhoulin
            > Mail: 715169549@qq.com
            > Created Time: Fri Mar 25 15:21:00 2016
     ************************************************************************/
    
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <signal.h>
    void deal(int sig){
        fprintf(stdout,"        *******cur %d exit*******
    ",getpid()); //Ctrl+C的发送信号的处理(SIGINT信号)
        exit(1);
    }
    int main(void)
    {
        pid_t f;
        int fd[2]; //管道通信,ffd[0]用于读,fd[1]用于写
        if(pipe(fd) <0){
            perror("pipe");
            return -1;
        }
        f= fork();
        if(f < 0){
            perror("fork");
            return -1;
        }
        if(f > 0){
            char buf[32] = {''};
            signal(SIGINT,&deal); //信号注册
            while(1)
            {
                memset(buf,'',32);
                fprintf(stdout,"        ******parent %d********
    ",getpid());
                system("ps -ef |grep -v 'grep'|grep -v 'ksoftirqd'|grep 'ft'");
                sleep(1);
                close(fd[1]);
                if(read(fd[0],buf,32) < 0) //接受子进程过来的信息
                {
                    perror("read");
                    return -1;
                }
                if(strlen(buf) > 0){
                    fprintf(stdout,"    ----------------%s-------------
    ",buf);
                    if(strncmp(buf,"active",6) == 0){
                        fprintf(stdout,"        *****child is running*****
    ");
                    }
                    if(strncmp(buf+6,"die",3) == 0) {
                        fprintf(stdout,"        *****child is die*****
    ");
                        system("ps -ef |grep -v 'grep'|grep -v 'ksoftirqd'|grep 'ft'");
                        int status;
                        fprintf(stdout,"    ****recyle child %d******
    ",wait(&status));
                    }
                }
            }
        }else {
            int i = 0;
            while(1){
                close(fd[0]);
                fprintf(stdout,"        ******child %d********
    ",getpid());
                system("ps -ef |grep -v 'grep'|grep -v 'ksoftirqd'|grep 'ft'");
                sleep(1);
                i++;
                if(i == 3){
                    fprintf(stdout,"        #####child exit#####
    ");
                    write(fd[1],"die",3); //发送消息给父进程,要推出啦
                    exit(0);
                }
                if(write(fd[1],"active",6) < 0){
                    perror("write");
                    return -1;
                }
            }
        }
        return 0;
    }

    运行结果:

  • 相关阅读:
    计蒜客 动态规划基础 蒜头跳木桩
    委托的使用和合并
    asp.net "callback" 和 "postback" 的区别.
    3 顶层父类
    2 异常类
    1 智能指针
    16 #error 和 #line
    15 条件编译
    14 宏
    13 编译和链接
  • 原文地址:https://www.cnblogs.com/innobase/p/5319975.html
Copyright © 2020-2023  润新知