Alarm function on Linux and Windows & # 8594; cannot find equivalent for Windows & # 8594; C

I worked for some time on Linux and did some C programs, and now I need to create a Windows application, but it is not easy to find a replacement for the alarm function (found on signal.h) ..

The fact is that in Linux, when you set the alarm flag as SIGALRM (if you are not mistaken), what the OS is going to do is to perform some function when the alarm goes off. Example:

signal(SIGALRM, myfunc); alarm (2); 

What will happen in this example is that the OS will call the myfunc function every 2 seconds (there will probably be milliseconds on Windows, but don’t pay attention to it right now).

I was looking for some time how to do the same in windows (only using windows.h atm), but I can not find a replacement for this (thousands of times on msdn), and on the windows there is no SIGALRM sign on the signal. h .....

What can I use in C to perform the same behavior as an alarm with the SIGALRM flag? (In other words, a function that allows you to perform another function with an alarm or similar).

+4
source share
2 answers

Check the MSDN for timeSetEvent . This is not exactly the same as alarm , but it can be close enough.

+3
source

Using alarm / <T21> is not a safe way to "start a function at a given time," because the function is called in the context of signal processing, where using almost any library function is unsafe. Indeed, only a good application I know for SIGALRM sets up a handler doing nothing with sigaction to interrupt system calls, so the system call you expect can hang interrupted with EINTR .

If you want to complete the task N seconds now, the safest way to do this is to start a new thread that sleeps N seconds and then performs the task. This should work on any streaming operating system.

Windows also has its own kind of timer events that can be delivered through the message box system, but using them will make your code much more Windows-oriented.

+3
source

Source: https://habr.com/ru/post/1333074/


All Articles