Linux alarm functions unblock read write functions such as block

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.