Handle a signal: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 267:
Program has run for 1.953 seconds
</pre>
 
=={{header|C sharp}}==
Signals in C# are called events, and are handled by attaching event handler functions to the event, which are called when the event is triggered.
 
<lang csharp>using System; //DateTime, Console, Environment classes
class Program
{
static DateTime start;
static void Main(string[] args)
{
start = DateTime.Now;
//Add event handler for Ctrl+C command
Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
int counter = 0;
while (true)
{
Console.WriteLine(++counter);
System.Threading.Thread.Sleep(500);
}
}
static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
var end = DateTime.Now;
Console.WriteLine("This program ran for {0:000.000} seconds.", (end - start).TotalMilliseconds / 1000);
Environment.Exit(0);
}
}</lang>
 
=={{header|C++}}==
Line 302 ⟶ 329:
 
return 0;
}</lang>
 
=={{header|C sharp}}==
Signals in C# are called events, and are handled by attaching event handler functions to the event, which are called when the event is triggered.
 
<lang csharp>using System; //DateTime, Console, Environment classes
class Program
{
static DateTime start;
static void Main(string[] args)
{
start = DateTime.Now;
//Add event handler for Ctrl+C command
Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
int counter = 0;
while (true)
{
Console.WriteLine(++counter);
System.Threading.Thread.Sleep(500);
}
}
static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
var end = DateTime.Now;
Console.WriteLine("This program ran for {0:000.000} seconds.", (end - start).TotalMilliseconds / 1000);
Environment.Exit(0);
}
}</lang>
 
Line 533:
9
Program has run for 5 secs, 4 ms, 357 ╬╝s, and 4 hnsecs</pre>
 
=={{header|F_Sharp|F#}}==
<lang fsharp>open System
 
let rec loop n = Console.WriteLine( n:int )
Threading.Thread.Sleep( 500 )
loop (n + 1)
 
let main() =
let start = DateTime.Now
Console.CancelKeyPress.Add(
fun _ -> let span = DateTime.Now - start
printfn "Program has run for %.0f seconds" span.TotalSeconds
)
loop 1
 
main()</lang>
 
=={{header|Forth}}==
Line 554 ⟶ 571:
 
main bye</lang>
 
=={{header|F_Sharp|F#}}==
<lang fsharp>open System
 
let rec loop n = Console.WriteLine( n:int )
Threading.Thread.Sleep( 500 )
loop (n + 1)
 
let main() =
let start = DateTime.Now
Console.CancelKeyPress.Add(
fun _ -> let span = DateTime.Now - start
printfn "Program has run for %.0f seconds" span.TotalSeconds
)
loop 1
 
main()</lang>
 
=={{header|Gambas}}==
Line 924:
</pre>
 
=={{Headerheader|Liberty BASIC}}==
Liberty BASIC cannot react to a SigInt signal and truly kill itself. The best it can do is respond to Ctrl-C by exiting normally.
<lang lb>
Line 1,148:
^C interrupted after 5.23734092712402 seconds
</pre>
 
=={{header|Perl 6}}==
We note with glee that the task does not require us to print <em>consecutive</em> integers, so we'll print Fibonacci numbers instead. <tt>:-)</tt>
<lang perl6>signal(SIGINT).tap: {
note "Took { now - INIT now } seconds.";
exit;
}
 
for 0, 1, *+* ... * {
sleep 0.5;
.say;
}</lang>
{{out}}
<pre>0
1
1
2
3
5
8
13
21
34
55
89
^CTook 6.3437449 seconds.
Aborted</pre>
 
=={{header|Phix}}==
Line 1,436 ⟶ 1,409:
Total time: 3.965
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
We note with glee that the task does not require us to print <em>consecutive</em> integers, so we'll print Fibonacci numbers instead. <tt>:-)</tt>
<lang perl6>signal(SIGINT).tap: {
note "Took { now - INIT now } seconds.";
exit;
}
 
for 0, 1, *+* ... * {
sleep 0.5;
.say;
}</lang>
{{out}}
<pre>0
1
1
2
3
5
8
13
21
34
55
89
^CTook 6.3437449 seconds.
Aborted</pre>
 
=={{header|REXX}}==
Line 1,690 ⟶ 1,691:
 
Note also that from 8.5 onwards, Tcl also has other mechanisms for delivering interrupt-like things, such as interpreter resource limits which permit stopping an execution after a set amount of time and returning control to a supervisor module. However, this is not driven by user interrupts and is so only tangential to ''this'' task.
 
=={{header|X86 Assembly}}==
{{works with|NASM|Linux}}<br>
Now, I realize linking to C libraries is somewhat cheating.
It is entirely possible to do this entirely in syscalls using sys_nanosleep/sys_write but that would require allot more work,
definition of the timespec structure among other things.
<lang asm>
%define sys_signal 48
%define SIGINT 2
%define sys_time 13
 
extern usleep
extern printf
 
section .text
global _start
_sig_handler:
mov ebx, end_time
mov eax, sys_time
int 0x80
mov eax, dword [start_time]
mov ebx, dword [end_time]
sub ebx, eax
mov ax, 100
div ebx
push ebx
push p_time
call printf
push 0x1
mov eax, 1
push eax
int 0x80
ret
_start:
mov ebx, start_time
mov eax, sys_time
int 0x80
mov ecx, _sig_handler
mov ebx, SIGINT
mov eax, sys_signal
int 0x80
xor edi, edi
.looper:
push 500000
call usleep
push edi
push p_cnt
call printf
inc edi
jmp .looper
section .data
p_time db "The program has run for %d seconds.",13,10,0
p_cnt db "%d",13,10,0
 
section .bss
start_time resd 1
end_time resd 1
</lang>
 
 
=={{header|TXR}}==
Line 1,885 ⟶ 1,824:
lLoop = .F.
ENDPROC
</lang>
 
=={{header|X86 Assembly}}==
{{works with|NASM|Linux}}<br>
Now, I realize linking to C libraries is somewhat cheating.
It is entirely possible to do this entirely in syscalls using sys_nanosleep/sys_write but that would require allot more work,
definition of the timespec structure among other things.
<lang asm>
%define sys_signal 48
%define SIGINT 2
%define sys_time 13
 
extern usleep
extern printf
 
section .text
global _start
_sig_handler:
mov ebx, end_time
mov eax, sys_time
int 0x80
mov eax, dword [start_time]
mov ebx, dword [end_time]
sub ebx, eax
mov ax, 100
div ebx
push ebx
push p_time
call printf
push 0x1
mov eax, 1
push eax
int 0x80
ret
_start:
mov ebx, start_time
mov eax, sys_time
int 0x80
mov ecx, _sig_handler
mov ebx, SIGINT
mov eax, sys_signal
int 0x80
xor edi, edi
.looper:
push 500000
call usleep
push edi
push p_cnt
call printf
inc edi
jmp .looper
section .data
p_time db "The program has run for %d seconds.",13,10,0
p_cnt db "%d",13,10,0
 
section .bss
start_time resd 1
end_time resd 1
</lang>
 
10,327

edits