Sponsored Content
Top Forums Programming interrupt a blocking lock(F_SETLKW) Post 302152752 by ktmchen on Friday 21st of December 2007 04:08:47 AM
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);

........
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
pthread_rwlock_rdlock(3)				     Library Functions Manual					  pthread_rwlock_rdlock(3)

NAME
pthread_rwlock_rdlock - Acquires a read-write lock for read access. LIBRARY
DECthreads POSIX 1003.1c Library (libpthread.so) SYNOPSIS
#include <pthread.h> int pthread_rwlock_rdlock( pthread_rwlock_t *rwlock); PARAMETERS
Address of the read-write lock object to acquire for read access. DESCRIPTION
This routine acquires a read-write lock for read access. If no thread already holds the lock for write access and there are no writers waiting to acquire the lock, the lock for read access is granted to the calling thread and this routine returns. If a thread already holds the lock for read access, the lock is granted and this routine returns. A thread can hold multiple, concurrent locks for read access on the same read-write lock. In a given thread, for each call to this routine that successfully acquires the same read-write lock for read access, a corresponding call to pthread_rwlock_unlock must be issued. If some thread already holds the lock for write access, the calling thread will not acquire the read lock. If the read lock is not acquired, the calling thread blocks until it can acquire the lock for read access. Results are undefined if the calling thread has already acquired a lock for write access on rwlock when this routine is called. If the read-write lock object referenced by rwlock is not initialized, the results of calling this routine are undefined. If a thread is interrupted (via a Tru64 UNIX signal or an OpenVMS AST) while waiting for a read-write lock for read access, upon return from the interrupt routine the thread resumes waiting for the lock as if it had not been interrupted. RETURN VALUES
If an error condition occurs, this routine returns an integer value indicating the type of error. Possible return values are as follows: Successful completion. The lock for read access could not be acquired because the maximum number of read lock acquisitions for rwlock has been exceeded. The current thread already owns the read-write lock for writing. The value specified by rwlock does not refer to an ini- tialized read-write lock object. ERRORS
None RELATED INFORMATION
Functions: pthread_rwlock_init(3), pthread_rwlockattr_init(3), pthread_rwlock_tryrdlock(3), pthread_rwlock_wrlock(3), pthread_rwlock_unlock(3) Manuals: Guide to DECthreads and Programmer's Guide delim off pthread_rwlock_rdlock(3)
All times are GMT -4. The time now is 05:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy