Sleep
You are encouraged to solve this task according to the task description, using any language you may know.
Write a program that does the following in this order:
- Input an amount of time to sleep in whatever units are most natural for your language (milliseconds, seconds, ticks, etc.). This unit should be noted in comments or in a description.
- Print "Sleeping..."
- Sleep the main thread for the given amount of time.
- Print "Awake!"
- End.
Ada
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; with Ada.Float_Text_Io; use Ada.Float_Text_Io;
procedure Sleep is
In_Val : Float;
begin
Get(In_Val); Put_Line("Sleeping..."); delay Duration(In_Val); Put_Line("Awake!");
end Sleep;</ada>
BASIC
<qbasic>INPUT sec 'the SLEEP command takes seconds PRINT "Sleeping..." SLEEP sec PRINT "Awake!"</qbasic> "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.
C
The function sleep needs seconds, which are read from the standard input.
<c>#include <stdio.h>
- include <unistd.h>
int main() {
unsigned int seconds; scanf("%u", &seconds); printf("Sleeping...\n"); sleep(seconds); printf("Awake!\n"); return 0;
}</c>
C++
<cpp>#include <unistd.h>
- include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
useconds_t microseconds; cin >> microseconds; cout << "Sleeping..." << endl; usleep(microseconds); cout << "Awake!" << endl; return 0;
}</cpp>
Common Lisp
(defun test-sleep () (let ((seconds (read))) (format t "Sleeping...~%") (sleep seconds) (format t "Awake!~%"))) (test-sleep)
E
You can't do that.
No, really. E's approach to timing, concurrency, and IO is non-blocking; if you want to wait for something, you say what you want to do when it happens — i.e. callbacks. There are no threads of control which can be stopped — except automatically when they just have nothing to do.
So, the closest thing possible to the task description is to wait for the specified time to pass, then do whatever the next thing is.
def sleep(milliseconds :int, nextThing) { stdout.println("Sleeping...") timer.whenPast(timer.now() + milliseconds, fn { stdout.println("Awake!") nextThing() }) }
Forth
: sleep ( ms -- ) ." Sleeping..." ms ." awake." cr ;
Haskell
import Control.Concurrent main = do seconds <- readLn putStrLn "Sleeping..." threadDelay $ round $ seconds * 1000000 putStrLn "Awake!"
Java
<java>import java.util.Scanner;
public class Sleep{ public static void main(String[] args) throws InterruptedException{ Scanner input = new Scanner(System.in); int ms = input.nextInt(); //Java's sleep method accepts milliseconds System.out.println("Sleeping..."); Thread.sleep(ms); System.out.println("Awake!"); } }</java>
OCaml
<ocaml>#load "unix.cma";; let seconds = read_int ();; print_endline "Sleeping...";; Unix.sleep seconds;; (* number is integer in seconds *) print_endline "Awake!";;</ocaml>
or <ocaml>#load "unix.cma";;
- directory "+threads";;
- load "threads.cma";;
let seconds = read_float ();; print_endline "Sleeping...";; Thread.delay seconds;; (* number is in seconds ... but accepts fractions *) print_endline "Awake!";;</ocaml>
Perl
seconds: <perl>$seconds = <>; print "Sleeping...\n"; sleep $seconds; # number is in seconds print "Awake!\n";</perl>
microseconds and nanoseconds using the Time::HiRes module: <perl>use Time::HiRes qw( usleep nanosleep );
$microseconds = <>; print "Sleeping...\n"; usleep $microseconds; print "Awake!\n";
$nanoseconds = <>; print "Sleeping...\n"; nanosleep $nanoseconds; print "Awake!\n";</perl>
Python
<python>import time
seconds = float(raw_input()) print "Sleeping..." time.sleep(seconds) # number is in seconds ... but accepts fractions
- Minimum resolution is system dependent.
print "Awake!"</python>
Ruby
seconds = gets.to_f puts "Sleeping..." sleep(seconds) # number is in seconds ... but accepts fractions
- Minimum resolution is system dependent.
puts "Awake!"
Tcl
set seconds [gets stdin] puts Sleeping... after [expr $seconds*1000] puts Awake!
Toka
This makes use of the sleep() function from libc which suspends execution for a specified number of seconds.
1 import sleep as sleep() [ ." Sleeping...\n" sleep() drop ." Awake!\n" bye ] is sleep 45 sleep