0
我有這樣的代碼:鍵盤信號處理,添加參數回調處理函數(Ubuntu的,英特爾)
#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;
}
謝謝
使用一般的變量?你能更具體嗎? – mabounassif 2010-10-29 16:26:21
當然,@Mahmoud,看到我的更新。 – paxdiablo 2010-10-29 16:30:13
即使如此,共享變量應該是'sig_atomic_t'類型,或者您應該在適當的時候仔細阻止關鍵部分周圍的信號(相對於程序和信號處理程序之間共享數據的變化)。請參閱GNU C庫文檔中的[「信號處理」](http://www.gnu.org/s/libc/manual/html_node/Atomic-Data-Access.html#Atomic-Data-Access)。 – 2010-10-29 16:34:02