File access from multiple processes or threads?


 
Thread Tools Search this Thread
Top Forums Programming File access from multiple processes or threads?
# 1  
Old 12-24-2009
Question File access from multiple processes or threads?

Hi,
Can anyone give me any idea when multiple processes access a file (like opening it, modifying it etc.) how can the synchronization can be done if they can access the same file at any time? How can this scenario is different from when multiple threads access a same file, modifying it etc- in this case how can this synchronization can be done?

It will be great help to me if you could give me some test programs in c/c++/linux/unix or some good links to follow.

Thanks in advance for your support.
sk
# 2  
Old 12-24-2009
In case of threads mutex can hold the write access ( which means only one thread writes at any time and all threads can read simultaneously ) . When some thread wants to write to file he should obtain the mutex first .

In linux ( ubuntu ) :
sudo apt-get install libpth-dev
man pth ( look for mutex )
# 3  
Old 12-24-2009
Thread synchronization is a big topic due to the various complexities involved. I agree with rr about where to look first. If you're coding on Linux, there is also the futex library which is worth looking at if the performance of the mutex is an issue.
# 4  
Old 12-25-2009
Thanks for the above replies guys, but I want a simple dummy example coe in linux for both scenarios. If you can provide it wud a gr8 help.
# 5  
Old 12-25-2009
I would suggest to go to :
Open Source Code Search Engine - Black Duck Koders
and search for mutex & futex there .
You will get as much example code as you can handle Smilie
# 6  
Old 12-26-2009
Hello
you can also do simple file locking using the fcntl.h
You would want to have a look at this.
fcntl(2): change file descriptor - Linux man page for a simple example.
Regards
# 7  
Old 12-26-2009
This is a complex topic.

Are your I/O operations synchronous or asynchronous? Are you using the O_NONBLOCK interface or the AIO interface? Are you going to use the buffer cache or using the O_DIRECT interface?

Synchronous I/O
Lets start with one process, one file descriptor, and multiple threads
You must use a muxtex like interface to protect the usage of the file descriptor since a file descriptor is an object of the process and not a thread.

If you duplicate the file descriptor, you need to protect the duplicated file descriptor using the same mutex because the duplicated file descriptor is a new number but points to the same file object (ie it is the same seek location)

File locking does nothing for you if you have one file descriptor since you already own the lock.

What happens if you open the file a second time within your process? File locking will work for you because you have another file structure.

What happens if a thread dies(crashes) that was using the second open file descriptor? The file descriptor is still open since the file descriptor is an object of the process. If there was a file lock on second file descriptor, then that file region becomes not accessible if you are using file locks.

What happens if you add a second process? First file locking is advisory and mandatory. This means another process can open your file unless you protect the file using file permissions.

File locking is the only OS interface for protecting against overlapping I/O operations but for cooperating processes you can build your own interface.

Asynchronous I/O interfaces have a different set of conventions and if you want answer here, you need to define your environment better because there are multiple solutions with different implementations.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to access multiple file and operate on single file

We have three files as mentioned below: 1. main_file.txt: This is the file in which all operations will be done. Which means this file will be signed by using the below two files 2. signature_file.txt: This is a status file and contains two signatures. 3. command.txt:file contains two commands... (2 Replies)
Discussion started by: chetanojha
2 Replies

2. Shell Programming and Scripting

Need to create multiple threads

Hi , i need to run multiple scripts parallely ,on my server....i have 8 cpus . planning to run minimum of 6 scripts paralley ....could you please suggest someone . thanks in advance , (3 Replies)
Discussion started by: Huvan
3 Replies

3. Linux

Linux Processes, Threads, scheduling header files

Hello everyone i am new to Linux. could anyone help me with the names of the Linux processes, threads and scheduling header files. I need them to write a documentation on Linux processes and threads. Thanks (5 Replies)
Discussion started by: bee_in_toronto
5 Replies

4. UNIX for Advanced & Expert Users

Monitoring cpu usage of mysql processes/threads/queries without any tool

hi all, i want to monitor mysql processes/threads/queries with respect to cpu usage.how can i do it? show processlist is of no use as no information abt cpu usage is given. plz help (7 Replies)
Discussion started by: rohitmahambre
7 Replies

5. UNIX for Advanced & Expert Users

Multiple processes write to log file at the same time

If we have 3 process to write to same log file at the same time like below. will it cause the data outdated because the multiple process writing same time? It this a safe way to keep the log for multiple process? p1 >> test.log &; p2 >> test.log &; p3 >> test.log & Thanks, (1 Reply)
Discussion started by: casttree
1 Replies

6. UNIX for Dummies Questions & Answers

kernel giving access for multiple users to access files

hi all, i want to know y kernel is giving access for multiple users to access a file when one user may be the owner is executing that file. Because other user can manipulate that file when the other user is executing that file, it will give the unexpected result to owner . plz help me... (1 Reply)
Discussion started by: jimmyuk
1 Replies

7. Shell Programming and Scripting

Multiple processes writing on the same file simultaneously

Hi All, I have encountered a problem,please help me. I have a script in which multiple processes are writing on to the same file . How should I stop this , I mean lock mechanism can be implemented or we can write the at different files and then concatenate the files. What would be a better... (1 Reply)
Discussion started by: Sayantan
1 Replies

8. IP Networking

How to choose Multiple process or Multiple threads?

Hi All, Please explain me when i have to use multiple process and when I have to use Multiple threads? Please give me an example.It will be very helpful for me. Thanks in advance. (0 Replies)
Discussion started by: ashleykumar
0 Replies

9. Post Here to Contact Site Administrators and Moderators

access old threads

i would like to see some of my olds post and threads so i can compile them for future use but when i tried to search all of my post some of them were not present. how can i have access from these threads? thanks a lot! (1 Reply)
Discussion started by: inquirer
1 Replies

10. Programming

How to use pipe() in multiple threads?

Hi, I have a program that runs two threads in stead of two processes. I want to use pipe to redirect the output of the first thread to the input of the second thread. One thread is continuously writing to a pipe, and the other thread will read from the pipe. How do I do that? Is there... (2 Replies)
Discussion started by: wminghao
2 Replies
Login or Register to Ask a Question