alarm signal processing


 
Thread Tools Search this Thread
Top Forums Programming alarm signal processing
# 1  
Old 06-05-2009
Error alarm signal processing

I'm writing a function right now, and I want to set an alarm to avoid a timeout, here's the general idea of my code:


int amt = -2;
alarm(10);
amt = read(fd, &t->buf[tailpos], TASKBUFSIZ - tailpos); //do a read

when the alarm goes off, i want to check the value of "amt"

basically, i want to check to see that the read call was completed in under 10 seconds, however in a signal handler, i dont think you can pass in the value of amt. so is my approach possible to implement? or would i need to a fork a process and do it that way? I'd prefer not to do that, I ran into some problems with that approach..

here's the entirety of the function as well:

taskbufresult_t read_to_taskbuf(int fd, task_t *t)
{
unsigned headpos = (t->head % TASKBUFSIZ);
unsigned tailpos = (t->tail % TASKBUFSIZ);
ssize_t amt;

if (t->head == t->tail || headpos < tailpos)
amt = read(fd, &t->buf[tailpos], TASKBUFSIZ - tailpos);
else
amt = read(fd, &t->buf[tailpos], headpos - tailpos);

if (amt == -1 && (errno == EINTR || errno == EAGAIN
|| errno == EWOULDBLOCK))
return TBUF_AGAIN;
else if (amt == -1)
return TBUF_ERROR;
else if (amt == 0)
return TBUF_END;
else {
t->tail += amt;
return TBUF_OK;
}
}

help would be much appreciated, my TAs suck...Smilie
# 2  
Old 06-05-2009
I think your approach using alarm and checking the return value of read makes sense. You should get back EINTR because the alarm signal handler went off.

Also, have you considered using select for this? Some implementations of select modify the timeout variable to indicate the amount of time not slept. You could use this to keep track of how much time you're willing to continue to sleep for the next select in the case of partial reads. Linux behaves this way. If you're not using Linux, you could use another function to track how long you've been in the function.

HTH
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Continue Processing after a signal is caught

Is it possible to continue after signal is caught and control goes to function specified in the trap statement? (3 Replies)
Discussion started by: Soham
3 Replies

2. Solaris

Signal Processing in unix

I've read the man page of singal(3) but I still can't quite understand what is the difference between SIGINT, SIGALRM and SIGTERM. Can someone tell me what is the behavioral difference among these 3 signals in kill command? Thanks! (2 Replies)
Discussion started by: joe228
2 Replies

3. Programming

Signal processing

We have written a deamon which have many threads. We are registering for the SIGTERM and trying to close main thread in this signal handling. Actually these are running on Mac OS X ( BSD unix). When we are unloading the deamon with command launchctl, it's sending SIGTERM signal to our process... (1 Reply)
Discussion started by: Akshay4u
1 Replies

4. Programming

Sinal-processing alarm()-function

hi programmers from all over the world, i am programming a simple program and want to deal with signals. i want to understand and work with the old signal-concept under unix, but i have a problem, hope you can help or knoew where i can get help. i use just one sig-handler, if a signal is... (1 Reply)
Discussion started by: xcoder44@gmx.de
1 Replies

5. Shell Programming and Scripting

Script Signal Processing

I am trying to develop a script that will properly handle kill signals particularly kill -2. I have program (_progres) that properly receives the signal if I run it from the command line directly: _progres -T /tmp -p /home/mejones/signal.p -b 2>&1 & If I try to put it in a script (i.e.... (2 Replies)
Discussion started by: mejones99
2 Replies

6. Programming

Basic signal and alarm usage

I am trying to write a program that will; 1) Show the message "Snoozing now...zzzz" on the screen for 5 seconds 2) Then in the same position show the message "The ALARM is going off now!" for 5 seconds 3) Repeat 1) then 2) infinitely until user presses Ctrl C I can't make it work. Any hints... (17 Replies)
Discussion started by: enuenu
17 Replies

7. Shell Programming and Scripting

Perl alarm signal

I am trying to write a signal to exit when a process times out. What I have come up with from poking around the web is this. #!/usr/bin/perl eval { local $SIG{ALRM} = sub { die "alarm clock restart" }; alarm 10; open(DSMADMC, "dsmadmc -se=tsmpc1 -id=XXXXX... (2 Replies)
Discussion started by: reggiej
2 Replies

8. UNIX for Dummies Questions & Answers

Alarm signal

Hi, when I execute a script on unix AIX, I've got an error message: "Execution: 85328 Signal d'alarme". If I edit this file with "vi", I ve got the same error after a while (about 1 minute). If I try with another user I still have the problem. But if I rename this file, no problem. My... (5 Replies)
Discussion started by: cgsteph
5 Replies

9. UNIX for Dummies Questions & Answers

Signal Processing

Hello, Can any body give example of using Unix Signals. What I want to do is I am running a sql query in a shell script I want, if sql query exceed the defined no. of seconds limit, then I would like to kill the process. I know this can be done thru Unix Signal Handling but I do not know... (8 Replies)
Discussion started by: sanjay92
8 Replies
Login or Register to Ask a Question