我个人讨厌这类问题,但我被困住了。我有一个正在尝试在Ubuntu 12.4上运行的程序 (内核:Linux 3.2.0-39-generic-pae #62-Ubuntu SMP i686 i686 i386 GNU/Linux;编译器:gcc(Ubuntu/Linaro 4.6.3- 1ubuntu5)4.6.3):
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
/*
* sleep1.c
* purpose show how sleep works
* usage sleep1
* info sets handler, sets alarm, pauses, then returns
*/
int main()
{
void onbell(int);
printf("about to sleep for 4 seconds\n");
signal(SIGALRM, onbell); /* catch it */
alarm(4); /* set clock */
pause(); /* do nothing */
printf("Morning so soon?\n"); /* back to work */
return 0;
}
void
onbell(int s)
{
printf("Alarm received from kernel\n");
}
问题是,SIGALRM永远不会通过。如果我在上面运行strace,最后三行是:
rt_sigaction(SIGALRM, {0x80484c1, [ALRM], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
alarm(4) = 0
pause(
暂停永远不会回来。当我在使用内核报告自身的系统上运行相同的代码时:Linux 2.6.24-32-generic i686 GNU/Linux;编译器:gcc (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu4)使用 strace 运行的相同代码显示:
rt_sigaction(SIGALRM, {0x8048470, [ALRM], SA_RESTART}, {SIG_DFL}, 8) = 0
alarm(4) = 0
pause() = ? ERESTARTNOHAND (To be restarted)
--- SIGALRM (Alarm clock) @ 0 (0) ---
write(1, "Alarm received from kernel\n", 27Alarm received from kernel
) = 27
知道为什么在Ubuntu 12.4系统上从未收到警报,或者可以做些什么来弄清楚发生了什么?