help needed for exclusive file access

help needed for exclusive file access

Post by Dennis Forema » Tue, 03 Sep 2002 06:54:45



Hi,
I have a web-based transaction program that needs to read, then append to a
file, but must ensure no other instances of the program are trying to do the
same thing at the same time on a Solaris system.

I am wondering if there is a way to get exclusive access (across multiple
instances of the same program) to a file (on a Solaris system), from a
program written in C++. I know that exclusive files are not a part of the
definition of C++ files, but am hoping to find an I/O library or some other
mechanism that provides such an extension.

I have discovered the flock function, but can't find a C/C++ function that
lets me obtain a file descriptor (from a named file) as the 1st parameter to
the call.

Can anyone offer some hints?
--
regards,
D. J. Foreman

 
 
 

help needed for exclusive file access

Post by Alan Coopersmi » Tue, 03 Sep 2002 12:00:31



|I have discovered the flock function, but can't find a C/C++ function that
|lets me obtain a file descriptor (from a named file) as the 1st parameter to
|the call.

open() to open the file directly or fileno() to get the file descriptor
if you used fopen() (I assume the C++ classes have some equivalent, but
I don't know what it is.)

--
________________________________________________________________________


  Working for, but definitely not speaking for, Sun Microsystems, Inc.

 
 
 

help needed for exclusive file access

Post by Dennis Forema » Wed, 04 Sep 2002 03:28:56


Alan,
Thanks a million! "fileno()" was exactly what I needed to comlete my
scenario. Now I can open the file using C++

    fstream myfile;
    myfile.open("myfile") the lock it with:

    fd=fileno("myfile");
    flock* myflockptr;
    myflockptr=new flock;
    myflockptr->l_type=F_WRLCK;
    myflockptr->l_whence=0;
    myflockptr->l_start=0;
    myflockptr->l_len=0;
    lockrc=fcntl(myfd,F_SETLK,myflockptr);

This locks the file against other copies of the program that also use the
fcntl (which is what I needed) and still lets me use the C++ "<<" operator
for the I/O.

--
regards,
D. J. Foreman


> |I have discovered the flock function, but can't find a C/C++ function
that
> |lets me obtain a file descriptor (from a named file) as the 1st parameter
to
> |the call.

> open() to open the file directly or fileno() to get the file descriptor
> if you used fopen() (I assume the C++ classes have some equivalent, but
> I don't know what it is.)

> --
> ________________________________________________________________________


>   Working for, but definitely not speaking for, Sun Microsystems, Inc.

 
 
 

help needed for exclusive file access

Post by Alan Coopersmi » Wed, 04 Sep 2002 08:04:57



|    fd=fileno("myfile");

fileno does NOT take a string!  It takes a FILE * returned from a
function such as fopen().

--
________________________________________________________________________


  Working for, but definitely not speaking for, Sun Microsystems, Inc.

 
 
 

1. Obtaining exclusive access to a file

Hi,

I've written a program with gcc 2.95.2, on FreeBSD, that briefly needs to
obtain exclusive access to a file.  I expected to be able to do this with
the CRT library open API, but it has no such option.  Searching the docs, I
found flock, so confidently proceeded to use that with its LOCK_EX option.
However, I now note that it only applies an advisory lock, so is only safe
with cooperating processes.  Given that this is a common requirement, it
seems unlikely that I should have to scan the source of every other program
that accesses this file, to take account of all possible file locking
semantics. Am I wrong?

Thanks,
Keith

2. Seeing CD audio tracks?

3. How do I get exclusive access to a file?

4. KDE Overiding X resources?

5. Exclusive file access

6. solaris 2.2 hangs on SIGURG upon receiving an OOB-msg

7. Exclusive File access

8. Small RAID configs

9. HELP!: exclusive open for output file failed in m4_defs: Permission denied

10. help - exclusive files on Solaris via C++

11. Help: exclusive file open

12. Exclusive Unix file system file write locks and Oracle and Veritas

13. exclusive access to scsi disk