interrupt a blocking lock(F_SETLKW)


 
Thread Tools Search this Thread
Top Forums Programming interrupt a blocking lock(F_SETLKW)
# 1  
Old 12-21-2007
interrupt a blocking lock(F_SETLKW)

Hi all,

I use F_SETLKW to lock a file or keep waiting. How can I interrupt the blocking lock after waiting for a period? I want to add an argument to let user set waiting time. Maybe I can use non-blocking lock(F_SETLK) and a loop to check it. But it may consume too many resources. Any idea?Smilie

p.s: C/C++, LINUX platform

Thanks,
Marco Chen
# 2  
Old 12-21-2007
alarm/SIGALRM sounds like the right thing here.
# 3  
Old 12-21-2007
That what I'm trying now. But it's weird. I wrote a signal handler function to judge if set lock successfully or wait timeout. There are A, B ,C three processes trying to get file lock. A starts to lock file first, then B starts waiting, then C starts waitng. (alarm after ten seconds). Now A releases its lock but C gets file lock instead of B. I don't know how could it happen. Since I use "F_SETLKW", it sholud wait sequentially.

//timeout handler function
static void timeout_handler (int signum) {
struct flock fl = { F_WRLCK, SEEK_SET, 0, 0, 0 };
int fd;
fl.l_type = F_RDLCK;

printf("enter locking timeout check\n");

if ((fd = open("lock.c", O_RDWR)) == -1) {
perror("open");
exit(1);
}

if (fcntl(fd, F_SETLK, &fl) == -1){
printf("timeout pid: %d\n",fl.l_pid);
printf("Timeout!\n");
exit(1);
}
}

//main function as below
......

printf("Press <RETURN> to try to get lock: ");
getchar();
printf("Trying to get lock...");

struct sigaction action, old_action;
unsigned int old_timeout;

/* Cancel any existing alarm. */
old_timeout = alarm (0);

/* Establish signal handler. */
action.sa_handler = timeout_handler;
sigemptyset (&action.sa_mask);
action.sa_flags = SA_RESTART;
sigaction (SIGALRM, &action, &old_action);

alarm(TIMEOUT);

fcntl(fd, F_SETLKW, &fl);

/* Reset the signal handler and alarm. */
sigaction (SIGALRM, &old_action, NULL);
alarm(old_timeout);

........
# 4  
Old 12-21-2007
Quote:
Originally Posted by ktmchen
There are A, B ,C three processes trying to get file lock. A starts to lock file first, then B starts waiting, then C starts waitng. (alarm after ten seconds). Now A releases its lock but C gets file lock instead of B. I don't know how could it happen. Since I use "F_SETLKW", it sholud wait sequentially.
A, B and C are all independent processes. There is no rule that says that he who starts waiting for a lock first is the first to get it, but there is a rule that says only one can have the lock at any time.

It may not seem fair, but it is democratic. Smilie
# 5  
Old 12-22-2007
thank you, porter.
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. UNIX for Advanced & Expert Users

Is there any shell command to show which interrupt handler handle which interrupt number?

Hi, all: Is there any shell command to show which interrupt handler handle which interrupt number in the system? li,kunlun (5 Replies)
Discussion started by: liklstar
5 Replies

3. UNIX for Advanced & Expert Users

Testing privileges -lock lockfile /var/lock/subsys/..- Permission denied

Hi all, I have to test some user priviliges. The goal is to be sure that an unauthorized user can't restart some modules (ssh, mysql etc...). I'm trying to automate it with a shell script but in same cases I got the syslog broadcast message. Is there any way to simply get a return code... (3 Replies)
Discussion started by: Dedalus
3 Replies

4. UNIX for Advanced & Expert Users

Interrupt storm detected on "irq 20" throttling interrupt source

I receive the following warning messages on a very new machine which has FreeBSD 8.1 x64 installed on it: Interrupt storm detected on "irq 20" throttling interrupt source It is unclear what this means and what its origins are (motherboard? CPU? RAM?). I can start the desktop and the message is... (4 Replies)
Discussion started by: figaro
4 Replies

5. Red Hat

Security Question: Lock after invalid login, Session Lock and Required Minimum Password Length

Hello all, If anyone has time, I have a few questions: How do I do the following in Linux. We are using Red Hat and Oracle Enterprise Linux, which is based on Red Hat too. 1. How to lock the account after a few (like 3) invalid password attempts? 2. How do you lock a screen after 30... (1 Reply)
Discussion started by: nstarz
1 Replies

6. UNIX for Advanced & Expert Users

hunting down for software interrupt causes

Hi, i have an rhel box with around 20 %soft every 2 seconds. The box is idle. How do i start hunting down what's causing this? i believe /proc/interrupts is hardware related, procinfo is basically the same. where else can i look? thanks, Marc (5 Replies)
Discussion started by: marcpascual
5 Replies

7. Shell Programming and Scripting

Sending an interrupt in a script?

Is it possible to sent a ^C interrupt via the command line? For example if I want to tail a log for 10 minutes at a time, kill the tail and then start it again is there a way to go about that? I would imagine there would be some way to do it by finding and killing the PID, but I'm curious if... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. UNIX for Dummies Questions & Answers

timer interrupt

hello all since a process running in kernel mode cannnot be preempted by any other process what would be the status of Timer interrupt that occurs when the time quantum of a process is elapsed? thanks (2 Replies)
Discussion started by: compbug
2 Replies

9. UNIX for Advanced & Expert Users

Trapping Interrupt From 'ulimit'

I want to know the interrupt passed to a process through 'ulimit' I am running a process which gets killed when the 'ulimit -t' reaches. But after killing the process I want to start another process which would send a message or do some clean up or anything at all. To do the same I am... (5 Replies)
Discussion started by: mrnuttynuts
5 Replies

10. UNIX for Dummies Questions & Answers

how to lock keyboard without using lock command

how can I lock my keyboard while I'm away from the computer without using lock command. What other commands gives me the option to lock keyboard device? thanks (7 Replies)
Discussion started by: dianayun
7 Replies
Login or Register to Ask a Question