Handle a signal: Difference between revisions

(Add Fortran example.)
imported>Nmz
 
(3 intermediate revisions by 3 users not shown)
Line 1,068:
wait
</syntaxhighlight>
=={{header|Lua}}==
} * Math.inf;</syntaxhighlight lang="lua">
local start_date = os.time()
 
local loop = true
local Exit = function ()
print()
loop = false
end
 
local posix = require"posix"
posix.signal(posix.SIGINT, Exit)
posix.signal(posix.SIGQUIT, Exit)
 
local int = 0
while loop do
int = int+1
print(int)
posix.time.nanosleep{tv_sec=0,tv_nsec=500*1000*1000}
end
 
print(os.time() - start_date)
</syntaxhighlight>
{{out}}
=={{header|MATLAB}}==
MATLAB versions 6.5 (R13) and newer can no longer catch CTRL+C with a try-catch block. The onCleanup() function was introduced in version 7.6 (R2008a), possibly specifically for this situation. However, the designated onCleanup() function will execute no matter how the function ends (task completion, CTRL+C, exception), and CTRL+C will still cause an exception to be thrown and displayed.
Line 1,291 ⟶ 1,314:
declare(ticks = 1);
 
$start = microtime(YEStrue);
 
function mySigHandler() {
global $start;
$elapsed = microtime(YEStrue) - $start;
echo "Ran for $elapsed seconds.\n";
exit();
Line 1,716 ⟶ 1,739:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var start = Time.sec;
 
 
Sig.INT { |_|
Sys.say( "Ran for #{Time.sec - start} seconds.");
Sys.exit;
}
 
{ |i|
Sys.say(i);
Sys.sleep(0.5);
} * Math.inf;</syntaxhighlight>
 
{ |i|
Sys.say( i);
Sys.sleep(0.5);
} * Inf</syntaxhighlight>
{{out}}
<pre>
Line 2,032 ⟶ 2,054:
=={{header|Wren}}==
Note that Thread.sleep not only suspends the current fiber but also the System.clock method (possibly unintended). We therefore have to add back on the time slept.
<syntaxhighlight lang="ecmascriptwren">import "scheduler" for Scheduler
import "timer" for Timer
import "io" for Stdin
Anonymous user