Drkcore

17 02 2009 Linux perl Tweet

名前付きパイプ

pは名前付きパイプらしい。mkfifoコマンドで作成できる。

$ mkfifo test
$ ll
合計 0
prw-r--r-- 1 kzfm kzfm 0 2009-02-17 19:49 test

パイプがいっぱいの時には書き手側がブロックされ、パイプが空の場合には読み手側がブロックされる。

書き手側

for my $i (0..3) {
  open my $fh, ">", "test";
  print $fh "test-$i";
  close $fh;
  sleep 1;
}

読み手側

while (1) {
  open my $fh, "<", "test";
  while(<$fh>){ print "Got: $_\n";}
  close $fh;
  sleep 3;
}

とやると読み手側で待たされて10秒くらいかかって終わる。

$ time perl testwrite.pl
perl testwrite.pl  0.00s user 0.00s system 0% cpu 10.004 total

POSIXにmkfifoもあった。

このzshの記事も面白そう

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021