首页 文章

获得“用户信号1”终止我的进程,UNIX中的C语言

提问于
浏览
0
void printToScreen(){
    write(1, boardString, strlen(boardString)) == -1 ? writeError() : 1;
    write(1, "\n", 1) == -1 ? writeError() : 1;

    int i = 0;
    char * pch;
    pch = strtok (boardString, ",");
    int len = strlen(pch);
    int count=0;
    // insert the number and pads it with zeros, '|' and spaces
    while (pch != NULL){
        printf("\niteration %d\n", count++);

        switch(len){
        case 1:
            if(*pch == '0'){
                write(1, "|      ", 7) == -1 ? writeError() : 1;
                break;
            }
            write(1, "| 000", 5) == -1 ? writeError() : 1;
            write(1, pch, 1) == -1 ? writeError() : 1;
            write(1, " ", 1) == -1 ? writeError() : 1;      
            break;
        case 2:
            write(1, "| 00", 4) == -1 ? writeError() : 1;
            write(1, pch, 2) == -1 ? writeError() : 1;
            write(1, " ", 1) == -1 ? writeError() : 1;  
            break;          
        case 3:
            write(1, "| 0", 3) == -1 ? writeError() : 1;
            write(1, pch, 3) == -1 ? writeError() : 1;
            write(1, " ", 1) == -1 ? writeError() : 1;  
            break;              
        case 4:
            write(1, "| ", 2) == -1 ? writeError() : 1;
            write(1, pch, 3) == -1 ? writeError() : 1;
            write(1, " ", 1) == -1 ? writeError() : 1;  
            break;
        default:
            break;
        }

        // update pch, len
        pch = strtok (NULL, ",");
        len = strlen(pch);
        i++;    
        // move to next line
        if (i % 4 == 0)
            write(1, "|\n", 2) == -1 ? writeError() : 1;    
    }
}

    void sig_hand(int sig){
    printf("got signal\n");
    read(STDIN, boardString, STRING_SIZE) == -1 ? readError() : 1;
    printToScreen();
}

我有这个处理SIGUSR1信号的信号处理程序 .
boardString是一个16字节的数组,字符串格式,','作为分隔符 .
printToScreen()只是以4X4矩阵格式打印
我有第二个进程,在boardString中的每次更新后将SIGUSR1发送到此进程 .
我的问题是我的程序终止时printToScreen函数没有't end. it prints the first iteration just fine and then I get this msg 2651326 and that' .
我无法理解为什么它会终止,以及"User signal 1"的意思 .

2 回答

  • 2

    从描述中可以看出,您将信号处理程序设置为一次性处理程序(因此在第一次接收到信号时调用它,然后将其重置为默认操作,即终止进程;然后,shell通常会打印“用户信号1”消息 .

    你是如何设置信号处理程序的?如果您使用的是 signal 且正在使用SysV派生的unix变体,则默认为单次使用 . 请改用 sigaction ,并确保未设置 SA_RESETHAND 标志 .

  • 1
    to start, there are other error return codes besides -1
    and write() normally returns the number of chars written
    and "\n" can be (depending on OS) more than one char
    so the first two lines would be better written as:
    
    (write(1, boardString, strlen(boardString)) < 0) ? writeError() : strlen(boardString);
    (write(1, "\n", sizeof("\n") ) < 0) ? writeError() : sizeof("\n");
    
    The follow is an example for handling a signal (note: kill and stop cannot be handled)
    caveat: I have not run the following, so it may have keypunch errors.
    
    #include<stdio.h>
    #include<signal.h>
    #include<unistd.h>
    
    // handle the signal
    void sig_handler(int signo)
    {
        if (signo == SIGUSR1)  printToScreen();
    }
    
    // register the signal handler
    typedef void sigfunc(int)
    
    sigfunc* = signal(SIGUSR1, (sigfunc*)sig_handler(int));
    

相关问题