LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-28-2007, 03:45 PM   #1
shogun1234
Member
 
Registered: May 2004
Posts: 226

Rep: Reputation: 15
lock file between two process


I need to lock a file, which will be accessed by a Java program (multi-thread). The content of the file is written by a C programme. I try the file lock (similar to the book - Advanced Linux Programming). It doesn't work because when reading the file and paring it, it returned with a error, saying no content inside the file (but `cat file_name` I can see the content exactly there). How can I lock a file that can prevent other processes (the Java programme) access the file before the writing process (the C programme) finishes its jobs? Or what to do to prevent such problem occurs?

The code snippet of locking file in C I write is as follow:
Code:
...
        memset(&lock, 0, sizeof(lock));
        lock.l_type = F_WRLCK;
        fcntl(fd, F_SETLK, &lock);

        //prepare content
...
        /* unlocking file */
        lock.l_type = F_UNLCK;
        fcntl(fd, F_SETLK, &lock);
...
Thanks in advice,
 
Old 07-28-2007, 04:29 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Most file locking is "advisory locking", in which processes must cooperate with each other in order for the locking to take effect. If you're trying to lock out from a file any arbitrary unknown code, advisory locking won't work.

No, you want "mandatory locking". See:

http://linux-security.cn/ebooks/ulk3...12-SECT-7.html

For words from the Linux horse's mouth, see:

http://www.in-ulm.de/~mascheck/vario.../mandatory.txt

Hope this helps.
 
Old 07-28-2007, 10:54 PM   #3
binutils
Member
 
Registered: Feb 2007
Posts: 59

Rep: Reputation: 15
man select
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
lock a file sailajabhandaru Linux - Software 2 01-02-2006 03:37 AM
How to lock a file satimis Linux - General 2 03-09-2005 05:50 PM
Can't read lock file /tmp/.X0-lock squinn Linux - Newbie 3 07-13-2004 03:42 PM
Lock file ust Linux - Software 1 10-11-2003 02:11 AM
Failing to set Process ID lock when starting CommunigatePro robeb Linux - General 1 12-31-2002 12:33 PM

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

All times are GMT -5. The time now is 10:11 AM.

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