Solved

File I/O in multi user environments.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
removing nim resources 5 74
FTP on FreeBSD server 2 166
Parsing a file using ksh 10 79
Execute multiple curl cmds with sleep and send output to file 10 120
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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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 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.:
Suggested Courses

739 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