The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Post Shell programming: Question about source a file and read data from the file ccwq Shell Programming and Scripting 3 08-04-2007 07:28 PM
How can i read a non text file in unix - ELF-64 executable object file - IA64 alexcol UNIX for Advanced & Expert Users 4 07-20-2007 02:05 PM
questions about non-blocking read() oddabe High Level Programming 3 05-23-2007 09:54 PM
How to read specific lines in a bulk file using C file Programming rajan_ka1 High Level Programming 10 11-09-2005 11:29 PM
Blocking a Single IP Phobos UNIX for Dummies Questions & Answers 4 04-27-2005 08:09 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-01-2006
Registered User
 

Join Date: Jul 2006
Posts: 2
Stumble this Post!
Blocking file read

There are many processes writing to log files at random times (they are all programmed in different manners and can not be altered). I am writing a new c program which should read from the log files whenever they are updated. At the moment it is just spooling, waiting for a change in file size, but I really want to block until an update is made on one of the files.
Can this be done?
I thought select or poll might work but apparently they arent much use when your using regular files.
Also looked at using fifo's but seeing as I cant alter the original code it seems impossible.

Any ideas?

thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-01-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 356
Stumble this Post!
There are two types of locking mechanisms: mandatory and advisory. Mandatory systems will actually prevent read()'s and write()'s to file. Several Unix systems support them.

To make an advisory lock in POSIX-systems fcntl must be used:
Code:
#include <fcntl.h>
...

struct flock fl;
int fd;

fl.l_type   = F_RDLCK;  /* read lock */
fl.l_whence = SEEK_SET; /* beginning of file */
fl.l_start  = 0;        /* offset from l_whence */
fl.l_len    = 0;        /* length, 0 = to EOF */
fl.l_pid    = getpid(); /* PID */

fd = open("filename", O_RDONLY);

fcntl(fd, F_SETLKW, &fl); /* set lock */

...


fl.l_type   = F_UNLCK;
fcntl(fd, F_SETLK, &fl); /* unset lock */
Note that there are no guarantees with advisory locks, mandatory locks are not supported by all systems however
Reply With Quote
  #3 (permalink)  
Old 07-01-2006
Registered User
 

Join Date: Jul 2006
Posts: 2
Stumble this Post!
Thanks very much, I'll have a mess around and see if I can get it going.

thanks again
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0