September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -10,13 +10,12 @@
{{omit from|Unlambda|Does not handle signals.}}
{{omit from|XSLT}}
Most general purpose operating systems provide interrupt facilities, sometimes called signals.
Most operating systems provide interrupt facilities, sometimes called signals either generated by the user or as a result of program failure or reaching a limit like file space.
Unhandled signals generally terminate a program in a disorderly manner.
Signal handlers are created so that the program behaves in a well-defined manner upon receipt of a signal.
;Task:
Provide a program that displays a single integer on each line of output at the rate of one integer in each half second.
Upon receipt of the SigInt signal (often created by the user typing ctrl-C) the program will cease printing integers to its output, print the number of seconds the program has run, and then the program will terminate.
Provide a program that displays an integer on each line of output at the rate of about one per half second.
<!-- some systems have difficulty with 1/2 second and that's not the point of this subject anyway DG-->
Upon receipt of the SIGINT signal (often generated by the user typing ctrl-C <!-- on unix see stty -a . on windows SIGBREAK DG -->( or better yet, SIGQUIT ctrl-\ )) the program will cease outputting integers, output the number of seconds the program has run, and then the program will quit. <!-- outputting the number if seconds is also unduly complicated for this topic . Hope nobody minds these comments and I didn't really want to change the task since there were so many examples already written. see the Perl example for a more extensive use of signals. PS See discussion. If you find these edits inappropriate let me know DG-->
<br><br>

View file

@ -0,0 +1,14 @@
' Handle signal
SUB Finished
SIGNAL SIG_DFL, SIGINT : ' Restore SIGINT to default
PRINT "Running for", TIMER / 1000.0, "seconds" FORMAT "%s %f %s\n"
STOP SIGINT : ' Send another terminating SIGINT
ENDSUB
SIGNAL Finished, SIGINT
iter = 1
WHILE TRUE
SLEEP 500
PRINT iter
iter = iter + 1
WEND

View file

@ -0,0 +1,31 @@
hTimer As Timer
fTime As Float
Public Sub Application_Signal(x As Integer)
Print "Program stopped after " & fTime & " seconds"
Quit
End
Public Sub Main()
hTimer = New Timer As "IntTimer"
Print "Press [Ctrl] + " & Chr(92) & " to stop"
Signal[Signal.SIGQUIT].Catch
With hTimer
.Delay = 500
.Start
End With
End
Public Sub IntTimer_Timer()
Print Rand(0, 100)
fTime += 0.5
End

View file

@ -0,0 +1,22 @@
// version 1.1.3
import sun.misc.Signal
import sun.misc.SignalHandler
fun main(args: Array<String>) {
val startTime = System.currentTimeMillis()
Signal.handle(Signal("INT"), object : SignalHandler {
override fun handle(sig: Signal) {
val elapsedTime = (System.currentTimeMillis() - startTime) / 1000.0
println("\nThe program has run for $elapsedTime seconds")
System.exit(0)
}
})
var i = 0
while(true) {
println(i++)
Thread.sleep(500)
}
}

View file

@ -1,8 +0,0 @@
my $start = time;
$SIG{INT} = sub
{print 'Ran for ', time - $start, " seconds.\n";
exit;};
for (my $n = 0 ;; select(undef, undef, undef, .5))
{print ++$n, "\n";}

View file

@ -1,9 +0,0 @@
use 5.010;
use AnyEvent;
my $start = AE::time;
my $exit = AE::cv;
my $int = AE::signal 'INT', $exit;
my $n;
my $num = AE::timer 0, 0.5, sub { say $n++ };
$exit->recv;
say " interrupted after ", AE::time - $start, " seconds";

View file

@ -0,0 +1,63 @@
my $start = time; # seconds since epohc
my $arlm=5; # every 5 seconds show how we're doing
my $i;
$SIG{QUIT} = sub
{print " Ran for ", time - $start, " seconds.\n"; die; };
$SIG{INT} = sub
{print " Running for ", time - $start, " seconds.\n"; };
$SIG{ALRM} = sub
{print " After $arlm seconds i= $i. Executing for ", time - $start, " seconds.\n"; alarm $arlm };
alarm $arlm; # trigger ALaRM after we've run for a while
print " ^C to inerrupt, ^\\ to quit, takes a break at $arlm seconds \n";
while ( 1 ) {
for ( $w=11935000; $w--; $w>0 ){}; # spinning is bad, but hey it's only a demo
print ( ++$i," \n");
}
^C to inerrupt, ^\ to quit, takes a break at 5 seconds
1
2
^C Running for 1 seconds.
3
4
^C Running for 2 seconds.
5
6
7
^C Running for 3 seconds.
8
9
10
After 5 seconds i= 10. Executing for 5 seconds.
11
12
13
14
15
16
17
18
19
20
After 5 seconds i= 20. Executing for 10 seconds.
21
22
^\ Ran for 11 seconds.
Died at 0.pl line 6..
This example does the required task:
<lang perl>use 5.010;
use AnyEvent;
my $start = AE::time;
my $exit = AE::cv;
my $int = AE::signal 'INT', $exit;
my $n;
my $num = AE::timer 0, 0.5, sub { say $n++ };
$exit->recv;
say " interrupted after ", AE::time - $start, " seconds";

View file

@ -0,0 +1,3 @@
var t=Time.Clock.time;
try{ n:=0; while(1){(n+=1).println(); Atomic.sleep(0.5)} }
catch{ println("ran for ",Time.Clock.time-t," seconds"); System.exit() }