简体   繁体   English

C linux信号和功能

[英]C linux signals and functions

I got this issue, which I will simplify below: 我遇到了这个问题,下面将对其进行简化:

#include <stdio.h>
#include <signal.h>

int main(void) {
    signal(SIGALRM, &INThandler);

    //get menu options which Im not going to put here
    game(...stuff...);
}

void game(..stuff...) {
    //do the game stuff AND set an alarm()
}

void INThandler(int sig) {
    system("clear");
    printf("Time is up!\n");
    //I WANT game() TO STOP WHICH WILL EXIT TO MAIN WHERE MORE STUFF IS HAPPENING
}

In game() I have 在game()中,我有

while(counter <= amount)

So I wanted to pass the variables counter and amount into INThandler so I could change them so the condition is false, however INThandler is only called when the alarm is at 0 and is not called with parameters. 因此,我想将变量计数器和数量传递给INThandler,以便可以更改它们,以便条件为假,但是仅在警报为0时才调用INThandler,而不使用参数来调用。 game() continues and I don't want it to. game()继续,但我不想这么做。 If there is a better way please tell me. 如果有更好的方法,请告诉我。

使用全局变量作为计数器和数量?

When a function is called and that function has variables in it, those variables are allocated on the stack. 当调用一个函数并且该函数中包含变量时,这些变量将分配在堆栈上。 If you define a global variable, it will be instead be allocated as the program loads. 如果定义全局变量,则将在程序加载时分配它。 Your signal handler should have access to those variables. 您的信号处理程序应有权访问这些变量。

#include <stdio.h>
#include <signal.h>
#include <stdlib.h> //Also include this, needed for exit(returncode)

int counter; //Not inside any function
int amount;  //All functions may access these

int main(void) {
    signal(SIGALRM, &INThandler); 

    //get menu options which Im not going to put here
    game(...stuff...);
}

void game(..stuff...) {
    //do the game stuff AND set an alarm()
}

void INThandler(int sig) {
    //Do stuff with counter and amount
    //system("clear"); I recommend that you do not use system to clear the screen, system(command) is inefficient.
    printf("\033[H\033[JTime is up!\n");
    //Do that extra stuff you want to do in main here, then
    exit(0);
}

Another note: according to signal(2) in the Linux programming manual: 另一个注意事项:根据Linux编程手册中的signal(2):

The only portable use of signal() is to set a signal's disposition to SIG_DFL or SIG_IGN. signal()的唯一可移植用法是将信号的处置方式设置为SIG_DFL或SIG_IGN。 The semantics when using signal() to establish a signal handler vary across systems (and POSIX.1 explicitly permits this variation); 使用signal()建立信号处理程序时的语义在系统之间有所不同(而POSIX.1明确允许这种变化); do not use it for this purpose. 请勿将其用于此目的。

POSIX.1 solved the portability mess by specifying sigaction(2), which provides explicit control of the semantics when a signal handler is invoked; POSIX.1通过指定sigaction(2)解决了可移植性问题,当调用信号处理程序时,它提供了对语义的显式控制。 use that interface instead of signal(). 使用该接口而不是signal()。

To register a signal handler using sigaction, 要使用sigaction注册信号处理程序,

#include <signal.h>

int main(){
    const struct sigaction saSIGALRM = {
        .sa_handler = mySignalHandler, //replace this with your signal handler, it takes the same parameters as using signal()
    };
    sigaction(SIGALRM, &saSIGALRM, 0);
}

It's simpler than it looks. 它比看起来简单。 Remember, computers are slow today because of inefficient programming. 请记住,由于编程效率低下,今天的计算机速度很慢。 Please, please, please, for efficient programs, use this instead. 请,请,请使用高效的程序。

Click here for more cool things sigaction can do, along with why not to use signal() 点击这里查看更多很酷的事情的sigaction可以做,以为什么不使用信号沿()

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

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