Solved

File I/O in multi user environments.

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

Independent Software Vendors: 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

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

696 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