Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alarm() in child process #1241

Open
adamdebek opened this issue Nov 26, 2024 · 0 comments
Open

alarm() in child process #1241

adamdebek opened this issue Nov 26, 2024 · 0 comments
Labels
bug Something isn't working libphoenix

Comments

@adamdebek
Copy link
Contributor

alarm() creates thread which wll send a signal, but fork() in multi-threaded process copy only thread from which was invoked, so alarm thread will not be present in that new process causing no arrival of alarm signal.

Reproduciton (ia32-generic-qemu):

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

volatile int n;

void handler(int signum) {
        printf("n: %d\n", ++n);
}

int main(void) {
    int i = 1;

    signal(SIGALRM, handler);

    alarm(5);
    while (i <= 8) {
        printf("tick %d\n", i++);
        sleep(1);
    }

    pid_t pid;
    pid = fork();
    if (pid < 0) {
        perror("fork");
        exit(EXIT_FAILURE);
    }
    else if (pid == 0) {
        alarm(5);
        i = 1;
        while (i <= 8) {
            printf("tick %d\n", i++);
            sleep(1);
        }

        exit(2);
    }

    int status;
    wait(&status);
    
    printf("status: %d\n", WEXITSTATUS(status));

    return 0;
}

In above reproduction handler is invoked once, but should be twice.

Furtermore as POSIX states:
If there is a previous alarm() request with time remaining, alarm() shall return a non-zero value that is the number of seconds until the previous request would have generated a SIGALRM signal. Otherwise, alarm() shall return 0.

Reproduction (ia32-generic-qemu):

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


int main(void) {
    unsigned int ret;
    alarm(1);
    ret = alarm(0);

    printf("ret: %u\n", ret);

    return 0;
}

image

The return value should be 1.

@adamdebek adamdebek added bug Something isn't working libphoenix labels Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working libphoenix
Projects
None yet
Development

No branches or pull requests

1 participant