zoukankan      html  css  js  c++  java
  • 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;
    }

    运行结果:

  • 相关阅读:
    [NS]运行行两年了,碰到一个没遇见的问题!
    [C++][MFC]关于菜单的一些操作
    [C++][MFC]CFile的一些简单使用
    [CSharp]HTML中的模式窗口
    [C++]堆栈与堆的概念
    [RS]关于ReportingServices的开发
    [JS]在程序中使用IE的模式对话框!
    [WWF][STUDY]向Workflow传入参数
    [学习]极限编程与敏捷开发
    [C++]什么是纯虚函数
  • 原文地址:https://www.cnblogs.com/innobase/p/5319975.html
Copyright © 2011-2022 走看看