简体   繁体   English

从信号处理程序返回

[英]Returning from Signal Handlers

Am I not leaving my signal handler function in the correct way? 我没有以正确的方式离开我的信号处理函数吗? It does not seem to return to the program normally. 它似乎没有正常返回程序。 Instead it goes into the loop and where it should wait for user input, it skips and reads the length of the "user input" to -1 and errors out. 相反,它进入循环,它应该等待用户输入,它跳过并读取“用户输入”的长度为-1并出错。 (Will make more sense in code.) (在代码中更有意义。)

void handle_SIGINT() {

    int k = recent;
    int count = 0;
    int stop;

    if (stringSize >= 10) {
        stop = 10;
    }
    else {
        stop = p;
    }

    printf("\nCommand History:\n");

    for (count = 0; count < stop; count++) {

        if (k < 0) {
            k += 10;
        }
        printf("%s", string[abs(k)]);
        k -= 1;

    }

}



void setup(char inputBuffer[], char *args[],int *background)
{
    //char inputBuffer[MAX_LINE];
    int length, /* # of characters in the command line */
    i,      /* loop index for accessing inputBuffer array */
    start,  /* index where beginning of next command parameter is */
    ct;     /* index of where to place the next parameter into args[] */
    int add = 1;
    ct = 0;

    /* read what the user enters on the command line */
    length = read(STDIN_FILENO, inputBuffer, MAX_LINE);  


        printf("%i",length);
    start = -1;
    if (length == 0)
        exit(0);            /* ^d was entered, end of user command stream */
    if (length < 0){
        perror("error reading the commanddddddddd");
        exit(-1);           /* terminate with error code of -1 */
    }
}



int main(void)
{
    char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
    int background;             /* equals 1 if a command is followed by '&' */
    char *args[MAX_LINE/2+1];/* command line (of 80) has max of 40 arguments */
    FILE *inFile = fopen("pateljay.history", "r");



    if (inFile != NULL) {
        int count = 0;
        char line[MAX_LINE];
        while (fgets(line,sizeof line, inFile) != NULL) {
            string[count] = strdup(line);
            //string[count][strlen(line)] = '\n';
            //string[count][strlen(line) + 1] = '\0';
            printf("%s", string[count]);
            count++;
            stringSize++;
        }


            p = count % 10;
            recent = abs(p - 1);

    }   

    fclose(inFile); 

    /* set up the signal handler */
    struct sigaction handler;
    handler.sa_handler = handle_SIGINT;
    sigaction(SIGINT, &handler, NULL);

    while (1) {/* Program terminates normally inside setup */


        background = 0;
        printf("COMMAND->");

        fflush(0);

        setup(inputBuffer, args, &background);/* get next command */
    }
}

So when ctrl+c is entered it should catch the signal and handle it. 因此,当输入ctrl + c时,它应该捕获信号并处理它。 Once it returns back to main, it goes into setup and completely skips the read function and makes length equal to -1. 一旦它返回到main,它进入设置并完全跳过读取函数并使长度等于-1。 This in turn errors out the program. 这反过来又误解了程序。 I think the code inside handle_SIGINT is irrelevant as it is right now. 我认为handle_SIGINT中的代码与现在无关。 Does anyone know any reason why it would be skipping the read function in setup? 有谁知道为什么它会跳过设置中的读取功能?

read is blocking, waiting for input. read阻塞,等待输入。 SIGINT arrives. SIGINT到了。 The kernel calls your signal handler. 内核调用你的信号处理程序。 When your signal handler returns, the kernel makes read return -1 and set errno to EINTR . 当您的信号处理程序返回时,内核使read返回-1并将errnoEINTR You need to check for this case and handle it by calling read again: 您需要检查此案例并通过再次调用read处理它:

   do {
        length = read(STDIN_FILENO, inputBuffer, MAX_LINE);
   } while (length == -1 && errno == EINTR);

信号处理程序应该采用int参数:

void handle_sigint(int signum) {}

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

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