zoukankan      html  css  js  c++  java
  • 这个可以程序主要测试高级并发服务器程序怎样写会避免僵尸进程?

    #include <unistd.h>  
    #include <stdio.h>  
    #include<stdlib.h>
    #include<signal.h>
    int main(void)  
    {  //signal(SIGCHLD, SIG_IGN);
       int i=0;  
       printf("i son/pa ppid pid  fpid
    ");  
       //ppid指当前进程的父进程pid  
       //pid指当前进程的pid,  
       //fpid指fork返回给当前进程的值  
       while(1){  
       sleep(1);                           //之所以加入这个时间是因为,容易看出程序的变化,不然根本看不出变化,连鼠标都懂不了!
           pid_t fpid=fork();  
           if(fpid==0)  
               {printf("%d child  %4d %4d %4d
    ",i,getppid(),getpid(),fpid);
                //return 0; //这两个效果一样的!
                exit(0);
                } 
             }  
    return 0;  
    }  
    [root@linux Desktop]# gcc b.c
    [root@linux Desktop]# ./a.out
    i son/pa ppid pid  fpid
    0 child  16980 16981    0
    0 child  16980 16982    0
    0 child  16980 16983    0
    0 child  16980 16985    0
    0 child  16980 16988    0
    0 child  16980 16990    0
    0 child  16980 16991    0
    0 child  16980 16993    0
    0 child  16980 16995    0
    0 child  16980 16996    0
    ^C
    [root@linux Desktop]# 
    
    [root@linux Desktop]# ps aux | grep -w 'Z'               //产生了好多僵尸进程了
    root     16981  0.0  0.0      0     0 pts/2    Z+   20:38   0:00 [a.out] <defunct>defunct某些系统中用它来作为僵尸的标志
    root     16982  0.0  0.0      0     0 pts/2    Z+   20:38   0:00 [a.out] <defunct>
    root     16983  0.0  0.0      0     0 pts/2    Z+   20:38   0:00 [a.out] <defunct>
    root     16985  0.0  0.0      0     0 pts/2    Z+   20:38   0:00 [a.out] <defunct>
    root     16987  0.0  0.0   4340   804 pts/3    S+   20:38   0:00 grep -w Z
    [root@linux Desktop]# 
    
    当把signal(SIGCHLD, SIG_IGN);这行代码加上时输出结果如下:
    [root@linux Desktop]# gcc b.c
    [root@linux Desktop]# ./a.out
    i son/pa ppid pid  fpid
    0 child  17135 17136    0
    0 child  17135 17137    0
    0 child  17135 17139    0
    0 child  17135 17140    0
    0 child  17135 17141    0
    0 child  17135 17142    0
    0 child  17135 17145    0
    0 child  17135 17147    0
    0 child  17135 17150    0
    0 child  17135 17151    0
    0 child  17135 17152    0
    ^C
    [root@linux Desktop]#                     //没有僵尸进程产生
    [root@linux Desktop]# ps aux | grep -w 'Z'
    root     17144  0.0  0.0   4336   796 pts/3    S+   20:42   0:00 grep -w Z
    [root@linux Desktop]# ps aux | grep -w 'Z'
    root     17149  0.0  0.0   4336   792 pts/3    S+   20:42   0:00 grep -w Z
    [root@linux Desktop]# 
    <defunct>
  • 相关阅读:
    第一次练习总结
    第一次上机总结
    写在程序组干活之前
    虚拟机Centos7安装Mysql
    第一章 开发体验
    如何优雅的移植JavaScript组件到Blazor
    Asp.net core中RedisMQ的简单应用
    docker容器安装mysql
    Centos 8安装Docker
    c# 定时启动一个操作、任务(版本2)
  • 原文地址:https://www.cnblogs.com/leijiangtao/p/4078506.html
Copyright © 2011-2022 走看看