简体   繁体   English

得到“用户信号1”以终止我的进程,在C中为UNIX

[英]getting “User signal 1” that terminates my process, UNIX in C

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();
}

I have this signal handler that handles SIGUSR1 signals. 我有一个处理SIGUSR1信号的信号处理程序。
boardString is an array of 16 ints in string format, with ',' as delimiters. boardString是由16个整数组成的数组,其字符串格式,以','作为分隔符。
The printToScreen() just prints it in a 4X4 matrix format printToScreen()仅以4X4矩阵格式打印
I have a second process that sends SIGUSR1 to this process after each update in the boardString. 我有第二个进程,它在boardString中的每次更新之后将SIGUSR1发送到该进程。
My problem is that the printToScreen function doesn't end. 我的问题是printToScreen函数没有结束。 it prints the first iteration just fine and then I get this msg "User signal 1" and that's when my program terminates. 它会很好地打印出第一个迭代,然后我得到此味精“用户信号1”,这就是我的程序终止的时间。
I can't understand why it terminates and what "User signal 1" means. 我不明白为什么会终止以及“用户信号1”的含义。

From the description, it sounds like you're setting up the signal handler as a single-use handler (so it is called the first time the signal is received, and is then reset to the default action, which is to kill the process; the shell will then generally print the "User signal 1" message). 从描述中,听起来好像您将信号处理程序设置为一次性使用的处理程序(因此,在第一次接收到信号时调用它,然后将其重置为默认操作,即终止该过程;然后,外壳程序通常会打印“用户信号1”消息)。

How are you setting the signal handler? 您如何设置信号处理程序? If you are using signal and are using a SysV-derived unix variant, the default will be single use. 如果您正在使用signal并且正在使用SysV派生的unix变体,则默认值为一次性使用。 Use sigaction instead, and make sure the SA_RESETHAND flags is not set. 请改用sigaction ,并确保设置SA_RESETHAND标志。

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM