Handle a signal: Difference between revisions

m (syntax highlighting fixup automation)
imported>Nmz
 
(4 intermediate revisions by 4 users not shown)
Line 598:
main bye</syntaxhighlight>
 
=={{header|Fortran}}==
{{Works with|gfortran}}
Must be compiled with the <code>-fcoarray=single</code> flag to enable use of atomic operations.
<syntaxhighlight lang="fortran">program signal_handling
use, intrinsic :: iso_fortran_env, only: atomic_logical_kind
implicit none
 
interface
integer(C_INT) function usleep(microseconds) bind(c)
use, intrinsic :: iso_c_binding, only: C_INT, C_INT32_T
integer(C_INT32_T), value :: microseconds
end function usleep
end interface
 
integer, parameter :: half_second = 500000
integer, parameter :: sigint = 2
integer, parameter :: sigquit = 3
 
logical(atomic_logical_kind) :: interrupt_received[*]
integer :: half_seconds
logical :: interrupt_received_ref
 
interrupt_received = .false.
half_seconds = 0
 
! "Install" the same signal handler for both SIGINT and SIGQUIT.
call signal(sigint, signal_handler)
call signal(sigquit, signal_handler)
 
! Indefinite loop (until one of the two signals are received).
do
if (usleep(half_second) == -1) &
print *, "Call to usleep interrupted."
 
call atomic_ref(interrupt_received_ref, interrupt_received)
if (interrupt_received_ref) then
print "(A,I0,A)", "Program ran for ", half_seconds / 2, " second(s)."
stop
end if
 
half_seconds = half_seconds + 1
print "(I0)", half_seconds
end do
 
contains
 
subroutine signal_handler(sig_num)
use, intrinsic :: iso_c_binding, only: C_INT
integer(C_INT), value, intent(in) :: sig_num
! Must be declared with attribute `value` to force pass-by-value semantics
! (what C uses by default).
 
select case (sig_num)
case (sigint)
print *, "Received SIGINT."
case (sigquit)
print *, "Received SIGQUIT."
end select
 
call atomic_define(interrupt_received, .true.)
end subroutine signal_handler
 
}end *program Math.inf;signal_handling</syntaxhighlight>
 
=={{header|FreeBASIC}}==
Line 1,005 ⟶ 1,068:
wait
</syntaxhighlight>
=={{header|Lua}}==
<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,228 ⟶ 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,653 ⟶ 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 1,969 ⟶ 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