Search code examples
cposix

Passing function with arguments to a signal handler?


I can successfully pass a function to signal(), but I cannot come up with a way to pass an argument to a function without using static or a global variable. functionontimer just updates an array.

if (signal(SIGALRM, (void (*)(int)) functionontimer) == SIG_ERR) 
{
return(-1);
}

Is there any way to pass a pointer along with the function without using static or a global variable? I would ideally see functiontimer be defined something like functiontimer(int *data, index).


Solution

  • Is there anyway to pass a pointer along with the function without using static or a global variable? I would ideally see functiontimer be defined something like functiontimer(int *data, index)

    No, there is not, and if you force in a function that expects an additional argument, such as by casting it as you show in your example, then undefined behavior ensues when the system calls it with the number and type of arguments it expects to use instead of the number and type of arguments that the function is actually defined to receive.

    It's unclear for what purpose you want the proposed behavior, but it may well be that signal handling does not serve that purpose. It certainly does not serve the purpose in the way you hope.