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}