fcntl - locking how to?

fcntl - locking how to?

Post by Claus P » Fri, 24 Jul 1998 04:00:00



I've read the UNIX FAQs about file locking using fcntl but nevertheless I'm
very confused.

Could someone tell me how to code in C the following programs:

a) lock an entire file
b) unlock file
c) (if necessary) check lock on file

Please respond by email.
Thanks,
Claus

--
Remove "atchoo" from email to reply.

 
 
 

fcntl - locking how to?

Post by !**?#! » Sat, 25 Jul 1998 04:00:00


Quote:> ) Could someone tell me how to code in C the following programs:
> )
> ) a) lock an entire file

        struct flock lock;
        int fd = open(...);

        memset(&lock,0,sizeof lock);
        lock.l_type = F_WRLCK;
        if (fcntl(fd,F_SETLKW,&lock)<0) ...

Quote:> ) b) unlock file

        abort();

or

        exit();

or

        memset(&lock,0,sizeof lock);
        lock.l_type = F_UNLCK;
        if (fcntl(fd,F_SETLK,&lock)<0) ...

Quote:> ) c) (if necessary) check lock on file

        memset(&lock,0,sizeof lock);
        lock.l_type = F_WRLCK;
        if (fcntl(fd,F_SETLK,&lock)<0) {
                lock already set
        }else {
                lock is now set
        }

--
The fire cools and fear returns;     |  I don't use no smilies.

The spectres spell in speeches fell; |   www.geocities.com/SoHo
their hatred hounds and horror burns.| /Studios/5079/index.html

 
 
 

1. fcntl() lock vs. flock() lock?

As I know
mail.local program uses  flock(fd, LOCK_FX) or lockf(fd,LOCK_FX,0),
where fd is file descriptor of open(var/mail/id,..) when it writes in
/var/mail/id file.

Can  following function detect whether mail.local is writing?

lockstatus.lockedfd=open(mailfile, O_RDONLY,0);
lock_test(lockstatus.lockedfd, F_WRLCK,0,SEEK_SET , 0); /* with  this
line!!! */

int lock_test(int fd, int type, off_t offset, int whence, off_t len)
{
        struct flock lock;

        lock.l_type=type;
        lock.l_start=offset; /*byte offset, relative to l_whence */
        lock.l_whence=whence; /*SEEK_SET, SEE_CUR,SEEK_END */
        lock.l_len=len; /* #bytes (0 means to EOF */

        if( fcntl(fd, F_GETLK, &lock) < 0 ){
                return -1;
        }
        if(lock.l_type== F_UNLCK )
                return 0;
        return (lock.l_pid);

Any info will be appreciated!

2. problem with SCSI on LINUX.

3. Any way to forcibly cancel fcntl lock?

4. I found the best shell provider EVER! $10/month for 6 bg processes and an UNLIMITED QUOTA!!! It's incredible!

5. fcntl locks over nfs

6. force redirection through "home" page

7. fcntl() locks and threads

8. Idle process monitor

9. flock() or fcntl() lock on mmap()ed file ?

10. flock vs fcntl locks

11. ==>> FCNTL locking NOT FLOCK!!!

12. fcntl(), locking, and l_sysid?

13. apache fcntl locking problem (solaris 2.5)