C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 29th, 2009, 05:34 AM
Michi21609 Michi21609 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 2 Michi21609 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 32 sec
Reputation Power: 0
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!

Reply With Quote
  #2  
Old June 29th, 2009, 07:16 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 799 L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 19 h 2 m 59 sec
Reputation Power: 233
Send a message via AIM to L7Sqr
you could use flock. However, you should be aware of the caveats; from the man pages:
Quote:
Originally Posted by man flock
NOTES
flock(2) does not lock files over NFS. Use fcntl(2) instead: that
does work over NFS, given a sufficiently recent version of Linux and a
server which supports locking.

...

flock(2) places advisory locks only; given suitable permissions on a
file, a process is free to ignore the use of flock(2) and perform I/O
on the file.

flock(2) and fcntl(2) locks have different semantics with respect to
forked processes and dup(2).
__________________
Some people have 20 years of experience. Some have 1 year of experience 20 times.

My personal site: Basic geek randomness

Reply With Quote
  #3  
Old June 29th, 2009, 04:57 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,530 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 11 h 58 m 20 sec
Reputation Power: 603
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/

Reply With Quote
  #4  
Old July 2nd, 2009, 08:54 AM
Michi21609 Michi21609 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 2 Michi21609 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 32 sec
Reputation Power: 0
Used flock()... Works fine!

Thanks for your support!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Communication between two processes over textfile


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek