以下程序分叉一个孩子,反复运行"/ bin/sleep 10".父级为SIGINT安装一个信号处理程序,它将SIGINT传递给子级.但是,有时向孩子发送SIGINT失败.为什么这样,我错过了什么?
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
pid_t foreground_pid = 0;
void sigint_handler(int sig)
{
printf("sigint_handler: Sending SIGINT to process %d\n",
foreground_pid);
if ((foreground_pid != 0) && kill(foreground_pid, SIGCONT) == -1) {
perror("sending SIGINT to forground process failed");
printf("foreground_pid == %d", foreground_pid);
exit(EXIT_FAILURE);
}
foreground_pid = 0;
}
int main(int argc, const char *argv[])
{
while (1) {
pid_t child_pid;
if ((child_pid = fork()) == -1) {
perror("fork failed");
exit(EXIT_FAILURE); …Run Code Online (Sandbox Code Playgroud) 以下代码运行2个孩子,他们将等待10秒并终止.父母坐在一个循环中,等待孩子们终止:
#!/usr/bin/perl
use strict;
use warnings;
use POSIX ":sys_wait_h";
sub func
# {{{
{
my $i = shift;
print "$i started\n";
$| = 1;
sleep(10);
print "$i finished\n";
}
# }}}
my $n = 2;
my @children_pids;
for (my $i = 0; $i < $n; $i++) {
if ((my $pid = fork()) == 0) {
func($i);
exit(0);
} else {
$children_pids[$i] = $pid;
}
}
my $stillWaiting;
do {
$stillWaiting = 0;
for (my $i = 0; $i < $n; …Run Code Online (Sandbox Code Playgroud) 我正在查看esh(easy shell)的实现,并且无法理解在这种情况下22和9是什么信号.理想情况下,有一个更具描述性的常量,但我找不到列表.
脚步:
在不同的程序组中派生并启动进程
使用 SIGTSTP
停止进程 使用 SIGCONT 重新启动
进程 进程结束
问题:SIGCHLD 处理程序有:
waitpid(-1, &status, WNOHANG | WUNTRACED);
Run Code Online (Sandbox Code Playgroud)
返回 pid=0 和 WIFEXITED=1 后,进程退出,但我无法获取 pid?我需要 pid。
从手册页来看:“如果指定了 WNOHANG 并且 pid 指定的一个或多个子进程存在,但尚未更改状态,则返回 0”
但状态似乎已更改为退出。
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
{
pid_t Checksum_pid = fork();
if (Checksum_pid < 0)
printf("Fork Failed\n");
else if (Checksum_pid == 0)
{
printf("\nInside Child Process before execl");
//execl("/bin/sleep", "/bin/sleep", "2", NULL);
execl("/bin/ls","ls",(char *)NULL) ;
//exit(EXIT_FAILURE);
printf("\nInside Child Process after execl\n");
exit(EXIT_FAILURE);
}
else
{
int childStatus;
printf("\nInside Parent Process");
sleep(5);
pid_t returnValue = waitpid(Checksum_pid, &childStatus, WNOHANG);
if (returnValue > 0)
{
if (WIFEXITED(childStatus))
printf("\nChild Exit Code: %d\n", WEXITSTATUS(childStatus));
else
printf("\nChild Exit Status: 0x%.4X\n", …Run Code Online (Sandbox Code Playgroud) 据我所知,如果 waitpid 返回 -1,则它是错误条件。如何从 WEXITSTATUS(childStatus) 中的子进程获得成功 (EXIT_SUCCUSS)?
waitpid 中的 childStatus 与 WEXITSTATUS(childStatus) 的返回值有什么区别?一样吗?
pid_t returnValue = waitpid(Checksum_pid, &childStatus, WNOHANG);
printf("return value = %d", returnValue);
printf("return value = %d", childStatus);
if (WIFEXITED(childStatus))
{
printf("Exit Code: _ WEXITSTATUS(childStatus)") ;
//Proceed with other calculation.
}
Run Code Online (Sandbox Code Playgroud) 我一直在编写一个生成子进程的程序,并调用waitpid等待子进程的终止.代码如下:
// fork & exec the child
pid_t pid = fork();
if (pid == -1)
// here is error handling code that is **not** triggered
if (!pid)
{
// binary_invocation is an array of the child process program and its arguments
execv(args.binary_invocation[0], (char * const*)args.binary_invocation);
// here is some error handling code that is **not** triggered
}
else
{
int status = 0;
pid_t res = waitpid(pid, &status, 0);
// here I see pid_t being a positive integer …Run Code Online (Sandbox Code Playgroud) 我正在研究科比和奥哈拉伦的作品Computer Systems, A Programmer's Perspective。练习 8.16 要求程序的输出如下(我更改了它,因为他们使用了一个你可以在他们的网站上下载的头文件):
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
int counter = 1;
int main()
{
if (fork() == 0){
counter--;
exit(0);
}
else{
Wait(NULL);
printf("counter = %d\n", ++counter);
}
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
我回答“counter = 1”,因为父进程等待其子进程终止,然后递增计数器。但孩子首先递减它。然而,当我测试该程序时,我发现正确的答案是“counter = 2”。变量“counter”在子进程和父进程中是否不同?如果不是,那为什么答案是2?
我没有在我的代码中处理SIGCHLD.终止后,我的流程仍然立即删除.我希望它成为僵尸进程.如果我将SIGCHLD设置为SIGDFT那么,它是否行得通呢?我如何将SIGCHLD设置为SIGDFT?我希望进程变成僵尸,所以我可以在waitpid之后读取父进程中的子状态.
我刚刚听了一个演讲,总结为:
收割
由父级对终止的子级执行(使用 wait 或 waitpid)
父母获得退出状态信息
内核然后删除僵尸子进程
所以我明白收割是通过调用wait或waitpid从父进程完成的,之后内核删除僵尸进程。如果确实是这种情况,那么只有在调用waitor时才会进行收割waitpid,为什么在 theor 入口函数中返回后子进程实际上消失了 - 我的意思是确实看起来好像子进程已经被收割了,因此没有资源即使父进程可能没有等待,也浪费了。
所以唯一可能的“收获”打电话时wait还是waitpid?只要进程从其入口函数返回并退出(我假设所有进程都这样做),进程是否会被“收割” - 谈论“收割”好像它是特殊的东西有什么意义?