ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

重新父母停止过程

2019-11-18 23:50:37  阅读:165  来源: 互联网

标签:ps c-3 linux process


如何重新为已停止的进程加油?为什么停止的进程只是在重新创建父项后才终止?

更准确地说,假设我有这样的代码

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h> 
#include <sys/syscall.h>
#include <stdio.h>


int main(void) {
    pid_t child;

    child = fork();

    if (child == 0) {
        int t = 0;
        while (true) {
            printf("%d. I'm %d, my parent is %d\n", t++, getpid(), getppid());
            sleep(1);
        }
    } else {
        printf("I'm the parent. My pid is %d\n", getpid());
        printf("Starting to wait for 30 seconds\n");
        sleep(30);
        printf("Done waiting, aborting\n");
    }
}

当我运行此代码时,子进程工作,而父进程进入睡眠状态.经过30秒后,父进程终止,子进程现在成为init的子进程,并继续运行.一切正常.

但是,如果我运行此代码,并且在执行的前30秒内,我也会运行

杀死-SIGSTOP< child_pid>

然后,子进程停止(ps xaf中的T状态),父进程进入睡眠状态.经过30秒后,父进程从睡眠状态返回并只是终止(因为它到达了main的末尾),但是子进程而不是被重新父级化为处于停止状态的init而是终止了.我在ps xaf中看不到它,如果运行lastcomm,我会看到以下输出:

a.out             F  X equi     pts/5      0.00 secs Wed Mar 16 17:44

为什么发生这种情况,导致重新创建父进程后停止的进程死亡?在Linux中是否有可能重新批准已停止的进程?

解决方法:

http://www.gnu.org/software/libc/manual/html_node/Job-Control-Signals.html

When a process in an orphaned process group (see Orphaned Process
Groups) receives a SIGTSTP, SIGTTIN, or SIGTTOU signal and does not
handle it, the process does not stop. Stopping the process would
probably not be very useful, since there is no shell program that will
notice it stop and allow the user to continue it. What happens instead
depends on the operating system you are using. Some systems may do
nothing; others may deliver another signal instead, such as SIGKILL or
SIGHUP. On GNU/Hurd systems, the process dies with SIGKILL; this
avoids the problem of many stopped, orphaned processes lying around
the system.

另请参阅:What’s the difference between SIGSTOP and SIGTSTP?

标签:ps,c-3,linux,process
来源: https://codeday.me/bug/20191118/2031966.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有