0

SIGCHLDハンドリングと機能の関係はsleep

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

sig_atomic_t child_exit_status;

void clean_up_child_process(int signal_number)
{
    int status;
    wait(&status);
    child_exit_status = status;
}

int main()
{
    struct sigaction sigchld_action;
    memset(&sigchld_action, 0, sizeof(sigchld_action));
    sigchld_action.sa_handler = &clean_up_child_process;
    sigaction(SIGCHLD, &sigchld_action, NULL);

    pid_t child_pid = fork();
    if (child_pid > 0) {
        printf("Parent process, normal execution\n");
        sleep(60);
        printf("After sleep\n");
    } else {
        printf("Child\n");
    }

    return 0;
}

上記のコードを実行すると、次のように出力されるためです。

Parent process, normal execution
Child
After sleep

ただしsleep、実行中の関数からの影響はありません。ここで何か特別なことはありますか?

4

1 に答える 1

1

ドキュメントから:

戻り値

要求された時間が経過した場合はゼロ、または呼び出しがシグナルハンドラーによって中断された場合は、スリープするまでの秒数。

何が起こるかというと、スリープ機能がシグナル ハンドラによって中断されます。それで、それの戻り値をチェックすること60が見られます。それが意図されている場合、別の戦略を使用して残りの秒をスリープさせることができます。

于 2018-12-10T01:16:52.957 に答える