473,804 Members | 2,123 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 4075
"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
9121
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
5959
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
6521
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
3813
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
1590
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
19970
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
11160
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
4896
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
3747
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
5260
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
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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...
0
10352
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10354
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
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
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
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.