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

时间:2014-05-19 19:23:27

标签: c unix signals

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功能没有结束。它打印第一次迭代就好了然后我得到了这个消息#34;用户信号1"当我的程序终止时。我无法理解为什么它会终止以及用户信号1"手段。

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分: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));