Why doesn’t SIGCHLD interrupt syscalls (i.e. read)?

When I receive a SIGCHLD blocking system calls like read doesn’t return (with EINTR). If I get another signal they do.

It doesn’t matter, whether the signal handler is set to a handler or to SIG_DFL. SA_RESTART isn’t set, explicitly unsetting it with siginterrupt (SIGCHLD, TRUE) doesn’t do anything.

Why is there special behaviour for SIGCHLD?
How can I configure, that syscalls should be interrupted by SIGCHILD?

I could not reproduce the behavior with this test program on Linux (6.11.10-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.11.10-1 (2024-11-23)):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
static sig_atomic_t child_caught = 0;
static void child_handler(int sig)
{
child_caught++;
}
int main(void)
{
int pipefd[2];
int err;
static const struct sigaction chld_action = {
.sa_handler = child_handler,
};
pid_t child_pid;
err = pipe(pipefd);
if (err == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
err = sigaction(SIGCHLD, &chld_action, NULL);
if (err == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}
child_pid = fork();
if (child_pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (child_pid == 0) {
/* child */
sleep(2);
exit(EXIT_SUCCESS);
} else {
char buf[100];
ssize_t rret;
do {
rret = read(pipefd[0], buf, sizeof(buf));
if (rret == -1) {
perror("read");
} else {
printf("read returned %zdn", rret);
}
} while (rret > 0);
printf("child_caught: %dn", (int)child_caught);
exit(rret ? EXIT_FAILURE : EXIT_SUCCESS);
}
}
</code>
<code>#include <signal.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <errno.h> #include <stdlib.h> static sig_atomic_t child_caught = 0; static void child_handler(int sig) { child_caught++; } int main(void) { int pipefd[2]; int err; static const struct sigaction chld_action = { .sa_handler = child_handler, }; pid_t child_pid; err = pipe(pipefd); if (err == -1) { perror("pipe"); exit(EXIT_FAILURE); } err = sigaction(SIGCHLD, &chld_action, NULL); if (err == -1) { perror("sigaction"); exit(EXIT_FAILURE); } child_pid = fork(); if (child_pid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (child_pid == 0) { /* child */ sleep(2); exit(EXIT_SUCCESS); } else { char buf[100]; ssize_t rret; do { rret = read(pipefd[0], buf, sizeof(buf)); if (rret == -1) { perror("read"); } else { printf("read returned %zdn", rret); } } while (rret > 0); printf("child_caught: %dn", (int)child_caught); exit(rret ? EXIT_FAILURE : EXIT_SUCCESS); } } </code>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

static sig_atomic_t child_caught = 0;

static void child_handler(int sig)
{
    child_caught++;
}

int main(void)
{
    int pipefd[2];
    int err;
    static const struct sigaction chld_action = {
        .sa_handler = child_handler,
    };
    pid_t child_pid;

    err = pipe(pipefd);
    if (err == -1) {
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    err = sigaction(SIGCHLD, &chld_action, NULL);
    if (err == -1) {
        perror("sigaction");
        exit(EXIT_FAILURE);
    }

    child_pid = fork();
    if (child_pid == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (child_pid == 0) {
        /* child */
        sleep(2);
        exit(EXIT_SUCCESS);
    } else {
        char buf[100];
        ssize_t rret;

        do {
            rret = read(pipefd[0], buf, sizeof(buf));
            if (rret == -1) {
                perror("read");
            } else {
                printf("read returned %zdn", rret);
            }
        } while (rret > 0);
        printf("child_caught: %dn", (int)child_caught);
        exit(rret ? EXIT_FAILURE : EXIT_SUCCESS);
    }
}

After 2 seconds, the program produced the following output, indicating that a SIGCHLD signal was caught and read failed with error EINTR (“Interrupted system call”):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>read: Interrupted system call
child_caught: 1
</code>
<code>read: Interrupted system call child_caught: 1 </code>
read: Interrupted system call
child_caught: 1

As Ian Abbott showed, system calls do in fact get interrupted by SIGCHLD.

However the system call I wanted to interrupt wasn’t in the main thread, but system calls seam to be only interrupted in the thread, that receives the signal. Blocking the signal in the main thread fixed the problem:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>sigset_t signal_set;
sigemptyset(&signal_set);
sigaddset(&signal_set, SIGCHLD);
sigprocmask(SIG_BLOCK, &signal_set, NULL);
</code>
<code>sigset_t signal_set; sigemptyset(&signal_set); sigaddset(&signal_set, SIGCHLD); sigprocmask(SIG_BLOCK, &signal_set, NULL); </code>
sigset_t signal_set;
sigemptyset(&signal_set);
sigaddset(&signal_set, SIGCHLD);
sigprocmask(SIG_BLOCK, &signal_set, NULL);

What makes SIGCHLD special is, that when the handler is set to SIG_DFL, it is actually ignored, so there must be signal handler, even if it doesn’t do anything:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>static void noop_handler (int signal) {
return;
}
struct sigaction handler = {0};
handler.sa_handler = noop_handler;
sigaction (SIGCHLD, &handler, NULL))
</code>
<code>static void noop_handler (int signal) { return; } struct sigaction handler = {0}; handler.sa_handler = noop_handler; sigaction (SIGCHLD, &handler, NULL)) </code>
static void noop_handler (int signal) {
    return;
}

struct sigaction handler = {0};
handler.sa_handler = noop_handler;
sigaction (SIGCHLD, &handler, NULL))

That maked me think, that SIGCHLD doesn’t work, but combining both it does.

However, I wonder, if there is a different way to achieve that signal disposition, then specifying ǹoop_handler.

7

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật