flock() on different Unix platform

Discussion in 'Perl Misc' started by Sun Guonian, Aug 18, 2003.

  1. Sun Guonian

    Sun Guonian Guest

    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, Aug 18, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Babu
    Replies:
    0
    Views:
    526
  2. jcc
    Replies:
    15
    Views:
    4,732
    Nigel Wade
    May 12, 2006
  3. Coca
    Replies:
    15
    Views:
    665
    Alan Balmer
    Jan 14, 2004
  4. thakadu
    Replies:
    3
    Views:
    523
    thakadu
    Nov 4, 2005
  5. Kaspar Schiess

    platform.rb (was: PLATFORM tests)

    Kaspar Schiess, Nov 25, 2005, in forum: Ruby
    Replies:
    0
    Views:
    136
    Kaspar Schiess
    Nov 25, 2005
Loading...

Share This Page