For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > November 2005 > Communication with signal handlers









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 Communication with signal handlers
amidrunk@gmail.com

2005-11-15, 7:01 pm

Hi!

Is it possible to send a "context variable" to a signal handler? I need
to send stuff to a signal handler, but can't use global variables
(assignment in school; not allowed to use global variables). How can I
do this?

Sort of like this:

void my_handler(int signum, void* context)
{
char* string = (char*)context;
// string := "Hello World!"
}

int main()
{
sigaction_wctx(SIGXXX, my_handler, "Hello World!");
return 0;
}

Thanks in advance,
Nille

David Schwartz

2005-11-15, 7:01 pm


<amidrunk@gmail.com> wrote in message
news:1132087757.680193.310530@g44g2000cwa.googlegroups.com...

> Is it possible to send a "context variable" to a signal handler? I need
> to send stuff to a signal handler, but can't use global variables
> (assignment in school; not allowed to use global variables). How can I
> do this?


I would just use a global variable for this and explain in comments how
you basically have no other choice. There are some really odd ways to get
around it, but they all wind up using global variables under the hood, it's
just not so obvious that they do. For example, you could store the string in
an environment or in a file.

DS


Pascal Bourguignon

2005-11-15, 7:01 pm

amidrunk@gmail.com writes:
> Is it possible to send a "context variable" to a signal handler? I need
> to send stuff to a signal handler, but can't use global variables
> (assignment in school; not allowed to use global variables). How can I
> do this?


AFAIK, it's not possible. At least, not portably.

--
__Pascal Bourguignon__ http://www.informatimago.com/

Nobody can fix the economy. Nobody can be trusted with their finger
on the button. Nobody's perfect. VOTE FOR NOBODY.
amidrunk@gmail.com

2005-11-15, 7:01 pm

Thanks for your extremely quick responses! That's what I'll do -- I'll
XXXXX about the assignment! Always works... :)

Michael Kerrisk

2005-11-16, 3:59 am

On 15 Nov 2005 12:49:17 -0800, amidrunk@gmail.com wrote:

>Hi!
>
>Is it possible to send a "context variable" to a signal handler? I need
>to send stuff to a signal handler, but can't use global variables
>(assignment in school; not allowed to use global variables). How can I
>do this?
>
>Sort of like this:
>
>void my_handler(int signum, void* context)
>{
> char* string = (char*)context;
> // string := "Hello World!"
>}
>
>int main()
>{
> sigaction_wctx(SIGXXX, my_handler, "Hello World!");
> return 0;
>}


The following may be what you are after. If you use real-time signals
and sigqueue(pid, sig, value), then 'value' can be used to send data
to accompany the signal. Many modern implementations support
real-time signals (signals in the range SIGRTMIN to SIGRTMAX) and
sigqueue() (not some of the BSDs if I recall correctly).

The 'value' argument is a union:

union sigval {
int sival_int;
void *sival_ptr;
};

You can set one of these fields (the latter could be a pointer to data
_somwhere_) as desired. The signal handler must then be established
using the sigaction() SA_SIGINFO flag, in which case the second
argument of the handler:

void handler(int sig, siginfo_t *siginfo, void *ucontext);

is a pointer to a structure that includes the field:

siginfo->si_value /* union sigval si_value */

which contains the data sent with sigqueue().

Cheers,

Michael
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com