473,796 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File locking

If I have multiple processes (parent+1 or more child processes) that are
reading a file at the same time (fopen("path.tx t","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 4074
"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.tx t","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.tx t","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.ed u>
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.progr ammer, 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
9118
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 data, imports it into a mySQL database and deletes the .csv-file after the import. so far, so good. but in some cases the cronjobs starts running when the file is not completely
2
5949
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 anywhere that mentions this. When a logger opens a log file for append, is it automatically locked so other processes cannot write to it? And what happens if two or more processes attempt to log an event at the same time? Here's my...
2
6518
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 in the file ? do i have to add manual file locking, or does open()/fopen() provide file locking ? Thanks,
8
3812
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 the same time, and all will write to the same file. If I do an immediate flush() on the file after writing the line, is there still a risk that 2 simultaneous writes will collide? If so, what's the best way to handle this type of file write? ...
4
1586
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 the front end to render out site's navigation, site map, breadcrumbs, etc. We're using it because it's more efficient to just cache the XML file rather than doing recursive DB calls on each page load. We ran into a problem where, as far as we...
1
19968
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 it, it throws an exception. How to solave this problem? So second process can wait until first process completes its processing on the file. Thanks in advance
13
11157
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 writes a specific string to the log file. My original program (MKS Toolkit shell program) which keeps running "grep" checking the "exit string" on the "log files". There are no file sharing problem.
17
4895
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 some problems with this programm. I checked, the log files showed that the database was corrupted. The customer told me that there no 'illegal' operation such as pull out the plug, or kill the programm via task manager... So is there any...
15
3745
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 hard to track down database corruption errors. Amir
15
5257
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 process. How does one maintain file locking between sibling threads? - Matt
0
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.