僵尸进程介绍

  • Z(zombie)-僵尸进程(子进程终止,父进程没有wait子进程)

僵尸进程产生原因

  • 僵尸进程是当子进程比父进程先结束,而父进程又没有回收子进程,释放子进程占用的资源,此时子进程将成为一个僵尸进程。(一个进程结束了,但是他的父进程没有等待(调用wait/ waitpid)他,那么他将变成一个僵尸进程)
  • 僵死进程会以终止状态保持在进程表中,并且会一直在等待父进程读取退出状态代码。
  • 所以,只要子进程退出,父进程还在运行,但父进程没有读取子进程状态,子进程进入Z状态

系统为什么需要僵尸进程这种进程状态

由于父进程创建子进程是异步的,双方不知道各自的运行状态,而父进程有的时候需要知道子进程退出时的一些信息,所以 linux提供了一种机制,通过让子进程退出时向父进程发送 SIGCHRD 信号来告知父进程,子进程已经退出了。同时,父进程通过调用 wait 和 waitpid 来获取子进程的退出信息。

僵尸进程的危害

  • 占用系统资源
  • 造成内存泄漏

如何防止僵尸进程

  • 让僵尸进程变成孤儿进程,由init回收,就是让父亲先死
  • 采用信号SIGCHLD通知处理,并在信号处理程序中调用wait函数
  • 让僵尸进程的父进程来回收,父进程每隔一段时间来查询子进程是否结束并回收,调用wait()或者waitpid(),通知内核释放僵尸进程

参考链接

  • https://blog.csdn.net/didi1663478999/article/details/98886027

代理进程退出,变成僵死进程原因

  • parent创建代理程序拿到代理程序pid和代理端口,ip,没有wait代理进程
  • 代理进程自杀后parent没有处理信号
#mermaid-svg-Behb2A13mpEh7BzH .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-Behb2A13mpEh7BzH .label text{fill:#333}#mermaid-svg-Behb2A13mpEh7BzH .node rect,#mermaid-svg-Behb2A13mpEh7BzH .node circle,#mermaid-svg-Behb2A13mpEh7BzH .node ellipse,#mermaid-svg-Behb2A13mpEh7BzH .node polygon,#mermaid-svg-Behb2A13mpEh7BzH .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-Behb2A13mpEh7BzH .node .label{text-align:center;fill:#333}#mermaid-svg-Behb2A13mpEh7BzH .node.clickable{cursor:pointer}#mermaid-svg-Behb2A13mpEh7BzH .arrowheadPath{fill:#333}#mermaid-svg-Behb2A13mpEh7BzH .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-Behb2A13mpEh7BzH .flowchart-link{stroke:#333;fill:none}#mermaid-svg-Behb2A13mpEh7BzH .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-Behb2A13mpEh7BzH .edgeLabel rect{opacity:0.9}#mermaid-svg-Behb2A13mpEh7BzH .edgeLabel span{color:#333}#mermaid-svg-Behb2A13mpEh7BzH .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-Behb2A13mpEh7BzH .cluster text{fill:#333}#mermaid-svg-Behb2A13mpEh7BzH div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-Behb2A13mpEh7BzH .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-Behb2A13mpEh7BzH text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-Behb2A13mpEh7BzH .actor-line{stroke:grey}#mermaid-svg-Behb2A13mpEh7BzH .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-Behb2A13mpEh7BzH .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-Behb2A13mpEh7BzH #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-Behb2A13mpEh7BzH .sequenceNumber{fill:#fff}#mermaid-svg-Behb2A13mpEh7BzH #sequencenumber{fill:#333}#mermaid-svg-Behb2A13mpEh7BzH #crosshead path{fill:#333;stroke:#333}#mermaid-svg-Behb2A13mpEh7BzH .messageText{fill:#333;stroke:#333}#mermaid-svg-Behb2A13mpEh7BzH .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-Behb2A13mpEh7BzH .labelText,#mermaid-svg-Behb2A13mpEh7BzH .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-Behb2A13mpEh7BzH .loopText,#mermaid-svg-Behb2A13mpEh7BzH .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-Behb2A13mpEh7BzH .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-Behb2A13mpEh7BzH .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-Behb2A13mpEh7BzH .noteText,#mermaid-svg-Behb2A13mpEh7BzH .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-Behb2A13mpEh7BzH .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-Behb2A13mpEh7BzH .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-Behb2A13mpEh7BzH .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-Behb2A13mpEh7BzH .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Behb2A13mpEh7BzH .section{stroke:none;opacity:0.2}#mermaid-svg-Behb2A13mpEh7BzH .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-Behb2A13mpEh7BzH .section2{fill:#fff400}#mermaid-svg-Behb2A13mpEh7BzH .section1,#mermaid-svg-Behb2A13mpEh7BzH .section3{fill:#fff;opacity:0.2}#mermaid-svg-Behb2A13mpEh7BzH .sectionTitle0{fill:#333}#mermaid-svg-Behb2A13mpEh7BzH .sectionTitle1{fill:#333}#mermaid-svg-Behb2A13mpEh7BzH .sectionTitle2{fill:#333}#mermaid-svg-Behb2A13mpEh7BzH .sectionTitle3{fill:#333}#mermaid-svg-Behb2A13mpEh7BzH .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Behb2A13mpEh7BzH .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-Behb2A13mpEh7BzH .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Behb2A13mpEh7BzH .grid path{stroke-width:0}#mermaid-svg-Behb2A13mpEh7BzH .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-Behb2A13mpEh7BzH .task{stroke-width:2}#mermaid-svg-Behb2A13mpEh7BzH .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Behb2A13mpEh7BzH .taskText:not([font-size]){font-size:11px}#mermaid-svg-Behb2A13mpEh7BzH .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Behb2A13mpEh7BzH .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-Behb2A13mpEh7BzH .task.clickable{cursor:pointer}#mermaid-svg-Behb2A13mpEh7BzH .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-Behb2A13mpEh7BzH .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-Behb2A13mpEh7BzH .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-Behb2A13mpEh7BzH .taskText0,#mermaid-svg-Behb2A13mpEh7BzH .taskText1,#mermaid-svg-Behb2A13mpEh7BzH .taskText2,#mermaid-svg-Behb2A13mpEh7BzH .taskText3{fill:#fff}#mermaid-svg-Behb2A13mpEh7BzH .task0,#mermaid-svg-Behb2A13mpEh7BzH .task1,#mermaid-svg-Behb2A13mpEh7BzH .task2,#mermaid-svg-Behb2A13mpEh7BzH .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-Behb2A13mpEh7BzH .taskTextOutside0,#mermaid-svg-Behb2A13mpEh7BzH .taskTextOutside2{fill:#000}#mermaid-svg-Behb2A13mpEh7BzH .taskTextOutside1,#mermaid-svg-Behb2A13mpEh7BzH .taskTextOutside3{fill:#000}#mermaid-svg-Behb2A13mpEh7BzH .active0,#mermaid-svg-Behb2A13mpEh7BzH .active1,#mermaid-svg-Behb2A13mpEh7BzH .active2,#mermaid-svg-Behb2A13mpEh7BzH .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-Behb2A13mpEh7BzH .activeText0,#mermaid-svg-Behb2A13mpEh7BzH .activeText1,#mermaid-svg-Behb2A13mpEh7BzH .activeText2,#mermaid-svg-Behb2A13mpEh7BzH .activeText3{fill:#000 !important}#mermaid-svg-Behb2A13mpEh7BzH .done0,#mermaid-svg-Behb2A13mpEh7BzH .done1,#mermaid-svg-Behb2A13mpEh7BzH .done2,#mermaid-svg-Behb2A13mpEh7BzH .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-Behb2A13mpEh7BzH .doneText0,#mermaid-svg-Behb2A13mpEh7BzH .doneText1,#mermaid-svg-Behb2A13mpEh7BzH .doneText2,#mermaid-svg-Behb2A13mpEh7BzH .doneText3{fill:#000 !important}#mermaid-svg-Behb2A13mpEh7BzH .crit0,#mermaid-svg-Behb2A13mpEh7BzH .crit1,#mermaid-svg-Behb2A13mpEh7BzH .crit2,#mermaid-svg-Behb2A13mpEh7BzH .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-Behb2A13mpEh7BzH .activeCrit0,#mermaid-svg-Behb2A13mpEh7BzH .activeCrit1,#mermaid-svg-Behb2A13mpEh7BzH .activeCrit2,#mermaid-svg-Behb2A13mpEh7BzH .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-Behb2A13mpEh7BzH .doneCrit0,#mermaid-svg-Behb2A13mpEh7BzH .doneCrit1,#mermaid-svg-Behb2A13mpEh7BzH .doneCrit2,#mermaid-svg-Behb2A13mpEh7BzH .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-Behb2A13mpEh7BzH .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-Behb2A13mpEh7BzH .milestoneText{font-style:italic}#mermaid-svg-Behb2A13mpEh7BzH .doneCritText0,#mermaid-svg-Behb2A13mpEh7BzH .doneCritText1,#mermaid-svg-Behb2A13mpEh7BzH .doneCritText2,#mermaid-svg-Behb2A13mpEh7BzH .doneCritText3{fill:#000 !important}#mermaid-svg-Behb2A13mpEh7BzH .activeCritText0,#mermaid-svg-Behb2A13mpEh7BzH .activeCritText1,#mermaid-svg-Behb2A13mpEh7BzH .activeCritText2,#mermaid-svg-Behb2A13mpEh7BzH .activeCritText3{fill:#000 !important}#mermaid-svg-Behb2A13mpEh7BzH .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Behb2A13mpEh7BzH g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-Behb2A13mpEh7BzH g.classGroup text .title{font-weight:bolder}#mermaid-svg-Behb2A13mpEh7BzH g.clickable{cursor:pointer}#mermaid-svg-Behb2A13mpEh7BzH g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-Behb2A13mpEh7BzH g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-Behb2A13mpEh7BzH .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-Behb2A13mpEh7BzH .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-Behb2A13mpEh7BzH .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-Behb2A13mpEh7BzH .dashed-line{stroke-dasharray:3}#mermaid-svg-Behb2A13mpEh7BzH #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Behb2A13mpEh7BzH #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Behb2A13mpEh7BzH #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-Behb2A13mpEh7BzH #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-Behb2A13mpEh7BzH #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Behb2A13mpEh7BzH #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Behb2A13mpEh7BzH #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Behb2A13mpEh7BzH #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Behb2A13mpEh7BzH .commit-id,#mermaid-svg-Behb2A13mpEh7BzH .commit-msg,#mermaid-svg-Behb2A13mpEh7BzH .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Behb2A13mpEh7BzH .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Behb2A13mpEh7BzH .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Behb2A13mpEh7BzH g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Behb2A13mpEh7BzH g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-Behb2A13mpEh7BzH g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-Behb2A13mpEh7BzH g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-Behb2A13mpEh7BzH g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-Behb2A13mpEh7BzH g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-Behb2A13mpEh7BzH .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-Behb2A13mpEh7BzH .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-Behb2A13mpEh7BzH .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-Behb2A13mpEh7BzH .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-Behb2A13mpEh7BzH .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-Behb2A13mpEh7BzH .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-Behb2A13mpEh7BzH .edgeLabel text{fill:#333}#mermaid-svg-Behb2A13mpEh7BzH .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Behb2A13mpEh7BzH .node circle.state-start{fill:black;stroke:black}#mermaid-svg-Behb2A13mpEh7BzH .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-Behb2A13mpEh7BzH #statediagram-barbEnd{fill:#9370db}#mermaid-svg-Behb2A13mpEh7BzH .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-Behb2A13mpEh7BzH .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-Behb2A13mpEh7BzH .statediagram-state .divider{stroke:#9370db}#mermaid-svg-Behb2A13mpEh7BzH .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-Behb2A13mpEh7BzH .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-Behb2A13mpEh7BzH .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-Behb2A13mpEh7BzH .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-Behb2A13mpEh7BzH .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-Behb2A13mpEh7BzH .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-Behb2A13mpEh7BzH .note-edge{stroke-dasharray:5}#mermaid-svg-Behb2A13mpEh7BzH .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-Behb2A13mpEh7BzH .error-icon{fill:#522}#mermaid-svg-Behb2A13mpEh7BzH .error-text{fill:#522;stroke:#522}#mermaid-svg-Behb2A13mpEh7BzH .edge-thickness-normal{stroke-width:2px}#mermaid-svg-Behb2A13mpEh7BzH .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-Behb2A13mpEh7BzH .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-Behb2A13mpEh7BzH .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-Behb2A13mpEh7BzH .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-Behb2A13mpEh7BzH .marker{fill:#333}#mermaid-svg-Behb2A13mpEh7BzH .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-Behb2A13mpEh7BzH {color: rgba(0, 0, 0, 0.75);font: ;}

创建
parent父进程
代理进程

解决代理变成僵尸进程方法

  • parent创建一个中间进程process并等待process返回,
  • process作为代理的父进程,来创建代理进程
  • processc创建完代理进程返回代理进程pid,代理端口port,ip,自动退出
  • 此时代理进程变成孤儿进程,被系统init托管,代理进程自杀不会成未僵死进程
#mermaid-svg-D04s7TZgelD8PvuW .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-D04s7TZgelD8PvuW .label text{fill:#333}#mermaid-svg-D04s7TZgelD8PvuW .node rect,#mermaid-svg-D04s7TZgelD8PvuW .node circle,#mermaid-svg-D04s7TZgelD8PvuW .node ellipse,#mermaid-svg-D04s7TZgelD8PvuW .node polygon,#mermaid-svg-D04s7TZgelD8PvuW .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-D04s7TZgelD8PvuW .node .label{text-align:center;fill:#333}#mermaid-svg-D04s7TZgelD8PvuW .node.clickable{cursor:pointer}#mermaid-svg-D04s7TZgelD8PvuW .arrowheadPath{fill:#333}#mermaid-svg-D04s7TZgelD8PvuW .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-D04s7TZgelD8PvuW .flowchart-link{stroke:#333;fill:none}#mermaid-svg-D04s7TZgelD8PvuW .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-D04s7TZgelD8PvuW .edgeLabel rect{opacity:0.9}#mermaid-svg-D04s7TZgelD8PvuW .edgeLabel span{color:#333}#mermaid-svg-D04s7TZgelD8PvuW .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-D04s7TZgelD8PvuW .cluster text{fill:#333}#mermaid-svg-D04s7TZgelD8PvuW div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-D04s7TZgelD8PvuW .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-D04s7TZgelD8PvuW text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-D04s7TZgelD8PvuW .actor-line{stroke:grey}#mermaid-svg-D04s7TZgelD8PvuW .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-D04s7TZgelD8PvuW .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-D04s7TZgelD8PvuW #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-D04s7TZgelD8PvuW .sequenceNumber{fill:#fff}#mermaid-svg-D04s7TZgelD8PvuW #sequencenumber{fill:#333}#mermaid-svg-D04s7TZgelD8PvuW #crosshead path{fill:#333;stroke:#333}#mermaid-svg-D04s7TZgelD8PvuW .messageText{fill:#333;stroke:#333}#mermaid-svg-D04s7TZgelD8PvuW .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-D04s7TZgelD8PvuW .labelText,#mermaid-svg-D04s7TZgelD8PvuW .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-D04s7TZgelD8PvuW .loopText,#mermaid-svg-D04s7TZgelD8PvuW .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-D04s7TZgelD8PvuW .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-D04s7TZgelD8PvuW .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-D04s7TZgelD8PvuW .noteText,#mermaid-svg-D04s7TZgelD8PvuW .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-D04s7TZgelD8PvuW .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-D04s7TZgelD8PvuW .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-D04s7TZgelD8PvuW .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-D04s7TZgelD8PvuW .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-D04s7TZgelD8PvuW .section{stroke:none;opacity:0.2}#mermaid-svg-D04s7TZgelD8PvuW .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-D04s7TZgelD8PvuW .section2{fill:#fff400}#mermaid-svg-D04s7TZgelD8PvuW .section1,#mermaid-svg-D04s7TZgelD8PvuW .section3{fill:#fff;opacity:0.2}#mermaid-svg-D04s7TZgelD8PvuW .sectionTitle0{fill:#333}#mermaid-svg-D04s7TZgelD8PvuW .sectionTitle1{fill:#333}#mermaid-svg-D04s7TZgelD8PvuW .sectionTitle2{fill:#333}#mermaid-svg-D04s7TZgelD8PvuW .sectionTitle3{fill:#333}#mermaid-svg-D04s7TZgelD8PvuW .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-D04s7TZgelD8PvuW .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-D04s7TZgelD8PvuW .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-D04s7TZgelD8PvuW .grid path{stroke-width:0}#mermaid-svg-D04s7TZgelD8PvuW .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-D04s7TZgelD8PvuW .task{stroke-width:2}#mermaid-svg-D04s7TZgelD8PvuW .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-D04s7TZgelD8PvuW .taskText:not([font-size]){font-size:11px}#mermaid-svg-D04s7TZgelD8PvuW .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-D04s7TZgelD8PvuW .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-D04s7TZgelD8PvuW .task.clickable{cursor:pointer}#mermaid-svg-D04s7TZgelD8PvuW .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-D04s7TZgelD8PvuW .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-D04s7TZgelD8PvuW .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-D04s7TZgelD8PvuW .taskText0,#mermaid-svg-D04s7TZgelD8PvuW .taskText1,#mermaid-svg-D04s7TZgelD8PvuW .taskText2,#mermaid-svg-D04s7TZgelD8PvuW .taskText3{fill:#fff}#mermaid-svg-D04s7TZgelD8PvuW .task0,#mermaid-svg-D04s7TZgelD8PvuW .task1,#mermaid-svg-D04s7TZgelD8PvuW .task2,#mermaid-svg-D04s7TZgelD8PvuW .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-D04s7TZgelD8PvuW .taskTextOutside0,#mermaid-svg-D04s7TZgelD8PvuW .taskTextOutside2{fill:#000}#mermaid-svg-D04s7TZgelD8PvuW .taskTextOutside1,#mermaid-svg-D04s7TZgelD8PvuW .taskTextOutside3{fill:#000}#mermaid-svg-D04s7TZgelD8PvuW .active0,#mermaid-svg-D04s7TZgelD8PvuW .active1,#mermaid-svg-D04s7TZgelD8PvuW .active2,#mermaid-svg-D04s7TZgelD8PvuW .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-D04s7TZgelD8PvuW .activeText0,#mermaid-svg-D04s7TZgelD8PvuW .activeText1,#mermaid-svg-D04s7TZgelD8PvuW .activeText2,#mermaid-svg-D04s7TZgelD8PvuW .activeText3{fill:#000 !important}#mermaid-svg-D04s7TZgelD8PvuW .done0,#mermaid-svg-D04s7TZgelD8PvuW .done1,#mermaid-svg-D04s7TZgelD8PvuW .done2,#mermaid-svg-D04s7TZgelD8PvuW .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-D04s7TZgelD8PvuW .doneText0,#mermaid-svg-D04s7TZgelD8PvuW .doneText1,#mermaid-svg-D04s7TZgelD8PvuW .doneText2,#mermaid-svg-D04s7TZgelD8PvuW .doneText3{fill:#000 !important}#mermaid-svg-D04s7TZgelD8PvuW .crit0,#mermaid-svg-D04s7TZgelD8PvuW .crit1,#mermaid-svg-D04s7TZgelD8PvuW .crit2,#mermaid-svg-D04s7TZgelD8PvuW .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-D04s7TZgelD8PvuW .activeCrit0,#mermaid-svg-D04s7TZgelD8PvuW .activeCrit1,#mermaid-svg-D04s7TZgelD8PvuW .activeCrit2,#mermaid-svg-D04s7TZgelD8PvuW .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-D04s7TZgelD8PvuW .doneCrit0,#mermaid-svg-D04s7TZgelD8PvuW .doneCrit1,#mermaid-svg-D04s7TZgelD8PvuW .doneCrit2,#mermaid-svg-D04s7TZgelD8PvuW .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-D04s7TZgelD8PvuW .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-D04s7TZgelD8PvuW .milestoneText{font-style:italic}#mermaid-svg-D04s7TZgelD8PvuW .doneCritText0,#mermaid-svg-D04s7TZgelD8PvuW .doneCritText1,#mermaid-svg-D04s7TZgelD8PvuW .doneCritText2,#mermaid-svg-D04s7TZgelD8PvuW .doneCritText3{fill:#000 !important}#mermaid-svg-D04s7TZgelD8PvuW .activeCritText0,#mermaid-svg-D04s7TZgelD8PvuW .activeCritText1,#mermaid-svg-D04s7TZgelD8PvuW .activeCritText2,#mermaid-svg-D04s7TZgelD8PvuW .activeCritText3{fill:#000 !important}#mermaid-svg-D04s7TZgelD8PvuW .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-D04s7TZgelD8PvuW g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-D04s7TZgelD8PvuW g.classGroup text .title{font-weight:bolder}#mermaid-svg-D04s7TZgelD8PvuW g.clickable{cursor:pointer}#mermaid-svg-D04s7TZgelD8PvuW g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-D04s7TZgelD8PvuW g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-D04s7TZgelD8PvuW .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-D04s7TZgelD8PvuW .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-D04s7TZgelD8PvuW .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-D04s7TZgelD8PvuW .dashed-line{stroke-dasharray:3}#mermaid-svg-D04s7TZgelD8PvuW #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-D04s7TZgelD8PvuW #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-D04s7TZgelD8PvuW #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-D04s7TZgelD8PvuW #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-D04s7TZgelD8PvuW #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-D04s7TZgelD8PvuW #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-D04s7TZgelD8PvuW #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-D04s7TZgelD8PvuW #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-D04s7TZgelD8PvuW .commit-id,#mermaid-svg-D04s7TZgelD8PvuW .commit-msg,#mermaid-svg-D04s7TZgelD8PvuW .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-D04s7TZgelD8PvuW .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-D04s7TZgelD8PvuW .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-D04s7TZgelD8PvuW g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-D04s7TZgelD8PvuW g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-D04s7TZgelD8PvuW g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-D04s7TZgelD8PvuW g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-D04s7TZgelD8PvuW g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-D04s7TZgelD8PvuW g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-D04s7TZgelD8PvuW .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-D04s7TZgelD8PvuW .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-D04s7TZgelD8PvuW .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-D04s7TZgelD8PvuW .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-D04s7TZgelD8PvuW .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-D04s7TZgelD8PvuW .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-D04s7TZgelD8PvuW .edgeLabel text{fill:#333}#mermaid-svg-D04s7TZgelD8PvuW .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-D04s7TZgelD8PvuW .node circle.state-start{fill:black;stroke:black}#mermaid-svg-D04s7TZgelD8PvuW .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-D04s7TZgelD8PvuW #statediagram-barbEnd{fill:#9370db}#mermaid-svg-D04s7TZgelD8PvuW .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-D04s7TZgelD8PvuW .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-D04s7TZgelD8PvuW .statediagram-state .divider{stroke:#9370db}#mermaid-svg-D04s7TZgelD8PvuW .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-D04s7TZgelD8PvuW .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-D04s7TZgelD8PvuW .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-D04s7TZgelD8PvuW .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-D04s7TZgelD8PvuW .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-D04s7TZgelD8PvuW .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-D04s7TZgelD8PvuW .note-edge{stroke-dasharray:5}#mermaid-svg-D04s7TZgelD8PvuW .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-D04s7TZgelD8PvuW .error-icon{fill:#522}#mermaid-svg-D04s7TZgelD8PvuW .error-text{fill:#522;stroke:#522}#mermaid-svg-D04s7TZgelD8PvuW .edge-thickness-normal{stroke-width:2px}#mermaid-svg-D04s7TZgelD8PvuW .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-D04s7TZgelD8PvuW .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-D04s7TZgelD8PvuW .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-D04s7TZgelD8PvuW .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-D04s7TZgelD8PvuW .marker{fill:#333}#mermaid-svg-D04s7TZgelD8PvuW .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-D04s7TZgelD8PvuW {color: rgba(0, 0, 0, 0.75);font: ;}

创建
创建
parent父进程
process
代理进程

代码

import time
import subprocess
from multiprocessing import Process, Queuedef open_test_server(port, q):com = '/Users/job/Venvs/test/bin/python {} {}'.format('/Users/job/my_tests/test_server.py', port)try:test_process = subprocess.Popen(com.split())pid = test_process.pidexcept Exception as e:test_process = Nonepid = Noneprint(e)q.put({'pid': pid})def main(port):q = Queue()sub_process = Process(target=open_test_server, args=(port, q))sub_process.start()sub_process.join()info = q.get()print(info)if __name__ == '__main__':main(12345)while True:time.sleep(10)print('父进程睡了10秒')

设置守护线程-解决subprocess启动的僵尸进程

  1. 启动线程
  2. 设置为守护线程 setDaemon(True)
  3. 循环监听等待信号

代码

import threading
import subprocessdef start_proxy():try:p = subprocess.Popen("要启动的程序", stdout=subprocess.PIPE, stderr=subprocess.PIPE)while True:process_code = p.poll()if process_code is not None and process_code != 0:print("进程异常退出!{}".format(str(process_code)))breakif process_code == 0:print("进程正常退出!{}".format(str(process_code)))breakexcept Exception as e:print(e)def start_threading():try:td = threading.Thread(target=start_proxy)td.setDaemon(True)td.start()except threading.ThreadError as e:print(e)if __name__ == '__main__':start_threading()

subprocess解决僵尸进程相关推荐

  1. Linux解决僵尸进程的几种方式,SIGCHLD信号设置SIG_IGN处理方式等

    目录 僵尸进程 解决方式 父进程阻塞调用wait() 父进程非阻塞调用waitpid() SIGCHLD信号 SIG_IGN信号 僵尸进程 我们知道linux下进程有种状态叫做僵尸状态; 原因是父进程 ...

  2. 【Linux】SIGCHLD信号解决僵尸进程问题

    1. 基本信息 SIGCHLD信号产生的条件: 子进程终止时 子进程接收到SIGSTOP信号停止时 子进程处在停止态,接受到SIGCONT后唤醒时 以上三种条件都会给父进程发送SIGCHLD信号,父进 ...

  3. 0基础学会SIGCHLD()信号的使用(可以解决僵尸进程的问题,内附C语言代码)

    SIGCHLD信号产生的3个条件: 1.子进程结束: 2.子进程暂停了: 3.子进程继续运行: 都会给父进程发送该信号,父进程默认忽略此信号. 接下来使用代码来解释这个问题: 1 #include & ...

  4. 什么是僵尸进程与孤儿进程

    1.什么是僵尸进程和孤儿进程: 在 Unix/Linux 系统中,正常情况下,子进程是通过父进程创建的,且两者的运行是相互独立的,父进程永远无法预测子进程到底什么时候结束.当一个进程调用 exit 命 ...

  5. Linux | 进程概念、进程状态(僵尸进程、孤儿进程、守护进程)、进程地址空间

    文章目录 进程和程序 操作系统如何控制和调度程序 进程控制块–PCB 子进程 进程状态 僵尸进程 孤儿进程 守护进程(精灵进程) 进程地址空间 引言 页表 进程和程序 程序: 一系列有序的指令集合(就 ...

  6. 僵尸进程zombie与孤儿进程orphan

    代码已上传至https://github.com/gatieme/AderXCoding/tree/master/system/unix/zombie 问题提出 以前在学习<unix环境高级编程 ...

  7. Linux操作系统之孤儿进程和僵尸进程

    一.僵尸进程 Unix进程模型中,进程是按照父进程产生子进程,子进程产生子子进程这样的方式创建出完成各项相互协作功能的进程的. 当一个进程调用 exit 命令结束自己的生命时,其实它并没有真正的被销毁 ...

  8. 孤儿进程和僵尸进程个人总结

    1.孤儿进程和僵尸进程 Unix/Linux中有孤儿进程和僵尸进程的概念如下: 孤儿进程:如果父进程退出而它的一个或多个子进程还在运行,那么这些子进程就被称为孤儿进程.孤儿进程最终将被 init 进程 ...

  9. Linux操作系统之僵尸进程

    文章目录 一.什么是僵尸进程? 二.僵尸进程带来的危害 三.如何解决僵尸进程 四.什么是孤儿进程? 一.什么是僵尸进程? 子进程先于父进程节结束,父进程没有调用wait()获取子进程的退出码,子进程此 ...

  10. 【k8s】理解Docker容器的进程管理(PID1进程(容器内kill命令无法杀死)、进程信号处理、僵尸进程)

    文章目录 概述 1. 容器的PID namespace(名空间) 2. 如何指明容器PID1进程 3. 进程信号处理 4. 孤儿进程与僵尸进程管理 5. 进程监控 6. 总结 参考 概述 简介: Do ...

最新文章

  1. 每日一套codeforce集训1119E[贪心],821C[栈模拟],645D[拓扑排序]
  2. 不是python中用于开发用户界面的第三方库-20个必不可少的Python库也是基本的第三方库...
  3. Storm Trident API
  4. 环境变量_UG环境变量设置
  5. 中国到底有多少个.NET 程序员?都在哪个城市写代码?
  6. Forensic Challenge 9 - Mobile Malware
  7. adblock去除烦人的广告
  8. Window10 Tensorflow 2.1 GPU 安装和测试
  9. JavaScript跨域问题分析与总结_直来直往_百度空间
  10. 设计模式笔记八:过滤器模式
  11. 程序员的 JavaScript 代码该如何让计算机搞懂?
  12. python实现新闻网站_Python 教你 4 行代码开发新闻网站通用爬虫
  13. spring加载属性(properties)文件
  14. V4L2摄像头取数据程序
  15. vmplayer网络配置详解
  16. 与全球外国人即时聊天的网站
  17. 关于Go语言的底层,你想知道的都在这里!
  18. 逻辑运算符 与、或、非
  19. FFmpeg总结(九)用ffmpeg进行切片生成m3u8索引文件
  20. 小学学计算机应该学什么礼物,小学毕业礼物排行榜,运动学习两不耽误

热门文章

  1. iconfont添加新图标_IconFont图标引用的方法步骤(代码)
  2. 垃圾焚烧发电厂如何推行5S/6S/7S管理
  3. 十分钟学后端技术:怎么才能学好分布式系统和相关技术?
  4. 神经网络如何利用C语言实现动画?
  5. 2023年南昌大学电子信息考研考情与难度、参考书及上岸前辈经验
  6. imx6ull的boot, 之我的理解
  7. 拆解一探电路设计:小米路由器3C拆解
  8. 知网下载论文CAJ格式转为PDF格式
  9. 51单片机SG90舵机控制原理
  10. CentOS 7 安装教程、硬盘分区、LVM、网络配置、软件源配