相关疑难解决方法(0)

我该如何处理SIGCHLD?

我需要SIGCHLD妥善处理.如何在现有代码中使用它.目前我不能等待孩子的过程,除非我用0而不是WNOHANG|WUNTRACED.

status = 0; 
pid_t child, endID;

if(amp == 1)
        signal( SIGCHLD, SIG_IGN ); 

child = fork(); 

if (child  <  0) {    
        perror("fork() error\n");
        exit(EXIT_FAILURE);

} else if (child == 0) { 
        // do sth here
        perror("error\n");

} else { 
        //sleep(1)
Run Code Online (Sandbox Code Playgroud)

如果我删除sleep然后父执行第一..为什么?

c sigchld

12
推荐指数
1
解决办法
3万
查看次数

父进程如何等待所有子进程终止

我是C的新手,所以我只知道简单的函数.(例如:wait(NULL)).

这是我的主要问题:

修改程序,以便只有父进程创建3个子进程,每个新创建的进程调用一个函数CPU().此外,让父进程等待每个子进程终止.

我知道这是正确的答案

#include<stdio.h>

int main(void)
{
    int i ;

    for( i = 0; i < 3; i++)
    {
        int ret = fork();
        if(ret == 0){
            printf("My process ID is %d\n", getpid());
            return 0;
        }

    }

    for (i = 0; i < 3; i++) //Line B
        wait(NULL);
}
Run Code Online (Sandbox Code Playgroud)

但我的问题是

  1. 当循环中的父进程执行wait时,为什么下面的代码是错误的

    #include <stdio.h>
    
    int main(void)
    {
        int i ;
    
        for( i = 0; i < 3; i++)
        {
            int ret = fork();
    
            if(ret == 0) {
                printf("My process ID is …
    Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
198
查看次数

标签 统计

c ×2

sigchld ×1