Test sending semaphore request interrupt response C

Source: Internet
Author: User

This is a very interesting program. For some programmers who have learned the operating system, this program will be preferred, because it can reflect the programmer's understanding of the semaphore mechanism.

Operation: After compiling and running with VC or VS, "waitting for something..." will always be printed. When you press Ctrl + C, "getsignal = 2" will appear. And keep repeating. (PS: Ctrl + c is the end operation of the console program, I believe everyone knows about it) the following code:

Code:

1 # include <stdio. h> 2 # include <signal. h> 3 # include <windows. h> 4 5 void fun (INT s) 6 {7 fprintf (stderr, "getsignal = % d \ n", S); 8 signal (SIGINT, fun ); // send semaphore 9} 10 11 int main () 12 {13 if (signal (SIGINT, fun) <0) // If the semaphore fails to be sent 14 {15 perror ("signal error \ n"); 16 return 1; 17} 18 19 while (1) // test the sending status of the semaphore: 20 {21 printf ("wait for something... \ n "); 22 sleep (500); // sleep for 0.5 seconds 23} 24 25 return 0; 26}

 

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.