|
Home > Archive > Unix Programming > April 2004 > Is this is an alarm or a signal handler
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Is this is an alarm or a signal handler
|
|
| LeTubs 2004-04-29, 9:34 pm |
| Hi
I'm not sure if this is correct ...place to post but here goes
This is what i'm trying to do, I want to write a signal / alarm handler
( I don't know which hence the posting here, as once I know the proper
name /useage then I'll use google to try and find out more
information )....
Thus any tips or pointers would be helpful (and I'm using a UNIX variant as
OS)
Many Thanks
David
the (?) means I'm not sure if I need the variable or not...
xxxxxxx( &MyTimeFunction, (?)(void *)SleepCounter );
do_some_other_work(......)
..
..
..
/* Check to see if the sleep counter has been set (ie it has
waited a specific time or timeout period
*/
xxxxxxx( SleepCounter );
void * MyTimeFunction( void * arg ){
int MySleepCounter = (int) arg;
for ( i = 0; i < SOME_SYSTEM_COUNTER; i++ ){}
/* Not sure on this bit also but want to set a value of 1
to denote the sleep time has been set
*/
SleepCounter = 1;
}
| |
| Rich Teer 2004-04-29, 10:34 pm |
| On Fri, 30 Apr 2004, LeTubs wrote:
> This is what i'm trying to do, I want to write a signal / alarm handler
> ( I don't know which hence the posting here, as once I know the proper
> name /useage then I'll use google to try and find out more
> information )....
Signal is the generic term; an alarm is a type of signal. You
set an alarm to go off after a certain period of time.
> Thus any tips or pointers would be helpful (and I'm using a UNIX variant as
> OS)
I'm not sure what you're trying to accomplish! A signal handler
is a function that is called when your process receives the signal,
assuming you've changed its disposition from the default. The
handler is passed one argument, which is the number of the signal
that caused the handler to be called.
What are you trying to do? Do you want to recieve an alarm clock
signal, or do you want to catch signals in general?
Tell you what, here's some example code that should illustrate
the principles. It won't compile without change, unless you happen
to have the example source code from my (soon to be released) book,
Solaris Systems Programming. The function called sigusr is the
signal handler in this program.
Very briefly, the program sets up a signal handler that is shared
by two signals. When one of the two signals is sent to the process,
its name is printed. Naturally, this program doesn't do much on its
own, but it should help you understand what is going on. For more
details, buy my book. :-)
I use tabstops of 4, so set your editor accordingly...
-------------------------------------8<----------------------------------
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include "ssp.h"
static volatile sig_atomic_t sig;
static void sigusr (int sig_num);
int main (void)
{
if (sigset (SIGUSR1, sigusr) == SIG_ERR)
err_msg ("Can't catch SIGUSR1");
if (sigset (SIGUSR2, sigusr) == SIG_ERR)
err_msg ("Can't catch SIGUSR2");
for (;;) {
pause ();
if (sig == SIGUSR1)
printf ("Received SIGUSR1\n");
else
printf ("Received SIGUSR2\n");
}
}
static void sigusr (int sig_num)
{
if ((sig_num == SIGUSR1) || (sig_num == SIGUSR2))
sig = sig_num;
else
err_dump ("Received signal %d", sig_num);
}
-------------------------------------8<----------------------------------
Enjoy,
--
Rich Teer, SCNA, SCSA
President,
Rite Online Inc.
Voice: +1 (250) 979-1638
URL: http://www.rite-online.net
| |
| LeTubs 2004-04-29, 10:34 pm |
|
"Rich Teer" <rich.teer@rite-group.com> wrote in message
news:Pine.SOL.4.58.0404291835230.25717@zaphod.rite-group.com...
> On Fri, 30 Apr 2004, LeTubs wrote:
<snip>
> I'm not sure what you're trying to accomplish! A signal handler
> is a function that is called when your process receives the signal,
> assuming you've changed its disposition from the default. The
> handler is passed one argument, which is the number of the signal
> that caused the handler to be called.
>
> What are you trying to do? Do you want to recieve an alarm clock
> signal, or do you want to catch signals in general?
What I'm trying to is to start a timer, then a some time later
determine if the timer counter has expired...if not wait untill
that timer has expired.
Now I don't know if this type of impletation is a signal/Alarm handler
or something else. However thanks for the info
>
> Tell you what, here's some example code that should illustrate
> the principles. It won't compile without change, unless you happen
> to have the example source code from my (soon to be released) book,
> Solaris Systems Programming. The function called sigusr is the
> signal handler in this program.
>
> Very briefly, the program sets up a signal handler that is shared
> by two signals. When one of the two signals is sent to the process,
> its name is printed. Naturally, this program doesn't do much on its
> own, but it should help you understand what is going on. For more
> details, buy my book. :-)
>
> I use tabstops of 4, so set your editor accordingly...
>
> -------------------------------------8<----------------------------------
> #include <stdio.h>
> #include <unistd.h>
> #include <signal.h>
> #include "ssp.h"
>
> static volatile sig_atomic_t sig;
>
> static void sigusr (int sig_num);
>
> int main (void)
> {
> if (sigset (SIGUSR1, sigusr) == SIG_ERR)
> err_msg ("Can't catch SIGUSR1");
> if (sigset (SIGUSR2, sigusr) == SIG_ERR)
> err_msg ("Can't catch SIGUSR2");
>
> for (;;) {
> pause ();
> if (sig == SIGUSR1)
> printf ("Received SIGUSR1\n");
> else
> printf ("Received SIGUSR2\n");
> }
> }
>
> static void sigusr (int sig_num)
> {
> if ((sig_num == SIGUSR1) || (sig_num == SIGUSR2))
> sig = sig_num;
> else
> err_dump ("Received signal %d", sig_num);
> }
> -------------------------------------8<----------------------------------
>
> Enjoy,
>
> --
> Rich Teer, SCNA, SCSA
>
> President,
> Rite Online Inc.
>
> Voice: +1 (250) 979-1638
> URL: http://www.rite-online.net
| |
| Barry Margolin 2004-04-30, 12:34 am |
| In article <uWikc.46$zz5.30@newsfe2-gui.server.ntli.net>,
"LeTubs" <dlaw001@yahoonospam.co.uk> wrote:
> What I'm trying to is to start a timer, then a some time later
> determine if the timer counter has expired...if not wait untill
> that timer has expired.
> Now I don't know if this type of impletation is a signal/Alarm handler
> or something else. However thanks for the info
Establish a handler for the SIGALRM signal; the handler should set a
variable. Call alarm() -- it will set up a timer that will cause that
signal to be sent when the timer expires. Later, check the variable to
see if it has the value that the handler sets. If not, it means the
timer hasn't expired; call sigwait() to wait for the signal to interrupt
you.
Actually, I think it's probably easier without the timer. Start by
saving the time in a variable. Then some time later, get the current
time and compare it to the saved time. If the difference is less than
the expire time, call sleep() to wait for the remainder of the time.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
| |
| Måns Rullgård 2004-04-30, 3:33 am |
| Barry Margolin <barmar@alum.mit.edu> writes:
> In article <uWikc.46$zz5.30@newsfe2-gui.server.ntli.net>,
> "LeTubs" <dlaw001@yahoonospam.co.uk> wrote:
>
>
> Establish a handler for the SIGALRM signal; the handler should set a
> variable. Call alarm() -- it will set up a timer that will cause that
> signal to be sent when the timer expires. Later, check the variable to
> see if it has the value that the handler sets. If not, it means the
> timer hasn't expired; call sigwait() to wait for the signal to interrupt
> you.
What if the signal arrives between your checking the variable and
calling sigwait()?
--
Måns Rullgård
mru@kth.se
| |
| LeTubs 2004-04-30, 5:34 am |
|
"Måns Rullgård" <mru@kth.se> wrote in message
news:yw1xbrlaot86.fsf@kth.se...
> Barry Margolin <barmar@alum.mit.edu> writes:
>
>
> What if the signal arrives between your checking the variable and
> calling sigwait()?
Thanks for all the ideas, but what I'm now thinking of doing is creating a
thread
with the sole purpose of looping through a counter. Then using pthread_join
to determine if the thread has exited or not. I haven't tried this but what
I'm
going to do is to sit down and go through all of your suggestions and see
what
works for me. Thanks for all the input, it is very much appreciated.
David
> --
> Måns Rullgård
> mru@kth.se
| |
| Barry Margolin 2004-04-30, 4:34 pm |
| In article <yw1xbrlaot86.fsf@kth.se>, Måns Rullgård <mru@kth.se> wrote:
> Barry Margolin <barmar@alum.mit.edu> writes:
>
>
> What if the signal arrives between your checking the variable and
> calling sigwait()?
You should block the signal before checking the variable and calling
sigwait(). That's why sigwait() takes a signal mask as a parameter --
it atomically unmasks the specified signals before waiting.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
|
|
|
|
|