View Full Version: Reading and Writing file by two processes

C++ Learning Community > C++ Help > Reading and Writing file by two processes


Title: Reading and Writing file by two processes


kasun04 - February 7, 2007 04:44 AM (GMT)

I got one file named "status.txt"

I'm accessing the same file by two processes simultaniously. (One is C++ binary and the other one is TCL script).
Both processes are supposed to modify the file (different records).
How to get rid of corrupted / incomplete / mixed data that is formed due to muliple access of two processes.

C-Man - February 7, 2007 04:52 AM (GMT)
you need a way to synchronise
probbaly some way of locking the file. you should probably open the file in exclusive rights as in only one process can modify it at a time.
process1 opens file
process1 modifies
process2 tries open file, fails
process2 waits till file is available
process1 closes file
process2 opens file
process2 modifies file
process2 closes file

so on

now how do you do it is os specific

myork - February 7, 2007 05:11 AM (GMT)
QUOTE (kasun04 @ Feb 6 2007, 11:44 PM)
I got one file named "status.txt"

I'm accessing the same file by two processes simultaniously. (One is C++ binary and the other one is TCL script).
Both processes are supposed to modify the file (different records).
How to get rid of corrupted / incomplete / mixed data that is formed due to muliple access of two processes.

Because of buffering and other effects.
If two processes have the same file open then you will get file corruption.

You must find a way to sychronise the writting to a file.
i.e. Only one processes at a time should write to a file. The other processes should be forced to wait.

kasun04 - February 7, 2007 05:29 AM (GMT)
Thanks for the reply,

Could u please tell me a way to lock files (synchronize file reading) inorder to acheive this task.

important:
Process 1 : C++ binary
Process 2 : TCL Script

I'm using c++ binary and TCL script as the simultanous processes.

Specifications

OS : Solaris 10
Compiler: gcc 3.4.3

C-Man - February 7, 2007 08:26 AM (GMT)
in c++ you can use the open syscall (check out O_EXCL)
don't know about TCL though

myork - February 7, 2007 02:20 PM (GMT)
QUOTE (kasun04 @ Feb 7 2007, 12:29 AM)
Thanks for the reply,

Could u please tell me a way to lock files (synchronize file reading) inorder to acheive this task.

important:
Process 1 : C++ binary
Process 2 : TCL Script

I'm using c++ binary and TCL script as the simultanous processes.

Specifications

OS        : Solaris 10
Compiler: gcc 3.4.3


In C there is a file locking function flock(). I am sure that TCL has the same feature. (see http://216.250.128.246/en/man/html.1tcl/flock.1tcl.html)

But it is an advisory lock.
Basically this means the file system flags the file as locked but this does not stop anybody from opening it. All processes that open the file must try and get the lock only one will succeed (but if a process is not friendly and does not check the lock it will still be able to write to the file).

CODE
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
                       operations.




Hosted for free by InvisionFree