473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

File locking

If I have multiple processes (parent+1 or more child processes) that are
reading a file at the same time (fopen("path.txt","r") - do I have to
implement mutual exclusion requiring only one process have access to the
file at a time?
Nov 14 '05 #1
5 4038
"Rookie" <do***********@rediffmail.com> writes:
If I have multiple processes (parent+1 or more child processes) that are
reading a file at the same time (fopen("path.txt","r") - do I have to
implement mutual exclusion requiring only one process have access to the
file at a time?


If you are only reading, no. If reading and writing, or more than one
process is writing, and you want consistent results you'll need some
kind of locking.

--
Måns Rullgård
mr*@mru.ath.cx
Nov 14 '05 #2

"Rookie" <do***********@rediffmail.com> wrote in message
news:ck**********@gist.usc.edu...
If I have multiple processes (parent+1 or more child processes) that are
reading a file at the same time (fopen("path.txt","r") - do I have to
implement mutual exclusion requiring only one process have access to the
file at a time?


You only have to implement mutual exclusion if your processes need
exclusive access. You can have multiple processes concurrently reading and
writing to a file without using mutual exclusion if you don't mind seeing
old data, new data, or mixtures of both.

You the kernel doesn't ever force you to use mutual exclusion. You only
need it if you can't meet one of your requirements without it.

DS
Nov 14 '05 #3
> If you are only reading, no. If reading and writing, or more than one
process is writing, and you want consistent results you'll need some
kind of locking.


I just wanted to clarify one thing - multiple processes reading from the
file will not cause the file pointer to be pointed to different positions
especially when there is a context switch between processes.

Ex. Suppose a parent forks two children A and B and both have file pointers
initialized to the start of the same file by the parent. If process A was
reading from the start of file to say a point X in the file and now if
process B starts executing and reads from the same file, will B's copy of
the file pointer point to the start of the file or will it point to X?

Nov 14 '05 #4
>I just wanted to clarify one thing - multiple processes reading from the
file will not cause the file pointer to be pointed to different positions
especially when there is a context switch between processes.
ANSI C does not guarantee support for multiple processes.
Ex. Suppose a parent forks two children A and B and both have file pointers
initialized to the start of the same file by the parent. If process A was
reading from the start of file to say a point X in the file and now if
process B starts executing and reads from the same file, will B's copy of
the file pointer point to the start of the file or will it point to X?


On a POSIX system, the file pointer (in both) will point to X plus
however much B read, assuming that A does all its reading, stops,
and then B does all its reading. If they interleave, you have a
potential mess.

If each process opens the file itself (after the fork(), on a POSIX
system), they have independent file pointers.

If the parent opens the file before the fork(), the two processes
with the file open after the fork() will share a file pointer.
at the OS level. I'm not sure what happens at the stdio level.

Gordon L. Burditt
Nov 14 '05 #5
In article <news:ck**********@gist.usc.edu>
Rookie <do***********@rediffmail.com> wrote:
Ex. Suppose a parent forks two children A and B and both have file pointers
initialized to the start of the same file by the parent.
In comp.lang.c, we do not have fork(). :-)
If process A was reading from the start of file to say a point X
in the file and now if process B starts executing and reads from the
same file, will B's copy of the file pointer point to the start of
the file or will it point to X?


In comp.unix.programmer, we have POSIX, which says that if the
underlying file descriptor is shared, so is the seek offset. If
only the file (not the descriptor) is shared, the seek offsets are
independent. The dup(), dup2(), and fork() calls all share the
file descriptor. Separate open()s do not.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Dino | last post by:
dear all, i've created an application for a customer where the customer can upload ..csv-files into a specified ftp-directory. on the server, a php-script, triggered by a cronjob, reads all the...
2
by: Kamus of Kadizhar | last post by:
Thanks to Robert Brewer, I got enough insight into logging to make it work.... Now I have another issue: file locking. Sorry if this is a very basic question, but I can't find a handy reference...
2
by: Corne' Cornelius | last post by:
Hi, When you open a file for writing/appending with open() or fopen(), and you have multiple applications that might want to write to the same file at the same time, could that cause weirdness...
8
by: Stephen Corey | last post by:
I'm writing an app that basically just appends text to a text file on a Win2K Server. They fill out a form, click a button, and 1 line is appended to the file. Multiple people will run this app at...
4
by: darrel | last post by:
I've been dealing with a file locking issue for a while. Our CMS spits out a new XML file each time an item in the DB is updated. This XML file is basically our site menu, and is what we use on...
1
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access...
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
17
by: shineofleo | last post by:
Here is the situation: I wrote a VB programm, which stores all the information in a single Access database file using jet engine. It worked well, however one of my customs reported that there was...
15
by: Amir Michail | last post by:
Hi, Trying to open a file for writing that is already open for writing should result in an exception. It's all too easy to accidentally open a shelve for writing twice and this can lead to...
15
by: Matt Brandt | last post by:
I am trying to get multiple threads to lock specific regions of a file. However, since each thread has the same PID, it appears that a lock by one thread does not block another thread from the same...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.