|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Communication between two processes over textfile
Hi!
I have the following problem: Two processes which communicate over a textfile. If the textfile is opened by one process the second process should notice that and usleep() for a while and try again after that. Is this possible with normal stdio.h functions (that just one process opens the file and is able to notice that file is already open) or do I have to use io.h? Or do I have to use a systemcall to see if file is already open? Does anyone have a solution? Kernel: 2.6.24 "Distribution": ELDK 4.2 (please google, I was not able to post url, forum blocked... :-( ) Thanks! |
|
#2
|
|||
|
|||
|
you could use flock. However, you should be aware of the caveats; from the man pages:
Quote:
__________________
Some people have 20 years of experience. Some have 1 year of experience 20 times. My personal site: Basic geek randomness |
|
#3
|
||||
|
||||
|
Another, more portable approach would be to have your writer and reader rename the file before using them. I have used additional dot extensions for such purposes in the past, usually version numbers with some other embedded stat information along the lines of whether file is pending a transaction or committed, etc. You can also use the presence of other specific file names as semaphores. The details would vary depending on the nature of the dependency between the processes using the file(s).
Given a modern OS, you shouldn't really have to resort to that sort of thing unless you have crash recovery issues to contend with. File system locks are often removed when a process quits or crashes and almost never survive a file system restart.
__________________
My worst nightmare was a pointless infinite loop. Work in progress; don't poke the curmudgeon! http://www.odonahue.com/ |
|
#4
|
|||
|
|||
|
Used flock()... Works fine!
Thanks for your support! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Communication between two processes over textfile |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|