您的位置 首页 golang

linux c编程之创建、查看、杀死僵尸进程方法详解

僵尸进程(Zombie process)通俗来说指那些虽然已经终止的进程,但仍然保留一些信息,等待其父进程为其收尸。也就是说父进程没有结束,但是子进程结束了,父进程没死,没办法给子进程收尸,真的是只有父进程死了才能收尸,哈哈,同时也没有显示的调用wait/waitpid给其子进程收尸。当然,父进程提前死亡,子进程会交给init进程,所以收尸的问题就交给init进程了。

一,在linux下生成一个僵尸进程的c代码

 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <linux/wait.h>
int main(int argc,char **argv)
{
        int i=0;
        pid_t pid=fork();
        if(pid==-1) return 0;
        else if(pid==0)
        {
                printf("son pid is %dn",getpid());
                while(1)
                {
                        printf("son---i=%dn",i);
                        i++;
                        sleep(1);
                        if(i==5)
                        break;
                }
                printf("son is over!n");
        }else if(pid>0)
        {
                printf("parent pid is %dn",getpid());
                while(1) sleep(100);
        }
        return 0;
}  

这样就是在子进程已经结束,但是父进程永远也不会结束,也没有调用wait/waitpid。那么子进程就成为僵尸进程。

编译,gcc makeDefunct.c -o makeDefunct

我的文件名是makeDefunct.c 运行./makeDefunct

需要C/C++ Linux服务器架构师学习资料私信“资料”(资料包括C/C++,Linux,golang技术, Nginx ,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK,ffmpeg等),免费分享

linux c编程之创建、查看、杀死僵尸进程方法详解

二、如何查看僵尸进程

可以利用top命令,效果如下,上文中创建的僵尸进程

linux c编程之创建、查看、杀死僵尸进程方法详解

可以发现我的服务器下只有一个僵尸进程。

也可以用 ps -aux |grep Z 来查看僵尸进程,效果如下:

linux c编程之创建、查看、杀死僵尸进程方法详解

僵尸进程的状态显示的是Z

三、如何杀死僵尸进程

从僵尸进程的概念可以看出,父进程没有结束导致的,我们把父进程杀死,父进程就会寻找他自己创建的子进程,从而杀死僵尸进程。所以我们要杀死父进程就可以。所以第一步要找到父进程 ps -ef | grep defunct_process_pid(僵尸进程pid) 然后执行,kill -s 9 父进程的pid。演示如下:

linux c编程之创建、查看、杀死僵尸进程方法详解

可以看出,先找到僵尸进程的pid,我这里使用ps -aux |grep make 是因为我的c文件是make开头的。然后在找到僵尸进程的父进程的pid 11781

然后杀死 kill -s 9 11781

linux c编程之创建、查看、杀死僵尸进程方法详解

检查一下是否杀死了僵尸进程

执行top后得到的结果如下图:

linux c编程之创建、查看、杀死僵尸进程方法详解

没有僵尸进程了已经被杀死了。

文章来源:智云一二三科技

文章标题:linux c编程之创建、查看、杀死僵尸进程方法详解

文章地址:https://www.zhihuclub.com/97173.shtml

关于作者: 智云科技

热门文章

评论已关闭

36条评论

  1. Check to see if your company is a NOD Leadership Council member and learn more about joining You can buy tadalafil right here

  2. tadalafil increases effects of silodosin by pharmacodynamic synergism How does nicotine affect sex drive

  3. Reliable safety data gathered in the 10 years since these PDE inhibitors were launched confirm that side effects such as Priapism prolonged erection , red eyes, painful eyes, syncope fainting , tachycardia rapid heart rate , and nausea do occur, but are rare Lifelong PE, 0

  4. The medication remains effective in the body even after all tablets are consumed since the half-life is very long. Goldman-Cecil Medicine.

  5. We observed a relation between testosterone and risk of incident AF that was not statistically significant HR 1.

  6. In the second case, first, you will need to download the APK file and install it Do I want pizza

  7. Hepatoprotective mechanisms include immunomodulation and inhibition of proinflammatory cytokines and chemokines 66, decreased miR 31 expression, and subsequent T cell apoptosis 32

  8. A more recent study investigated the effect of CKD and HD on nebivolol pharmacokinetics Treatment for these tumor deposits by cytostatic drugs through their own blood supply may be more effective than by superficial exposure to drugs dissolved in the CSF 13

  9. We worked with the USPSTF and Agency for Healthcare Research and Quality AHRQ to develop the scope and key questions for this research The two sample t test was used to compare the mean of a continuous variable between two samples

  10. sertraline, phentermine Estrogen receptor positive MCF 7 cells were incubated with 4OH tamoxifen 10 nM and gene expression analyzed by array hybridization during 12 weeks

  11. said Huang Kaidi, a shopkeeper in China s southern city of Shenzhen, who has been selling iPhones smuggled from Hong Kong since the phone debuted in 2007

  12. ESR1 mutations are largely acquired with hormonal treatment; it s very uncommon to see them in patients who are hormone therapy naГЇve

  13. The chemical shift in Оґ ppm was assigned with a reference to the signal from the residual protons in the deuterated solvent and TMS was used as an internal standard Young, USA 2022 04 29 16 13 28

  14. Heart problems, high blood pressure, low blood pressure Since devil s claw can affect heart rate, heartbeat, and blood pressure, it might harm people with disorders of the heart and circulatory system

  15. Both contributed to data collection and analysis for studies in this manuscript while participating in the ORISE Research Participation Programs

  16. Smith Magenis a rare disorder with sleep and developmental impact The analysis of surgical findings showed an incidence of 5

  17. The group said seven people were killed nationwide; a government health official said only six died

  18. Improvement in diminished ovarian reserve after dehydroepiandrosterone supplementation Isabelle, USA 2022 06 19 18 37 56

  19. The woman in the white dress saw through Gu Min s thoughts, lightly opened her red lips, and said male enhancement pills kuwait calmly, There are too many secrets in this world, whether it is here with you or with us

  20. One patient who had previously had radiotherapy underwent triple coronary artery bypass graft surgery and a tricuspid annuloplasty, but the patient s condition deteriorated intraoperatively and pericardiectomy was not performed

  21. Occurs most commonly in small breed dogs particularly in the Chihuahua, Lhasa Apso, Maltese, Pomeranian, Pug, Shih Tzu, Toy Poodle and Yorkshire Terrier It could also be useful to use artificial tears and lubricating antiseptic gels

  22. cialis can we take tentex forte and confido together Net profit at Chrysler rose to 507 million in the secondquarter from 436 million a year before

  23. Operations on people with undiagnosed pheochromocytomas can be fatal This process also greatly affects the sex steroid synthesis pathways

  24. Phase I and phase II clinical trials in sarcoma Implications for drug discovery and development

  25. In that way, the impact of each drugs on proliferation, but also their effect on apoptosis 17 along with the putative specific predictive role of PAX 2 for tamoxifen may be appropriately addressed

  26. Both of these are reasonable options and how the woman chooses to proceed should be based on an educated consideration of the pros and cons of each 2012 Feb; 36 1 e33 9

网站地图