LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-19-2007, 09:08 AM   #1
shogun1234
Member
 
Registered: May 2004
Posts: 226

Rep: Reputation: 15
wait free synchronization question


I have two programmes. One (a C programme) writes data into a file (e.g., A) . Then the other programme (a Java programme) would read data from the file A written by the C programme.

In order to prevent concurrency problem (Java and C programmes read/ write the file simultaneously), I make the C programme create another file called .lck and the Java programme must read the .lck file first. If the .lck file exists, the Java programme tries to read the .lck file again (constantly read whether the .lck exists or not) until the .lck file is deleted by the C programme. Then the Java programme reads the data of the file A. Beside, the way how the Java programme probes whether the .lck file exists or not is done through a while loop.

Code:
// create lck file object ...
boolean doLoop = true;
while(doLoop){
    if(lckFile.exists()){
        doLoop = false;
    }
}
It looks fine up to the present. However, I read some document saying that by using while loop would have performance implication, which is what I need to take into account. Although I have found some resources discussing the mechanism of wait-free synchronization that can be applied to such case. Unfortunately, it looks like those mechanism only provide for a single language. For example, java.util.concurrent.atomic. Moreover, the platforms currently my application runs on only support JDK1.4, which seems not support such methods. What should I do if I want to solve this problem?


I appreciate any suggestion,

Thank you very much.
 
Old 08-19-2007, 09:53 AM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
You have to think up some other way of communicating between the two processes. How about a Unix socket? Your Java program, when it gets to the point that it needs the file, waits for the C program to send it a message on the socket. The C program, on the other hand, writes the file, then sends a message to the socket when it's done. To avoid the same problem of waiting busily in filesystem checks, the socket should be made available before the C program needs it, so the C program never has to check to make sure the socket exists.

I think this might work, although I must admit (caveat emptor) that I've not done anything like this before! A better solution would be to move the functionality of your C program into your Java program (or vice-versa) but in the absence of this, message passing seems like a good idea.
 
Old 08-19-2007, 08:16 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
As above, alternately, forget the file and just send the data over a socket.
 
Old 08-20-2007, 09:37 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
well, at least you could use a FIFO.
they are a file and look after themselves as reading and writing and
act like a pipe.

though you can only have one reader and one writer at a time.
probably you can get away without changing your code.

But i would recommend creating and deleting the fifo in the
application each time.

mkfifo FIFO creates one.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A little question about SMP synchronization queuebil Programming 2 10-01-2006 03:59 AM
I/O Wait question DotHQ Red Hat 6 05-04-2006 12:00 PM
I/O Wait question DotHQ Red Hat 0 05-03-2006 02:06 PM
Wait Free Data Structure neo_119 Programming 0 11-27-2004 01:44 AM
email synchronization (not file synchronization) Moebius Linux - Software 6 10-05-2004 05:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration