in reply to Strawberry Perl and alarm() on Windows
Also works in Strawberry 5.14.2 on Win7-64.
alarm says: Emulated using timers that must be explicitly polled whenever Perl wants to dispatch "safe signals" and therefore cannot interrupt blocking system calls. (Win32). And sleep says: Emulated using synchronization functions such that it can be interrupted by alarm(), and limited to a maximum of 4294967 seconds, approximately 49 days. (Win32)
So, yes, mixing alarm and sleep works on Win32. Replace sleep with something that blocks for 5 seconds outside perl (i.e. in the system) and alarm will no longer work.
Alexander
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Strawberry Perl and alarm() on Windows
by SimonPratt (Friar) on May 22, 2015 at 16:52 UTC | |
Uhh, am I missing something?
Output: Fri May 22 17:46:53 2015 Alarm appears to work just fine even when calling out to system | [reply] [d/l] |
by afoken (Chancellor) on May 24, 2015 at 09:21 UTC | |
am I missing something? Yes. I did not write about the system function, but about the system (as in "operating system"). I cited alarm in perlport: "Emulated using timers that must be explicitly polled whenever Perl wants to dispatch "safe signals" and therefore cannot interrupt blocking system calls. (Win32)" (Emphasis mine) I wrote " Replace sleep with something that blocks for 5 seconds outside perl (i.e. in the system) and alarm will no longer work." Demo, using flock, a function using a system call that may block for a long time:
How the demo works: Without arguments, main() is invoked, main() creates a background process (using system(1,@args) on Windows, fork() and exec() on Linux), waits a second, then tries to lock a temp file with a classic timeout construct (eval, $SIG{'ALRM'}=sub { die }, alarm($timeout), system call, alarm(0)). Time for this is measured. The background process is the same script, but invoked with an argument, so that locker() is invoked instead of main(). Locker locks the temp file for 10 seconds, and because main() waits a second, it will succeed. In main(), the temp file is already locked, so flock() will block until locker() releases the lock OR the system call is interrupted by the ALRM signal caused by alarm(). On Linux, signals just work. flock() in main() is interrupted by the ARLM signal, the signal handler in $SIG{'ALRM'} is invoked, that terminales the eval block using die() after 5 seconds. On Windows, signals are emulated. alarm($timeout) sets up a timer that must be polled. This is impossible during a blocking system call like flock(). So flock() blocks until it can aquire the lock after about 9 seconds (locker() waits 10 seconds after locking, main() waits 1 second before trying to lock, 9 seconds remain). $SIG{'ALRM'} is not invoked. alarm(0) disables the timer. The eval block terminates without an error. Output on Linux:
Output on Windows:
So why did alarm($timeout) "work" with system "sleep 10"? system @args (unlike system 1,@args) waits for the sub-process to terminate in perl. (flock() waits in the operating system!) During that time, perl can poll the timer and thus can emulate the ARLM signal. Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-) | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on May 24, 2015 at 10:55 UTC | |
On Windows, signals are emulated. alarm($timeout) sets up a timer that must be polled. That's not a very good description of what actually happens. On Windows, alarm is simulated using the SetTimer() system API, which send a message to the process' message queue when the time expires. From perl5.18/Win32.c: And that message is received when the run loop checks the message queue:
And, at least since the advent of SAFE_SIGNALS in 5.8.1, *nix perls, also only process signals, between opcodes, by "polling" (from the run loop) to see if any have been received during the last opcode. Ie. in exactly the same way as Windows perls do. The reason your rather over-elaborate demo "works" on *nix and not under windows, is because under *nix, signals are also delivered (by the OS) to child processes, whereas they are obviously not on Windows. Thus, the locker child process also receives the alrm signal which interrupts the 10 second sleep, thus the lock it holds gets removed, and the main() process can therefore acquire its lock before the alarm is triggered. This is clearly shown by the output you posted:
Perl's "Safe Signals" (unless disabled) are also implemented on *nix perls, and are only acted upon between opcodes, just as they are on Windows perls. Your demo is deceptive (I'm not suggesting deliberately so), because the alarm is not interrupting the flock opcode in the main() process, but the sleep in the child process. The lock is only acquired early in the main process because the child process relinquished its lock early. That doesn't happen in the Windows example, because the OS doesn't do signals, thus doesn't deliver the signal to the child process. I agree that the emulated signals on Windows are less complete than the real ones on *nix; but then the reason Perl has "Safe signals" in the first place is because the entire concept of using asynchronous interrupts as a flow-control mechanism is terminally brain-dead. With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
| [reply] [d/l] [select] |
by afoken (Chancellor) on May 24, 2015 at 13:15 UTC | |
by BrowserUk (Patriarch) on May 24, 2015 at 15:05 UTC | |
| |