Sleep: Difference between revisions
Content added Content deleted
(C) |
No edit summary |
||
Line 11: | Line 11: | ||
The Ada delay statement takes an argument of type Duration, which is a real number counting the number of seconds to delay. Thus, 2.0 will delay 2.0 seconds, while 0.001 will delay 0.001 seconds. |
The Ada delay statement takes an argument of type Duration, which is a real number counting the number of seconds to delay. Thus, 2.0 will delay 2.0 seconds, while 0.001 will delay 0.001 seconds. |
||
<ada>with Ada.Text_Io; use Ada.Text_Io; |
<lang ada>with Ada.Text_Io; use Ada.Text_Io; |
||
with Ada.Float_Text_Io; use Ada.Float_Text_Io; |
with Ada.Float_Text_Io; use Ada.Float_Text_Io; |
||
Line 21: | Line 21: | ||
delay Duration(In_Val); |
delay Duration(In_Val); |
||
Put_Line("Awake!"); |
Put_Line("Awake!"); |
||
end Sleep;</ |
end Sleep;</lang> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
{{works with|QuickBasic|4.5}} |
{{works with|QuickBasic|4.5}} |
||
<qbasic>INPUT sec 'the SLEEP command takes seconds |
<lang qbasic>INPUT sec 'the SLEEP command takes seconds |
||
PRINT "Sleeping..." |
PRINT "Sleeping..." |
||
SLEEP sec |
SLEEP sec |
||
PRINT "Awake!"</ |
PRINT "Awake!"</lang> |
||
"SLEEP" with no argument will sleep until a button is pressed on the keyboard (including modifier keys such as shift or control). Also, pressing a key while SLEEP is waiting for a specific amount of time (as above) will end the SLEEP. |
"SLEEP" with no argument will sleep until a button is pressed on the keyboard (including modifier keys such as shift or control). Also, pressing a key while SLEEP is waiting for a specific amount of time (as above) will end the SLEEP. |
||
Line 37: | Line 37: | ||
The function <tt>sleep</tt> needs seconds, which are read from the standard input. |
The function <tt>sleep</tt> needs seconds, which are read from the standard input. |
||
<c>#include <stdio.h> |
<lang c>#include <stdio.h> |
||
#include <unistd.h> |
#include <unistd.h> |
||
Line 48: | Line 48: | ||
printf("Awake!\n"); |
printf("Awake!\n"); |
||
return 0; |
return 0; |
||
}</ |
}</lang> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
<cpp>#include <unistd.h> |
<lang cpp>#include <unistd.h> |
||
#include <iostream> |
#include <iostream> |
||
Line 65: | Line 65: | ||
cout << "Awake!" << endl; |
cout << "Awake!" << endl; |
||
return 0; |
return 0; |
||
}</ |
}</lang> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 110: | Line 110: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
<java>import java.util.Scanner; |
<lang java>import java.util.Scanner; |
||
public class Sleep{ |
public class Sleep{ |
||
Line 120: | Line 120: | ||
System.out.println("Awake!"); |
System.out.println("Awake!"); |
||
} |
} |
||
}</ |
}</lang> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<ocaml>#load "unix.cma";; |
<lang ocaml>#load "unix.cma";; |
||
let seconds = read_int ();; |
let seconds = read_int ();; |
||
print_endline "Sleeping...";; |
print_endline "Sleeping...";; |
||
Unix.sleep seconds;; (* number is integer in seconds *) |
Unix.sleep seconds;; (* number is integer in seconds *) |
||
print_endline "Awake!";;</ |
print_endline "Awake!";;</lang> |
||
or |
or |
||
<ocaml>#load "unix.cma";; |
<lang ocaml>#load "unix.cma";; |
||
#directory "+threads";; |
#directory "+threads";; |
||
#load "threads.cma";; |
#load "threads.cma";; |
||
Line 137: | Line 137: | ||
print_endline "Sleeping...";; |
print_endline "Sleeping...";; |
||
Thread.delay seconds;; (* number is in seconds ... but accepts fractions *) |
Thread.delay seconds;; (* number is in seconds ... but accepts fractions *) |
||
print_endline "Awake!";;</ |
print_endline "Awake!";;</lang> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
seconds: |
seconds: |
||
<perl>$seconds = <>; |
<lang perl>$seconds = <>; |
||
print "Sleeping...\n"; |
print "Sleeping...\n"; |
||
sleep $seconds; # number is in seconds |
sleep $seconds; # number is in seconds |
||
print "Awake!\n";</ |
print "Awake!\n";</lang> |
||
microseconds and nanoseconds using the Time::HiRes module: |
microseconds and nanoseconds using the Time::HiRes module: |
||
<perl>use Time::HiRes qw( usleep nanosleep ); |
<lang perl>use Time::HiRes qw( usleep nanosleep ); |
||
$microseconds = <>; |
$microseconds = <>; |
||
Line 158: | Line 158: | ||
print "Sleeping...\n"; |
print "Sleeping...\n"; |
||
nanosleep $nanoseconds; |
nanosleep $nanoseconds; |
||
print "Awake!\n";</ |
print "Awake!\n";</lang> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
<python>import time |
<lang python>import time |
||
seconds = float(raw_input()) |
seconds = float(raw_input()) |
||
Line 168: | Line 168: | ||
time.sleep(seconds) # number is in seconds ... but accepts fractions |
time.sleep(seconds) # number is in seconds ... but accepts fractions |
||
# Minimum resolution is system dependent. |
# Minimum resolution is system dependent. |
||
print "Awake!"</ |
print "Awake!"</lang> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<ruby>seconds = gets.to_f |
<lang ruby>seconds = gets.to_f |
||
puts "Sleeping..." |
puts "Sleeping..." |
||
sleep(seconds) # number is in seconds ... but accepts fractions |
sleep(seconds) # number is in seconds ... but accepts fractions |
||
# Minimum resolution is system dependent. |
# Minimum resolution is system dependent. |
||
puts "Awake!"</ |
puts "Awake!"</lang> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |