Solved

File I/O in multi user environments.

Posted on 1998-03-24
3
257 Views
Last Modified: 2010-04-21
null
0
Comment
Question by:carydb
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
ID: 2009017
If you are needing to write files un a multi-process or multi-user scenario, you must open the file and then use the flock() function on it before doing any write operation.  If you try and flock() a file that is already "flocked" by another process, your call will hang until the flock is removed by the other process.
0
 

Author Comment

by:carydb
ID: 2009018
That's exactly what I was looking for. Only problem is that "flock()" is not documented in my C books. Could you provide a syntax: Thanks.
0
 
LVL 32

Expert Comment

by:jhance
ID: 2009019

FLOCK(2)            Linux Programmer's Manual            FLOCK(2)

NAME
       flock - apply or remove an advisory lock on an open file

SYNOPSIS
       #include <sys/file.h>

       int flock(int fd, int operation)

DESCRIPTION
       Apply  or  remove  an  advisory lock on an open file.  The file is specified by fd.  Valid operations are given
       below:

              LOCK_SH   Shared lock.  More than one process may hold a shared lock for a given file at a given time.

              LOCK_EX   Exclusive lock.  Only one process may hold an exclusive lock for a given file at a given time.

              LOCK_UN   Unlock.

              LOCK_NB   Don't  block  when  locking.   May be specified (by or'ing) along with one of the other opera-
                        tions.

       A single file may not simultaneously have both shared and exclusive locks.

       A file is locked (i.e., the inode), not the file descriptor.  So, dup(2) and fork(2)  do  not  create  multiple
       instances of a lock.

RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.

ERRORS
       EWOULDBLOCK
              The file is locked an the LOCK_NB flag was selected.

NOTES
       Under Linux, flock is implemented as a call to fcntl.  Please see fcntl(2) for more details on errors.

SEE ALSO
       open(2), close(2), dup(2), execve(2), fcntl(2), fork(2),
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question