- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是代码
void sigchld_handler(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int main(void)
{
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size;
struct sigaction sa;
int yes=1;
char s[INET6_ADDRSTRLEN];
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP
if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("server: socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("server: bind");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "server: failed to bind\n");
return 2;
}
freeaddrinfo(servinfo); // all done with this structure
if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
printf("server: waiting for connections...\n");
while(1) { // main accept() loop
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1) {
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s);
printf("server: got connection from %s\n", s);
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
if (send(new_fd, "Hello, world!", 13, 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd); // parent doesn't need this
}
return 0;
}
我知道SIGCHLD是子进程终止时发送给进程的信号但是子进程在哪里创建的?!
最佳答案
它们是在调用 fork
时创建的。这里:
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
fork
在父进程(以子进程 ID 作为返回值)和子进程(以 0 作为返回值)中返回。
关于c - SIGCHLD 处理 Beej 的指南示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7480828/
在下面的代码中,我期望的是控制台打印十个 SIGCHLD caught。我已经通过将 sa_flags 设置为 SA_SIGINFO 并使用 sa_sigaction 而不是 sa_handler 来
在编译我的 C 程序以使用 UDP 实现多个客户端服务器聊天程序时,我遇到了一些错误..我能够纠正其中一些错误,但无法纠正其他一些错误..他们是......: 1) 未知类型名称 sig_atomic
在运行 Linux 2.6.35+ 的系统中,我的程序创建许多子进程并监视它们。如果子进程死亡,我会进行一些清理并再次生成该进程。我使用 signalfd() 来获取进程中的 SIGCHLD 信号。
我需要检测我的后台进程之一何时退出。因此,我安装了一个陷阱。 run_gui 和 run_ai1 是简单的 exec 函数。 run_gui & gui_pid=$! run_ai1 & ai1_pi
我打算在父进程休眠 10 秒时将以下代码 fork 并作为子进程执行“ sleep 3”。我希望父进程在 3 秒后收到 SIGCHLD,当“ sleep 3”结束时。 这不会发生,相反我得到: mai
我需要正确处理 SIGCHLD。我如何将它与我现有的代码一起使用?目前我等不及子进程了,除非我使用 0 而不是 WNOHANG|WUNTRACED。 status = 0; pid_t child,
我正在尝试创建一个管理其他进程的进程,如果一个 child 死了,那么 parent 会重新启动该进程以及依赖于它的进程。 问题是我注意到,如果我在这个结构的中间重新启动一个进程时创建了一个进程的树结
我无法理解以下程序的输出。我观察到子进程返回后,父进程在 wait() 之前没有休眠 3 秒。如果 SIGCHLD 设置为默认处理程序,则它会休眠 3 秒,调用等待并按预期返回。这里究竟发生了什么??
在运行 Linux 2.6.35+ 的系统中,我的程序创建了许多子进程并监视它们。如果子进程死亡,我会进行一些清理并再次生成该进程。我使用 signalfd() 在我的进程中获取 SIGCHLD 信号
我有一个 C 程序,它每分钟轮询一次硬件传感器,并将读数添加到数据库中。为了确保读数更接近于每分钟(而不是每分钟 + 开销时间),该程序创建了一个 child 来完成实际工作,然后在创建另一个 chi
我有一个在 Linux 3.12 上运行的 C 程序。该程序产生几个子进程。其中一个进程会生成一个线程,该线程运行一段时间然后终止。当该子进程运行时,它会执行 epoll_wait()。 epoll_
我正在尝试制作一个简单的 shell,并添加功能以使用 & 在后台运行进程。在我的主要方法中我基本上有: int main() { if (signal(SIGCHLD, handle) == S
我的程序上有一个 SIGCHLD 处理程序,它捕获僵尸进程。 如何避免在执行多个 fork() 调用和 waitpid() 调用的代码部分中调用 SIGCHLD 处理程序? 因为现在,当我进行 for
我的父进程无法捕获 SIGCHLD,即使它处于 waitpid(SIGCHLD... 状态。尝试从子级向父级提供显式的kill(SIGCHLD ..)以测试父级是否没有收到信号。而且风格是AIX...
这是我的信号处理程序: pid_t pid; int status; while (pid = waitpid(-1, &status, WNOHANG|WUNTRACED|WCONTINUED))
我在开头声明这是一项家庭作业,并且在编写 SIGCHLD 处理程序时我陷入了困境。我需要访问子进程中的变量。 #include #include #include #include #incl
我在脚本中看到监控程序使用“ps”或“服务状态(在 Linux 上)”定期检查进程状态,或者在 C/C++ 中 fork 并等待进程... 我想知道是否可以将 bash 与 trap 一起使用并在收到
我目前正在使用 C++ 构建一个小型 shell。 用户可以在提示符下输入作业,例如 exe1 && exe2 &。与 BASH shell 类似,如果 exe1 成功退出,我只会执行 exe2。此外
这是我的代码,我已经简化了它。 #include #include #include #include void signal_handle(int sig) { int status
我正在阅读一篇关于信号的文章,我不确定这部分代码在做什么。有人可以为我解释一下吗?这篇文章是关于创建 TCP un-blocked 服务器。 struct sigaction sigchldActio
我是一名优秀的程序员,十分优秀!