challenge 出力の一時停止と再開

起動すると、標準出力に1秒毎に'a'の1文字を出力し続けるプログラムで、 以下の条件を満たすものを「どう書く?」

  • 'q'キーが押されるとプログラムは終了する
  • 出力中に'p'キーが押されると一時停止する
  • 一時停止中に'p'キーが押されると出力を再開する

Posted feedbacks - OCaml

これもleditは付けずに実行してください。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#load "unix.cma";;
open Unix;;

(*エコー、行バッファoff*)
let x = tcgetattr Unix.stdout in
  tcsetattr Unix.stdout TCSADRAIN {x with
    c_icanon = false;  c_vmin = 1; c_echo = false; };
  at_exit (fun () -> tcsetattr Unix.stdout TCSADRAIN x;);

(*一秒ごとのa*)
Sys.set_signal Sys.sigalrm (Sys.Signal_handle 
  (fun _ -> print_char 'a'; flush Pervasives.stdout));

(*本編*)
let t = ref (setitimer ITIMER_REAL {it_interval=1.0; it_value=1.0}) in
  while true do
    match input_char Pervasives.stdin with
    | 'q' -> exit 0
    | 'p' -> t := setitimer ITIMER_REAL !t
    | _ -> ()
  done;;

Index

Feed

Other

Link

Pathtraq

loading...