Solved

File I/O in multi user environments.

Posted on 1998-03-24
3
225 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now