http://www.perlmonks.org?node_id=228120


in reply to fcntl() madness with C and perl

The problem is your perl program, and it does not lock properly.

I tested the following perl code with your c code on AIX, and they worked with each other (compare my code with yours):
use strict; use Fcntl qw(SEEK_SET F_WRLCK F_UNLCK F_SETLKW); my($pack); open(FILE,">a"); $pack = pack('s s l l s', F_WRLCK, SEEK_SET, 0, 1, 0); print(fcntl(FILE, F_SETLKW, $pack) . "\n"); sleep(20); $pack = pack('s s l l s', F_UNLCK, SEEK_SET, 0, 1, 0); print(fcntl(FILE, F_SETLKW, $pack) . "\n"); close(FILE);

Replies are listed 'Best First'.
Re: Re: fcntl() madness with C and perl
by Anonymous Monk on Jan 19, 2003 at 04:41 UTC
    I think you're right that the pack is the problem and wanted to add that even a link to 's s l l l' working won't be useful since the lengths of the data types of a struct flock are not standard. Some systems use 64 bits for file offsets and those definitely won't work with 'l' in pack (which is always 32 bits), but 'q' might.
      I agree with you on this, but if the perl and c program are compiled under the same setting, most likely this is not a problem. But you are right, this is definitely something should be taken care of.

      The original perl code actually does not import constants defined in FCntl properly. That is a big problem, and messed up the values he tried to set in the lock structure. You can not simply say "use FCntl", as FCntl does not export those contants by default.

      His pack format might be wrong, but I cannot say that for sure. Although his pack format is different from mine, but as you said, that might matches the lock structure on his computer. That's up to darkomen to check.

Re^2: fcntl() madness with C and perl
by Preceptor (Deacon) on May 10, 2016 at 11:24 UTC

    13 years later - this was helpful. Thank you.