flock() on different Unix platform

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default flock() on different Unix platform

    Hi,
    I want to Perl flock() between to process to work with one output
    data file.
    But on different Unix platform, I got different results.
    As followed, on Solaris7, It could work. (compile the perl-5.8.0
    with default options)

    But on Redhat Linux(7.3/8.0/9.0 for Intel), it couldn't work.
    the output file is messed, mixed with ('A','B','C','D')
    and ('1','2','3','4')
    (compile the perl-5.8.0 with default options)

    Someone had told me that it is related to the compile
    option of Perl, and I haved tried "-Ud_flock", but it still
    don't work.

    I wonder if my script have any problem or anything else is
    wrong.

    Any advices is appreciated!

    Best Regards,
    Sun Guonian

    ===== On Solaris 7 for SPARC platform =====
    % cat lock03.pl
    #!/usr/bin/perl

    use Fcntl ':flock';
    use strict;

    my($res);
    my($pid);

    open(FH, ">lock_out");

    if($pid=fork()) {
    sleep(1);
    $res=mylock();
    print "$$: lock res=$res\n";
    sleep(5);
    writefile('A' x 40);
    sleep(5);
    writefile('B' x 40);
    sleep(5);
    writefile('C' x 40);
    sleep(5);
    writefile('D' x 40);
    $res=myunlock();
    print "$$: unlock res=$res\n";
    }
    elsif($pid==0) {
    $res=mylock();
    print "$$: lock res=$res\n";
    sleep(3);
    writefile('1' x 40);
    sleep(3);
    writefile('2' x 40);
    sleep(3);
    writefile('3' x 40);
    sleep(3);
    writefile('4' x 40);
    $res=myunlock();
    print "$$: unlock res=$res\n";
    exit(0);
    }

    sub writefile {
    my($var)=@_;
    my($max)=80000;
    my($i);
    for($i=0;$i<$max;$i++) {
    print FH "$var\n";
    }
    }

    sub mylock {
    flock(FH, LOCK_EX);
    seek(FH, 0, 2);
    return 1;
    }

    sub myunlock {
    flock(FH, LOCK_UN);
    }

    % ./lock03.pl
    16198: lock res=1
    16198: unlock res=1
    16197: lock res=1
    16197: unlock res=1

    % wc lock_out
    640000 640000 26240000 lock_out

    % uniq lock_out > kkk
    % cat kkk
    1111111111111111111111111111111111111111
    2222222222222222222222222222222222222222
    3333333333333333333333333333333333333333
    4444444444444444444444444444444444444444
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
    CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
    DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
    ===== On Solaris 7 for SPARC platform =====
    Sun Guonian Guest

  2. Similar Questions and Discussions

    1. #38749 [NEW]: odbc_connect failing on Unix platform
      From: david dot rothwell at wolseley dot co dot uk Operating system: SunOS 5.9 PHP version: 5.1.6 PHP Bug Type: ODBC related...
    2. Need help with a simple UNIX sockets server based on IO::Socket::UNIX
      Hi. I've tried to create a simple client + server that communicate through a unix socket. As with all socket servers, it has a loop where it...
    3. fonts from platform to platform problem
      Does anybody out there have an IBM clone with shockwave on it? (it's a free download like acrobat reader) I've made a director movie in a class I'm...
    4. non-unix admin pleads for help from unix gurus
      Hello, I am a network admin working on a project which involves a SCO Unix 5 server provided by another software company. The server provides...
    5. SCO Registers UNIX(R) Copyrights, Offers UNIX License by ignatius.schwartz-pr.com(Postfix) with ESMTP id 5EF605D87 for <scoannmod@xenitec.on.ca>; Mon, 21
      Some code of Windows is based/taken from the UNIX and LINUX environnement system. (Like some DNS, TCP and Internet Explorer things) Can we use...
  3. #2

    Default Re: flock() on different Unix platform

    Quote Originally Posted by Sun Guonian View Post
    Hi,
    I want to Perl flock() between to process to work with one output
    data file.
    But on different Unix platform, I got different results.
    As followed, on Solaris7, It could work. (compile the perl-5.8.0
    with default options)

    But on Redhat Linux(7.3/8.0/9.0 for Intel), it couldn't work.
    the output file is messed, mixed with ('A','B','C','D')
    and ('1','2','3','4')
    (compile the perl-5.8.0 with default options)
    Judging by the ages of RedHat reported, this is probably a fairly old post. But, just so people who are searching the Internet and come across this, I found a resolution to your problem.

    In the initial test code, the lock file is created before the fork() code, and when I run their test code it too creates bad output on my workstation (Ubuntu 10.04.1, Perl 5.10.1).

    If we move the lock file creation after the fork (i.e. Parent and Child each create their own file handle to it), the resulting output is correct.

    The Perl documentation for flock ([url]http://perldoc.perl.org/functions/flock.html[/url]) alludes to some fork&flock anomalies, but doesn't explain. From some additional research, this appears to be an expected result. If the file handle is created before the fork, both parent and child share the file descriptors and flock doesn't see them as being different:
    [url]http://www.perlmonks.org/?node_id=463377[/url]

    Hope this helps!

    DanL
    Dan Linder is offline Junior Member
    Join Date
    Sep 2010
    Posts
    1

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139