问题描述
我想检查子进程在Unix上的C中是否终止。它不应该阻塞,而是在循环中进行简短检查。 我的代码:
pid_t pid = fork();
if (pid > 0)
// Parent Process
while (1) {
// Do a short check whether Child has already terminated if yes break the loop.
// Ik that it's possible to use waitpid(pid,&status,0) but that blocks the whole loop until the child has terminated
}
if (pid == 0)
printf("child process born");
exit(0);
提前谢谢
解决方法
resource
|_______static
|________css
|_________bootstrap.min.css
============================================================================
@Bean
fun staticRouter(): RouterFunction<ServerResponse> {
return RouterFunctions.resources("/**",ClassPathResource("static/"))
}
============================================================================
localhost:8080/css/bootstrap.min.css,200 OK
的第三个参数是一组标志。如果您将waitpid
传递给此参数,则如果还没有子级退出,该函数将立即返回。
然后您可以检查WNOHANG
是否返回0。如果是,则没有孩子退出,请等待并重试。
waitpid
,
传统方式是:
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
int exist(pid_t pid) {
return kill(pid,0) > 0 || errno != ESRCH;
}
int main(int ac,char **av) {
while (--ac > 0) {
pid_t p = strtol(*++av,0);
printf("%d %s\n",p,exist(p) ? "exists" : "doesn't exist");
}
return 0;
}
它并不在乎parent:child关系(而等待派生确实如此),即使您没有权限影响该过程,它也可以正常工作。