drkcore

2009/09/09 20:24:31

OCamlでfork

どう書くの外部の実行ファイルを呼び出しって問題を解く。

ブロックする版は

#load "unix.cma";;
Unix.system "/bin/sleep 3";;
print_string "waited\n";;

でいいんでしょ。

で、ブロックしない版をこんな感じで書いたら、

let pid = Unix.fork () in match pid with
     0 -> Unix.system "/bin/sleep 3"; print_string "waited\n"
   | _ -> print_string "not wait\n"
;;

warningがでる。

Warning S: this expression should have type unit.

よくわからなくて気持ち悪い。

追記090909

ignoreを使えばよいとはてブで教えてもらった。

# ignore;;
- : 'a -> unit = <fun>

unitを返す

let pid = Unix.fork () in match pid with
     0 -> ignore(Unix.system "/bin/sleep 3"); print_string "waited\n"
   | _ -> print_string "not wait\n"
;;

Comments