File locking

R

Rookie

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?
 
?

=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=

Rookie said:
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.
 
D

David Schwartz

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
 
R

Rookie

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?
 
C

Chris Torek

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,178
Messages
2,570,955
Members
47,509
Latest member
Jack116

Latest Threads

Top