1

子プロセスが実行されるとfork()、親プロセスwait()は子プロセスを完了することができます。試しにwait()、親プロセスを作るのでsleep()はなく、なぜそれが機能しないとしますか?

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{
    pid_t child_id ;

    child_id = fork() ;

    if (child_id == 0)
    {
        printf("\nChild process");
        printf("\nChild process exiting");
    }
    else
    {
        printf("\nParent process");
        sleep(10);
        printf("\nParent process exiting");
    }
}

SIGCHLDシグナルが親プロセスを から復帰させていると思いsleep()ます。しかし、なぜ、それは子プロセスであり、異なるアドレス空間とリソースを持っているのに、どうして親プロセスの問題に干渉できるのでしょうか?

4

2 に答える 2

3

システム間の違いに注意してください。Mac OS X 10.9 で実行されているコードのこのマイナーな適応により、子の死亡はsleep(10)親の に影響しません。

Parent process

Child process
Child process exiting 1384590368

Parent process exiting 1384590378

ご覧のとおり、親は子よりも 10 秒程度遅れて終了しました。

#include <stdio.h>
#include <unistd.h>
#include <time.h>

int main(void)
{
    pid_t child_id;

    child_id = fork();

    if (child_id == 0)
    {
        printf("\nChild process");
        printf("\nChild process exiting %ld\n", (long)time(0));
    }
    else
    {
        printf("\nParent process\n");
        sleep(10);
        printf("\nParent process exiting %ld\n", (long)time(0));
    }
}

古いバージョンの Linux (2008 年の 2.6.16.60 カーネル) を実行している VM でも同じ動作をしました。親は子供が死亡してから 10 秒後に死亡しました。

それで、あなたが尋ねている行動が「なぜうまくいかないのですか?」「親はすぐに子が死ぬ」である場合、コードは2つのシステムのいずれかで終了することを証明しません。あなたのシステムで親がすぐに死なないと断言することはできませんが、それは予想外です。

このプログラムは、SIGCHLD シグナルの動作を調べるのに役立つ場合があります。

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

static siginfo_t sig_info;
static volatile sig_atomic_t sig_num;
static void *sig_ctxt;

static void catcher(int signum, siginfo_t *info, void *vp)
{
    sig_num = signum;
    sig_info = *info;
    sig_ctxt = vp;
}

static void set_handler(int signum)
{
    struct sigaction sa;
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = catcher;
    sigemptyset(&sa.sa_mask);

    if (sigaction(signum, &sa, 0) != 0)
    {
        int errnum = errno;
        fprintf(stderr, "Failed to set signal handler (%d: %s)\n", errnum, strerror(errnum));
        exit(1);
    }
}

static void prt_interrupt(FILE *fp)
{
    if (sig_num != 0)
    {
        fprintf(fp, "Signal %d from PID %d\n", sig_info.si_signo, (int)sig_info.si_pid);
        sig_num = 0;
    }
}

static void five_kids(void)
{
    for (int i = 0; i < 5; i++)
    {
        pid_t pid = fork();
        if (pid < 0)
            break;
        else if (pid == 0)
        {
            printf("PID %d - exiting with status %d\n", (int)getpid(), i);
            exit(i);
        }
        else
        {
            int status = 0;
            pid_t corpse = wait(&status);
            printf("Child: %d; Corpse: %d; Status = 0x%.4X\n", pid, corpse, (status & 0xFFFF));
            prt_interrupt(stdout);
            fflush(0);
        }
    }
}

int main(void)
{
    printf("SIGCHLD set to SIG_DFL\n");
    signal(SIGCHLD, SIG_DFL);
    five_kids();

    printf("SIGCHLD set to SIG_IGN\n");
    signal(SIGCHLD, SIG_IGN);
    five_kids();

    printf("SIGCHLD set to catcher()\n");
    set_handler(SIGCHLD);
    five_kids();

    return(0);
}

Mac OS X 10.9 では、次のように表示されます。

SIGCHLD set to SIG_DFL
PID 52345 - exiting with status 0
Child: 52345; Corpse: 52345; Status = 0x0000
PID 52346 - exiting with status 1
Child: 52346; Corpse: 52346; Status = 0x0100
PID 52347 - exiting with status 2
Child: 52347; Corpse: 52347; Status = 0x0200
PID 52348 - exiting with status 3
Child: 52348; Corpse: 52348; Status = 0x0300
PID 52349 - exiting with status 4
Child: 52349; Corpse: 52349; Status = 0x0400
SIGCHLD set to SIG_IGN
PID 52350 - exiting with status 0
Child: 52350; Corpse: -1; Status = 0x0000
PID 52351 - exiting with status 1
Child: 52351; Corpse: -1; Status = 0x0000
PID 52352 - exiting with status 2
Child: 52352; Corpse: -1; Status = 0x0000
PID 52353 - exiting with status 3
Child: 52353; Corpse: -1; Status = 0x0000
PID 52354 - exiting with status 4
Child: 52354; Corpse: -1; Status = 0x0000
SIGCHLD set to catcher()
PID 52355 - exiting with status 0
Child: 52355; Corpse: -1; Status = 0x0000
Signal 20 from PID 52355
Child: 52356; Corpse: 52355; Status = 0x0000
PID 52356 - exiting with status 1
Child: 52357; Corpse: -1; Status = 0x0000
PID 52357 - exiting with status 2
Signal 20 from PID 52356
Child: 52358; Corpse: 52357; Status = 0x0200
Signal 20 from PID 52357
PID 52358 - exiting with status 3
Child: 52359; Corpse: 52356; Status = 0x0100
PID 52359 - exiting with status 4

Linux での動作は似ていますが、まったく同じではありません。

SIGCHLD set to SIG_DFL
PID 14645 - exiting with status 0
Child: 14645; Corpse: 14645; Status = 0x0000
PID 14646 - exiting with status 1
Child: 14646; Corpse: 14646; Status = 0x0100
PID 14647 - exiting with status 2
Child: 14647; Corpse: 14647; Status = 0x0200
PID 14648 - exiting with status 3
Child: 14648; Corpse: 14648; Status = 0x0300
PID 14649 - exiting with status 4
Child: 14649; Corpse: 14649; Status = 0x0400
SIGCHLD set to SIG_IGN
PID 14650 - exiting with status 0
Child: 14650; Corpse: -1; Status = 0x0000
PID 14651 - exiting with status 1
Child: 14651; Corpse: -1; Status = 0x0000
PID 14652 - exiting with status 2
Child: 14652; Corpse: -1; Status = 0x0000
PID 14653 - exiting with status 3
Child: 14653; Corpse: -1; Status = 0x0000
PID 14654 - exiting with status 4
Child: 14654; Corpse: -1; Status = 0x0000
SIGCHLD set to catcher()
PID 14655 - exiting with status 0
Child: 14655; Corpse: 14655; Status = 0x0000
Signal 17 from PID 14655
PID 14656 - exiting with status 1
Child: 14656; Corpse: 14656; Status = 0x0100
Signal 17 from PID 14656
PID 14657 - exiting with status 2
Child: 14657; Corpse: 14657; Status = 0x0200
Signal 17 from PID 14657
PID 14658 - exiting with status 3
Child: 14658; Corpse: 14658; Status = 0x0300
Signal 17 from PID 14658
PID 14659 - exiting with status 4
Child: 14659; Corpse: 14659; Status = 0x0400
Signal 17 from PID 14659
于 2013-11-16T08:41:41.967 に答える
0

fork(2)execve(2)wait(2) ...の man ページを注意深く読んでください。

syscall は、子プロセスを受動的に待機するだけではwaitありません。ゾンビ プロセスを回避するために内部カーネル状態をクリーンアップします。

プログラムなどでstrace(1)も使用してください。strace -f

また、 Advanced Linux Programmingなどの優れた本を読むのに数時間かかります。プロセスを理解するには何時間もかかりますが、それを教える時間はあまりありません。本を読んで、あなたと同じように実験を続けてください!また、フリーソフトウェアのソースコードを読むのにも時間がかかります (いくつかのシェルのソースコードのように -egbashsash)

ところで、あなたのプログラムは別の点で間違っています:の失敗を常にテストする必要があります (したがって、子プロセス内、親プロセス内、失敗時forkの 3 つの可能な戻り値を常に処理する必要があります)。ulimit (シェル内でsetrlimit(2)を呼び出す) を使用して、テスト目的でこのようなエラー状態をトリガーすることを検討してください。fork==0>0<0

于 2013-11-16T08:04:58.470 に答える