AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / unix / Perguntas / 778175
Accepted
Tavian Barnes
Tavian Barnes
Asked: 2024-06-12 04:54:06 +0800 CST2024-06-12 04:54:06 +0800 CST 2024-06-12 04:54:06 +0800 CST

É possível que o SIGALRM seja entregue após settimer() desabilitar o timer?

  • 772

Tenho alguns códigos como este que falharam uma vez no CI e uma vez localmente, embora não consiga reproduzi-lo de maneira confiável. O código é assim:

#include <stddef.h>
#include <signal.h>
#include <sys/time.h>
#include <time.h>

int main(void) {
    // Ignore SIGALRM
    signal(SIGALRM, SIG_IGN);

    // Set a timer for every 100us
    struct itimerval ival = {0};
    ival.it_value.tv_usec = 100;
    ival.it_interval.tv_usec = 100;
    setitimer(ITIMER_REAL, &ival, NULL);

    // Busy-wait 1s
    time_t start = time(NULL);
    while (time(NULL) <= start);

    // Disable the timer
    ival.it_value.tv_usec = 0;
    setitimer(ITIMER_REAL, &ival, NULL);

    // Restore the default SIGALRM disposition
    signal(SIGALRM, SIG_DFL);

    // Busy-wait 1s
    start = time(NULL);
    while (time(NULL) <= start);

    return 0;
}

A falha que observo é o encerramento do processo devido a SIGALRM(o shell diz "Despertador"). É possível que um SIGALRMtemporizador seja entregue após os setitimer()retornos finais?

O texto POSIXsetitimer() não é muito claro sobre a ordem entre os sinais e setitimer()a desativação do temporizador.

Observei essa falha uma vez no Linux e uma vez no macOS .

c
  • 2 2 respostas
  • 93 Views

2 respostas

  • Voted
  1. Best Answer
    Tavian Barnes
    2024-07-06T04:59:45+08:002024-07-06T04:59:45+08:00

    Isso acabou não sendo um problema setitimer(), mas sim um problema ABA com meu código de registro do manipulador de sinal. Basicamente, eu tinha uma série de manipuladores de sinal para poder adicioná-los/removê-los dinamicamente durante o teste. Mas se você tem um loop fazendo

    while (true) {
        // At least one signal handler is always present
        // handlers[] is an array of atomic pointers
        handlers[1] = handler; // A1
        handlers[0] = NULL;    // A2
        handlers[0] = handler; // A3
        handlers[1] = NULL;    // A4
    }
    

    enquanto o manipulador de sinal faz

    bool handled = false;
    if (handlers[0]) { // B1
        handlers[0]();
        handled = true;
    }
    if (handlers[1]) { // B2
        handlers[1]();
        handled = true;
    }
    if (!handled) {
        signal(signo, SIG_DFL);
        raise(signo);
    }
    

    Então a intercalação A1, A2, B1, A3, A4, B2fará com que ele pense que ambos os manipuladores estão NULLe aumente novamente o sinal fatal.

    • 1
  2. warashi nguyen
    2024-06-30T11:10:20+08:002024-06-30T11:10:20+08:00
    #include <stddef.h>
    #include <signal.h>
    #include <sys/time.h>
    #include <time.h>
    #include <stdio.h>
    
    int main(void) {
        // Ignore SIGALRM
        signal(SIGALRM, SIG_IGN);
    
        // Set a timer for every 100us
        struct itimerval ival = {0};
        ival.it_value.tv_usec = 100;
        ival.it_interval.tv_usec = 100;
        setitimer(ITIMER_REAL, &ival, NULL);
    
        // Busy-wait 1s
        time_t start = time(NULL);
        while (time(NULL) <= start);
    
        // Block SIGALRM
        sigset_t mask, oldmask;
        sigemptyset(&mask);
        sigaddset(&mask, SIGALRM);
        sigprocmask(SIG_BLOCK, &mask, &oldmask);
    
        // Disable the timer
        ival.it_value.tv_usec = 0;
        setitimer(ITIMER_REAL, &ival, NULL);
    
        // Unblock SIGALRM
        sigprocmask(SIG_SETMASK, &oldmask, NULL);
    
        // Restore the default SIGALRM disposition
        signal(SIGALRM, SIG_DFL);
    
        // Busy-wait 1s
        start = time(NULL);
        while (time(NULL) <= start);
    
        return 0;
    }
    
    • -3

relate perguntas

  • Obter saída somente hexadecimal do objdump

  • Leia /proc/pid/maps usando read()

  • Quando o heap é usado para alocação dinâmica de memória?

  • O que exatamente o GNU faz dep faz?

  • Otimização de programas baseados em OpenCV sistema operacional linux incorporado [fechado]

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    Possível firmware ausente /lib/firmware/i915/* para o módulo i915

    • 3 respostas
  • Marko Smith

    Falha ao buscar o repositório de backports jessie

    • 4 respostas
  • Marko Smith

    Como exportar uma chave privada GPG e uma chave pública para um arquivo

    • 4 respostas
  • Marko Smith

    Como podemos executar um comando armazenado em uma variável?

    • 5 respostas
  • Marko Smith

    Como configurar o systemd-resolved e o systemd-networkd para usar o servidor DNS local para resolver domínios locais e o servidor DNS remoto para domínios remotos?

    • 3 respostas
  • Marko Smith

    apt-get update error no Kali Linux após a atualização do dist [duplicado]

    • 2 respostas
  • Marko Smith

    Como ver as últimas linhas x do log de serviço systemctl

    • 5 respostas
  • Marko Smith

    Nano - pule para o final do arquivo

    • 8 respostas
  • Marko Smith

    erro grub: você precisa carregar o kernel primeiro

    • 4 respostas
  • Marko Smith

    Como baixar o pacote não instalá-lo com o comando apt-get?

    • 7 respostas
  • Martin Hope
    user12345 Falha ao buscar o repositório de backports jessie 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl Por que a maioria dos exemplos do systemd contém WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky Como exportar uma chave privada GPG e uma chave pública para um arquivo 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll status systemctl mostra: "Estado: degradado" 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim Como podemos executar um comando armazenado em uma variável? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S Por que /dev/null é um arquivo? Por que sua função não é implementada como um programa simples? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 Como ver as últimas linhas x do log de serviço systemctl 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - pule para o final do arquivo 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla Por que verdadeiro e falso são tão grandes? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis Substitua a string em um arquivo de texto enorme (70 GB), uma linha 2017-12-30 06:58:33 +0800 CST

Hot tag

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve