gpt4 book ai didi

linux - LINUX 中 SIGCONT 和 waitpid() 的问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:01 27 4
gpt4 key购买 nike

我会尽量保持简单。我目前正在为 LINUX 复制一个 shell。我使用链表结构“job_list”来存储所有后台进程。如果后台进程终止,则将其从列表中删除。如果后台进程被挂起,其在列表中的状态将从 BACKGROUND 更改为 STOPPED。如果进程被重新唤醒(通过 SIGCONT 信号),那么想法是列表中的进程状态应该变回 BACKGROUND。

我的问题如下:当我向一个进程发送一个 SIGSTOP 信号时,//1 部分被执行并且它的状态变化被成功地注册在列表中。但是,当我使用 SIGCONT 信号重新唤醒同一个进程时,WIFCONTINUED(status) 将返回 false,但 WIFEXITED(status) 将始终返回 true。因此,执行//3 部分并将进程从列表中删除。

有什么问题吗?提前致谢。

void sigchld_handler (){

block_SIGCHLD();


job *item;
int l_size = list_size(job_list);
int i, new_pid, pid_wait, status, info;
enum status status_res;

for (i = 1; i <= l_size; i++){



item = get_item_bypos(job_list, i);
new_pid = item->pgid;
pid_wait = waitpid(new_pid, &status, WUNTRACED | WNOHANG);



if (WIFSTOPPED(status)){
//1
printf("****SUSPENDED\n");
item->state = STOPPED;

}else if (WIFCONTINUED(status)){
//2
printf("****CONTINUED\n");
item->state = BACKGROUND;

}else if (WIFEXITED(status)){
//3
printf("****EXITED\n");
l_size--;
i--;
delete_job(job_list, item);

}
}

print_job_list(job_list);
unblock_SIGCHLD();

}

最佳答案

您在调用 waitpid 时似乎缺少 WCONTINUED 值。

来自 waitpid specification :

pid_t waitpid(pid_t pid, int *stat_loc, int options);

The options argument is constructed from the bitwise-inclusive OR of zero or more of the following flags, defined in the header:

WCONTINUED

The waitpid() function shall report the status of any continued child process specified by pid whose status has not been reported since it continued from a job control stop.

关于linux - LINUX 中 SIGCONT 和 waitpid() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30307876/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com