?
Solved

File I/O in multi user environments.

Posted on 1998-03-24
3
Medium Priority
?
271 Views
Last Modified: 2010-04-21
null
0
Comment
Question by:carydb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
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…
Suggested Courses

800 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