How do I use fcntl on HPUX 9.0? 
Author Message
 How do I use fcntl on HPUX 9.0?

I am trying to understand how to use the unix fcntl commands F_GETLK,
F_SETLKW, and F_SETLK with the perl fcntl function.  In particular,
I don't understand how the perl SCALAR argument maps into the flock
structure.  Also, when I look at the "sys/fcntl.ph" created by h2ph
and c2ph, I see no code where the flock structure was defined.
Should I see some corresponding perl code?  Could someone who has
utilized this function explain it to me?

Dave Hannum



Sun, 05 May 1996 11:17:13 GMT  
 How do I use fcntl on HPUX 9.0?

   I am trying to understand how to use the unix fcntl commands F_GETLK,
   F_SETLKW, and F_SETLK with the perl fcntl function.  In particular,
   I don't understand how the perl SCALAR argument maps into the flock
   structure.  Also, when I look at the "sys/fcntl.ph" created by h2ph
   and c2ph, I see no code where the flock structure was defined.
   Should I see some corresponding perl code?  Could someone who has
   utilized this function explain it to me?

   Dave Hannum

I'm using the following code fragment for setting write locks. It's
part of a script which appends some reports to a logfile. It works
well on HP-UX 9.0 and SunOS 5.2 (Solaris 2.2).

-----------------------------------------------------------------------------
#!/usr/local/bin/perl
[...]

$logfile = '/tmp/testfile';
sub __STDC__{1;};               # Without this line I'll get errors from
                                #   including 'sys/types.ph' under SunOS 5.x
sub _POSIX1_SOURCE{1;}          # Next two sub's are necessary on HP-UX only.
sub _INCLUDE_POSIX_SOURCE{1;}
require 'fcntl.ph';
require 'errno.ph';
require 'unistd.ph';

$count = 0;                     # lock retry counter
open(LOG, ">>$logfile") || die "$0: $logfile: $!\n\taborted";

# pack lock structure
local ($lck) = pack('ss8l', &F_WRLCK, 0, 0, 0, 0, 0, 0, 0, 0, 0);
while (1) {
    # set writelock logfile
    last if (fcntl(LOG, &F_SETLK, $lck));

    # not successful - maybe file already locked
    die "$0: $logfile: $!\n\taborted" # not locked, but some other error
        unless (&EAGAIN == $!);
    die "$0: $logfile: still locked\n"        # exit after 15 retries
        if $count > 15;
    # output some dots, showing we are still busy...
    print ($count++ ? '.' :
           "$0: $logfile: file locked\nwaiting for release...");
    sleep(1);                           # retry after one second

Quote:
}

# seek to end of file - maybe $logfile has grown or shrunk in the time between
# open and successfully locking
seek (LOG, 0, &SEEK_END) || die "$0: seek $logfile: $!\n\taborted";

[...]

# closing $logfile releases flocks, too.
(close LOG) || die "$0: $!\n\taborted";

-----------------------------------------------------------------------------

Hope this helps.

        Andreas Koppenhoefer
--
Andreas Koppenhoefer, Student der Universitaet Stuttgart, BR Deutschland
prefered languages: German, English, C, perl ("Just another Perl hacker,")

privat: Belaustr. 5/3, D-70195 Stuttgart, Germany
phone:  +49 711 696378 and +49 711 694111 (19-22h MEZ=GMT+1)



Thu, 09 May 1996 01:35:08 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. How do I use fcntl on HPUX 9.0?

2. installing Perl4.036 on HPUX 9.0 - HP9000/700

3. curseperl, HP 9000/8xx, HPUX 9.0

4. Making Perl on HPUX 9.0

5. Checking for open files using fcntl: flock in Solaris

6. Has anyone used Fcntl to handle file locking?

7. Req for example: file lock using fcntl

8. Help with using fcntl file locking

9. File locking using Fcntl

10. Using fcntl in perl

11. Piddly runtime error using DB_File and Fcntl

12. Nee file lock example using fcntl

 

 
Powered by phpBB® Forum Software