See the tenth chapter of Apue, said Alarm,pause can achieve sleep, you can let some blocking function timeout, such as Read,write. The code is as follows:
1 Static voidSIG_ALRM (intSigno)2 {3printf"I ' m just print from sig_alrm\n");4 }5 6 intMain (intargcChar*argv[])7 {8 intN;9 Charbuf[ +];Ten One if(Signal (SIGALRM, sig_alrm) = =Sig_err) A { -Perror ("Signal"); -Exit (1); the } - -Alarm3); - if(n = Read (Stdin_fileno, buf, +)) <0 ) + { -Perror ("Read"); + } AAlarm0); at Write (stdout_fileno,buf, n); - return 0; -}
Compile run, found that there is no effect, the program is still stuck in the read, but with the book source can, after comparison found that the compiler parameters are different, my example is the default, but the book used-ansi this parameter to compile, use this parameter to compile my code, the effect, the reason is unclear, It is also not found that the conjecture is the-ansi standard read function block can be interrupted by the signal.
Linux alarm functions unblock read write functions such as block