In Perl, how do I safely create a temporary FIFO?

前端 未结 2 704
南旧
南旧 2021-02-05 17:08

To compare with diff adjacent records from a file, I created two FIFOs, forked children to supply their write ends, and captured the output of

diff -ub $previous         


        
2条回答
  •  借酒劲吻你
    2021-02-05 17:44

    How about creating a temporary directory (a la mkdtemp()) to avoid race conditions, and then put your FIFOs in there?

    For example:

    use File::Temp qw(tempdir);
    use File::Spec::Functions qw(catfile);
    use POSIX qw(mkfifo);
    
    my $dir = tempdir(CLEANUP=>1);
    my $fifo0 = catfile($dir, "fifo0");
    mkfifo($fifo0, 0700) or die "mkfifo($fifo0) failed: $!";
    my $fifo1 = catfile($dir, "fifo1");
    mkfifo($fifo1, 0700) or die "mkfifo($fifo1) failed: $!";
    
    print "FIFO #0: $fifo0\n";
    print "FIFO #1: $fifo1\n";
    

提交回复
热议问题