SIGINT 回调打印两次

发布于 2025-01-14 04:37:21 字数 1875 浏览 3 评论 0原文

我正在尝试启动 IPC 的相关项目,根据我的观点,我必须开始处理信号。当按下 Ctrl+C 时,处理程序函数会正确启动,但在控制台中它会显示打印两次。

另一个奇怪的情况是,如果我将字符更改为空字符串,父进程会正常休眠 10 秒,然后接收 SIGINT

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sem.h>
void handler(int);
/*typedef struct {
int semaforo;
//iremos añadiendo mas a medida que necesitemos, pero para pasar todos como un
solo argumento a la función manejadora es más comodo. }ipces;*/
int semaforo;
int main() {
    // ipces ipces;
    semaforo = semget(IPC_PRIVATE, 3, IPC_CREAT | 0600);

    semctl(semaforo, 0, SETVAL, 1);

    struct sigaction newact, oldact;
    struct sembuf semoper, semoper1;
    sigset_t mask;

    newact.sa_handler = handler;
    sigemptyset(&mask);
    // nueva.sa_mask=mask;
    // newact.sa_flags=SA_RESTART;
    sigaction(SIGINT, &newact, &oldact);

    int pid = fork();

    switch (pid) {
        case -1:
            perror("Error en el forking");
            kill(getpid(), SIGINT);  // envía sigint a el mismo proceso que lo
                                     // invoca, e inicia la manejadora
            break;

        case 0:  // hijo
            while (1) {
                printf("*");
            }

            break;

        default:

            semoper.sem_op = -1;
            semoper.sem_num = 0;
            semop(semaforo, &semoper, 0);
            sleep(10);
            // hacemos que haga cosas y si todo ha ido bien lanzamos SIGINT ->
            // LANZARÁ LA MANEJADORA
            kill(getpid(), SIGINT);
            break;
    }
}
void handler(int sig) {
    printf("SIGINT");

    if (semctl(semaforo, 0, IPC_RMID) == 1) {
        printf("Error borrando semaforo;");
    }
    // system("clear");
    exit(3);
}

i'm trying to start a IPC's related project and under my point of view I have to start handling signals. When it's time to press Ctrl+C the handler function starts correctly but in console it appears printed twice.

Another curious situation is that if I change the character to an empty string, the parent process sleeps 10 secs properly and then receives the SIGINT.

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sem.h>
void handler(int);
/*typedef struct {
int semaforo;
//iremos añadiendo mas a medida que necesitemos, pero para pasar todos como un
solo argumento a la función manejadora es más comodo. }ipces;*/
int semaforo;
int main() {
    // ipces ipces;
    semaforo = semget(IPC_PRIVATE, 3, IPC_CREAT | 0600);

    semctl(semaforo, 0, SETVAL, 1);

    struct sigaction newact, oldact;
    struct sembuf semoper, semoper1;
    sigset_t mask;

    newact.sa_handler = handler;
    sigemptyset(&mask);
    // nueva.sa_mask=mask;
    // newact.sa_flags=SA_RESTART;
    sigaction(SIGINT, &newact, &oldact);

    int pid = fork();

    switch (pid) {
        case -1:
            perror("Error en el forking");
            kill(getpid(), SIGINT);  // envía sigint a el mismo proceso que lo
                                     // invoca, e inicia la manejadora
            break;

        case 0:  // hijo
            while (1) {
                printf("*");
            }

            break;

        default:

            semoper.sem_op = -1;
            semoper.sem_num = 0;
            semop(semaforo, &semoper, 0);
            sleep(10);
            // hacemos que haga cosas y si todo ha ido bien lanzamos SIGINT ->
            // LANZARÁ LA MANEJADORA
            kill(getpid(), SIGINT);
            break;
    }
}
void handler(int sig) {
    printf("SIGINT");

    if (semctl(semaforo, 0, IPC_RMID) == 1) {
        printf("Error borrando semaforo;");
    }
    // system("clear");
    exit(3);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文