Blocking file read


 
Thread Tools Search this Thread
Top Forums Programming Blocking file read
# 1  
Old 07-01-2006
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
# 2  
Old 07-01-2006
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
# 3  
Old 07-01-2006
Thanks very much, I'll have a mess around and see if I can get it going.

thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Which are blocking and non-blocking api's in sockets in C ?

among the below socket programming api's, please let me know which are blocking and non-blocking. socket accept bind listen write read close (2 Replies)
Discussion started by: VSSajjan
2 Replies

2. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

3. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

4. IP Networking

ping blocking

Hi I am starting to practice nmap for my own education. Now I created two host in virtual box. Bot are scientific linux, one in installed as web server and the other as developing station. I tried to run nmap on so I did nmap on their IP address, I got an answer that ip is down or that... (8 Replies)
Discussion started by: programAngel
8 Replies

5. Shell Programming and Scripting

Script for Removing Lines from File / Blocking internet connection

Hey all. I am trying to write some scripts and need some assistance. One: I already have a script that appends lines to a file. I need a script that will remove those lines from that file, and have no idea how to go about doing this. Just need the command (if any) that can remove lines. ... (2 Replies)
Discussion started by: Dysruption
2 Replies

6. UNIX for Advanced & Expert Users

ps blocking

Hi Folks I have been debugging a script that is called every thirty seconds. Basically it is doing a ps, well two actually, one to file (read by the getline below) and the other into a pipe. The one into the pipe is: - V_SYSVPS=/usr/sysv/bin/ps $V_SYSVPS -p$PIDLIST -o$PSARGS... (0 Replies)
Discussion started by: steadyonabix
0 Replies

7. Infrastructure Monitoring

Blocking File Uploads with Squid

Dear All I want to block email attachments upload on internet through different mail servers. My requirement is that no user can send email attachments on yahoo, hotmail, gmail etc. I have RHEL-5 and squid 2.7. I have applied the undermentioned ACL but it in vain ACL is acl fileupload... (2 Replies)
Discussion started by: surfer24
2 Replies

8. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

9. Shell Programming and Scripting

Non-blocking pipe

Hello, Would this be an acceptable way of creating a non-blocking pipe. Basically I want to create kind of a server client arch. This code would be in the server, and I don't want to have to wait for clients to read before moving on to the next client. One problem I can see is if... (4 Replies)
Discussion started by: cdlaforc
4 Replies

10. Programming

questions about non-blocking read()

Hi, I need to read incoming data from a serial port. I also need the read() to be non-blocking, such that my program can then check for any data to be send via the serial port. I followed some FAQs on the net, set read() to be non-blocking, and got EAGAIN errors popping out. I realise that... (3 Replies)
Discussion started by: oddabe
3 Replies
Login or Register to Ask a Question