C 信号和过程
所以基本上我希望“cmd_limit”以秒为单位获取一个数字,这是我们等待子进程(可以安全地假设只有一个)完成的最长时间。如果子进程在睡眠期间完成,我希望 cmd_limit 返回 pass 并且不运行其余的 cmd_limit 代码。谁能帮我做到这一点,这就是我到目前为止所得到的..
int cmd_limit( int limit, int pid ) {
signal( SIGCHLD, child_died );
sleep( limit );
kill( pid, SIGKILL );
printf("killin'\n");
return PASS;
}
void child_died( int sig ) {
int stat_loc; /* child return information */
int status; /* child return status */
waitpid( -1, &stat_loc, WNOHANG );
if( WIFEXITED(stat_loc) ) { // program exited normally
status = WEXITSTATUS( stat_loc ); /* get child exit status */
}
printf("child died: %s\n", signal);
}
so basically I want "cmd_limit" to take a number in seconds which is the maximum time we'll wait for the child process (safe to assume there's only one) to finish. If the child process does finish during the sleep, I want cmd_limit to return pass and not run the rest of the cmd_limit code. Could anyone help me do this, here's what I've got so far..
int cmd_limit( int limit, int pid ) {
signal( SIGCHLD, child_died );
sleep( limit );
kill( pid, SIGKILL );
printf("killin'\n");
return PASS;
}
void child_died( int sig ) {
int stat_loc; /* child return information */
int status; /* child return status */
waitpid( -1, &stat_loc, WNOHANG );
if( WIFEXITED(stat_loc) ) { // program exited normally
status = WEXITSTATUS( stat_loc ); /* get child exit status */
}
printf("child died: %s\n", signal);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果发送了信号,kill 返回 0;如果没有发送信号,则返回 -1。如果进程已经退出,则无法发送信号,因此可以使用kill的返回值来告诉您进程是被kill杀死还是已经死亡。
此外,您可以使用 wait 系统调用之一来查看子进程的状态更改(包括 exit )。
您还应该研究父进程中的 SIGCHLD 信号处理,当子进程更改状态(包括 exit )时,父进程会收到该信号,并且还会将父进程踢出睡眠状态(尽管在某些系统上,睡眠功能可能会重新启动睡眠状态) )。
如果孩子在您的时间限制内退出,使用 wait 和/或 SIGCHLD 您可以让父母更快完成。
您应该注意,您可能应该调用 wait 来等待子进程退出,这样就不会创建僵尸进程(已调用 exit 但必须保留的进程,以防父进程想要查看其退出状态)。
您需要研究的另一件事是 errno 值 EINTR,如果系统调用被信号中断,则可以设置该值。
在命令行中输入:
以获取有关我所讨论内容的更多信息。
kill returns 0 if a signal was sent, and -1 if no signal was sent. No signal can be sent if the process has already exited, so the return value from kill can be used to tell you if the process was killed by kill or already dead.
In addition, you could use one of the wait system calls to look into the state change (including exit ) of the child process.
You should also look into signal handling in the parent process for SIGCHLD, which the parent would receive when the child changes state (including exit ) and will also kick the parent out of a sleep state (though on some systems the sleep function may restart sleeping).
Using wait and/or SIGCHLD you could make the parent finish sooner if the child exited within your time limit.
You should note that you probably should call wait for child processes to exit so that you don't create zombies (processes which have called exit but have to be kept around in case the parent wants to look at their exit status).
Another thing you'll want to look into is the errno value EINTR which system calls can set if they were interrupted by a signal.
From your command line type:
for more information about the things I've talked about.
你的意思只是这样吗?
Do you just mean something like this?