LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-08-2005, 05:22 PM   #1
Arcades
LQ Newbie
 
Registered: Jul 2003
Location: Auckland, New Zealand
Distribution: Vector Linux
Posts: 21

Rep: Reputation: 15
Question flock and touch


Hello, despite looking on google (possibly im searching without vital keywords?) and a forum search I can't seem to dig up anything usefull on flock and touch in linux.

Im looking to lock a file with flock(), and I think touch() is used to prevent a file from accidentally being permenantly locked?

Any relevant url's or even better code snippet examples would be VERY appreciated, as I have no idea how to use either of these, and my guessing attempts, while compiling don't seem to lock the file, nor return any perror.

EDIT: I should add, this is in C. I can find a few examples in other situations, that I don't understand how to use in C.

Last edited by Arcades; 03-08-2005 at 11:52 PM.
 
Old 03-09-2005, 12:56 AM   #2
Arcades
LQ Newbie
 
Registered: Jul 2003
Location: Auckland, New Zealand
Distribution: Vector Linux
Posts: 21

Original Poster
Rep: Reputation: 15
Bump, one last attempt for attention before I let the thread die!
 
Old 03-09-2005, 08:34 AM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
man flock
man touch

That man page for touch is for the shell command touch. I'm not seeing any man pages for a function form of touch on my Slackware system, but I'm assuming it would do the same thing.

P.S. The forum rules indicate that you should wait 24 hours before bumping a topic. Not everyone reading these forums are in the same timezone as you.
 
Old 03-09-2005, 06:39 PM   #4
Arcades
LQ Newbie
 
Registered: Jul 2003
Location: Auckland, New Zealand
Distribution: Vector Linux
Posts: 21

Original Poster
Rep: Reputation: 15
Ok, thanks for having a look.

Im a relatively new with C, and to be honest don't understand what Im doing wrong in this case.

using the man pages for flock, has got me upto this point...

Code:
#include <stdio.h>
#include <sys/file.h>

/* file locking! */

int main()
{
	int fd;
	
	if ((fd = open("text.txt", O_RDWR)) < 0 ) { /* get file descriptor */
		perror("\nOpening error: ");
		exit(2);
	}
	
	if (flock(fd, LOCK_EX) < 0) { /* lock file */
		perror("\nLocking error: ");
		exit(1);
	}
	
	char tmp; /* wait for keypress */
	scanf("\n", &tmp);
		
	flock(fd, LOCK_UN); /* unlock file */
	
	return 0;
}
I think this should lock "text.txt" . The scanf gives me a pause, in which I can go and edit the file to test.

So far, it doesnt lock it.

Last edited by Arcades; 03-09-2005 at 06:43 PM.
 
Old 03-11-2005, 12:27 AM   #5
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
From the man page:

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.

In other words, you have to check for a lock explicitly, it's not handled automatically for you...
 
Old 03-11-2005, 05:36 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by chrism01
In other words, you have to check for a lock explicitly, it's not handled automatically for you...
It is possible though using fcntl().
Code:
Quote from "man fcntl" 

   Mandatory locking
       (Non-POSIX.)   The above record locks may be either advisory or mandatory, and are advi-
       sory by default.  To make use of mandatory locks,  mandatory  locking  must  be  enabled
       (using  the  "-o mand" option to mount(8)) for the file system containing the file to be
       locked and enabled on the file itself (by disabling group execute permission on the file
       and enabling the set-GID permission bit).

       Advisory  locks  are  not  enforced  and  are useful only between cooperating processes.
       Mandatory locks are enforced for all processes.
If those requirements are satisfied, you should be able to lock an open file descriptor with a function like:
Code:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int lock(int fd)
{
    struct flock lock;

    lock.l_type = F_WRLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len = 0;
    lock.l_pid = -1;
    if (fcntl(fd, F_SETLK, &lock) < 0) {
        perror("Locking file");
        return 0;
    }
    return 1;
}
 
Old 03-11-2005, 08:13 PM   #7
Arcades
LQ Newbie
 
Registered: Jul 2003
Location: Auckland, New Zealand
Distribution: Vector Linux
Posts: 21

Original Poster
Rep: Reputation: 15
Thank you VERY, VERY much!

chrism01, I missed that point, and I feel like slapping myself for it. Thanks for pointing it out!

Hko, that is what I need then! - Thank you for the example
 
  


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
One Touch backup versus One Touch II q6supvr Linux - Software 4 12-16-2007 08:28 PM
flock problem climbingmerlin Programming 3 10-20-2005 09:36 AM
do we have concept of flock in solaris invinciblemk Solaris / OpenSolaris 3 05-23-2005 08:02 PM
Error - can't touch touch: /var/lock - filesystem problem? xanas3712 Linux - Newbie 0 05-10-2004 05:26 AM
Problem s with undefined sysmbol-flock invinciblemk Linux - General 1 03-23-2004 05:40 PM

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

All times are GMT -5. The time now is 11:30 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