Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

fcntl() madness with C and perl

by darkomen (Acolyte)
on Jan 19, 2003 at 00:54 UTC ( [id://228103]=perlquestion: print w/replies, xml ) Need Help??

darkomen has asked for the wisdom of the Perl Monks concerning the following question:

I have a C program that uses fcntl() to lock files, the following code is a test program that does a lock, sleep, unlock:
#include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> int main() { struct flock lock; int fd; fd = open("a",O_WRONLY); lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 1; lock.l_pid = 0; printf("%d\n",fcntl(fd, F_SETLKW, &lock)); sleep(20); lock.l_type = F_UNLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 1; lock.l_pid = 0; printf("%d\n",fcntl(fd, F_SETLKW, &lock)); close(fd); return(0); }
I would like to write a perl script to do the same locking so I wrote:
!/usr/bin/perl use strict; use Fcntl; my($pack); open(FILE,">a"); $pack = pack('s s l l l', &F_WRLCK, 0, 0, 1, 0); print(fcntl(FILE, &F_SETLKW, $pack) . "\n"); sleep(20); $pack = pack('s s l l l', &F_UNLCK, 0, 0, 1, 0); print(fcntl(FILE, &F_SETLKW, $pack) . "\n"); close(FILE);
The two programs when run right after one another dont see each other's lock, but when running two copies of the same program (either program) they respect the lock. I imagine I have the perl syntax not quite matched up to the C syntax. Any help would be appriciated.

Replies are listed 'Best First'.
Re: fcntl() madness with C and perl
by pg (Canon) on Jan 19, 2003 at 04:34 UTC
    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);
      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.

      13 years later - this was helpful. Thank you.

Re: fcntl() madness with C and perl
by bart (Canon) on Jan 19, 2003 at 13:53 UTC
    Could it be you posted the very same question in the newsgroup <comp.lang.perl.misc>? It sure looks that way. So people interested in the replies should check out the replies in that thread as well. As usual, Benjamin Goldberg gave a pretty solid reply. Most important tip of all: do not unlock a file before closing it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://228103]
Approved by djantzen
Front-paged by Aristotle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-06-20 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.