代码之家  ›  专栏  ›  技术社区  ›  Searock

sigaction()中第二个结构(*oldact)的用法是什么

  •  2
  • Searock  · 技术社区  · 16 年前

    int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
    

    这是我的密码

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    
    void CustomHandler(int signo)
    {
        printf("Inside custom handler");
        switch(signo)
        {
        case SIGFPE:
            printf("ERROR: Illegal arithmatic operation.\n");
            break;
    
        }
    
        exit(signo);
    }
    
    void newCustomHandler(int signo)
    {
        printf("Inside new custom handler");
        switch(signo)
        {
        case SIGINT:
            printf("ERROR: Illegal arithmatic operation.\n");
            break;
    
        }
    
        exit(signo);
    }
    
    int main(void)
    {
        long value;
        int i;
        struct sigaction act = {CustomHandler};
        struct sigaction newact = {newCustomHandler};
    
    
        newact = act;
        sigaction(SIGINT, &newact, NULL); //whats the difference between this
    
        /*sigaction(SIGINT, &act, NULL); // and this?
        sigaction(SIGINT, NULL, &newact);*/
    
    
        for(i = 0; i < 5; i++)
        {
            printf("Value: ");
            scanf("%ld", &value);
            printf("Result = %ld\n", 2520 / value);
        }
    }
    

    现在,当我运行程序并按Ctrl+c时,它显示在自定义处理程序中。

    我读过sigaction的文档,上面写着

    如果act为非null,则 如果oldact不为空,则前一个 动作保存在旧动作中。

    当我可以直接赋值时,为什么需要传递第二个结构

    newact = act
    

    谢谢。

    2 回复  |  直到 16 年前
        1
  •  5
  •   Sjoerd    16 年前

    oldact

    sigaction(SIGINT, &copyInterrupted, &previousHandler);
    copy(something);
    sigaction(SIGINT, &previousHandler, null);
    

    这样,即使不知道以前的信号处理程序是什么,也可以重置它。

        2
  •  1
  •   Norman Gray    16 年前

    当你打电话的时候 sigaction ,你 旧的处理者和新的处理者;新的处理者 oldact 指针设置为指向此替换之前有效的处理程序信息。

    信号 ,在这个区域的尽头。