键盘信号处理,向回调处理函数添加参数(Ubuntu、intel)

发布于 2024-09-29 08:59:53 字数 1156 浏览 0 评论 0原文

我有这段代码:

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

// Define the function to be called when ctrl-c (SIGINT) signal is sent to process
void
signal_callback_handler(int signum)
{
   printf("Caught signal %d\n",signum);
   // Cleanup and close up stuff here

   // Terminate program
   exit(signum);
}

int main()
{
   // Register signal and signal handler
   signal(SIGINT, signal_callback_handler);

   while(1)
   {
      printf("Program processing stuff here.\n");
      sleep(1);
   }
   return EXIT_SUCCESS;
}

有没有办法在回调函数中传递额外的参数? 比如:

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

// Define the function to be called when ctrl-c (SIGINT) signal is sent to process
void
signal_callback_handler(int signum, int k)
{

   k++; // Changing value of k
}

int main()
{
   int k = 0;
   // Register signal and signal handler
   signal(SIGINT, signal_callback_handler(k);

   while(1)
   {
      printf("Program processing stuff here.\n");
      printf(" blah %d\n", k);
      sleep(1);
   }
   return EXIT_SUCCESS;
}

谢谢你

I have this code:

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

// Define the function to be called when ctrl-c (SIGINT) signal is sent to process
void
signal_callback_handler(int signum)
{
   printf("Caught signal %d\n",signum);
   // Cleanup and close up stuff here

   // Terminate program
   exit(signum);
}

int main()
{
   // Register signal and signal handler
   signal(SIGINT, signal_callback_handler);

   while(1)
   {
      printf("Program processing stuff here.\n");
      sleep(1);
   }
   return EXIT_SUCCESS;
}

Is there a way to pass an extra argument in the callback function?
Something like:

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

// Define the function to be called when ctrl-c (SIGINT) signal is sent to process
void
signal_callback_handler(int signum, int k)
{

   k++; // Changing value of k
}

int main()
{
   int k = 0;
   // Register signal and signal handler
   signal(SIGINT, signal_callback_handler(k);

   while(1)
   {
      printf("Program processing stuff here.\n");
      printf(" blah %d\n", k);
      sleep(1);
   }
   return EXIT_SUCCESS;
}

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛上残留的泪 2024-10-06 08:59:53

不,没有,而且无论如何,您实际上应该在信号处理程序中做的事情很少。

我通常只是设置一个标志并返回,让真正的代码从那时起处理它。

如果您确实想这样做,您可以将 k 设为文件级静态,以便 main 和信号处理程序(以及文件中的所有其他函数)都可以访问它,但您可能想研究该选项的安全性(信号处理程序是否可以在实际程序使用或更新该值时运行)。

换句话说,类似:

static int k = 0;

void signal_callback_handler(int signum) {
   k++; // Changing value of k
}

int main() {
   // Register signal and signal handler
   signal(SIGINT, signal_callback_handler);
   : :

No, there's not, and there's very little that you should actually be doing in a signal handler anyway.

I generally just set a flag and return, letting the real code handle it from then on.

If you really wanted to do that, you could make k a file-level static so that both main and the signal handler (and every other function in the file) could access it, but you might want to investigate the safety of that option (whether a signal handler can run while the real program is using or updating the value).

In other words, something like:

static int k = 0;

void signal_callback_handler(int signum) {
   k++; // Changing value of k
}

int main() {
   // Register signal and signal handler
   signal(SIGINT, signal_callback_handler);
   : :
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文