Thread: Pass user parameter to signal handler in linux

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    Pass user parameter to signal handler in linux

    Hi
    In my program, I want to use signal handler so that if signal alarm is sent to the process my function is to called.till no everything is ok.but I want to pass some parameters to signal handler.but "signal" function does not have any parameter for this. according to it's prototype:
    Code:
    sighandler_t signal (int signum, sighandler_t action)

    Thanks for any help or guidance

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    there's also sigaction, but it also doesn't allow a user-defined pointer to be sent to the handler. there may be a way to do it, but as far as I know, signal and sigaction are the only way to get signals in linux.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Unfortunately there is no way to pass user data to the signal handler. This is actually one of the few cases where global variables are an acceptable solution.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    A signal handler is a global object in every sense. If you need to "pass" data into it, it must be done with a global variable.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Hi
    Thanks for your reply.
    I did not want to use Global Variable at all.But I think there is no any way.
    Maybe it's better that I study more on limitation and disadvantage of global variables.I always think that use of Global Variable is bad idea in program.

    Thanks in advance

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can put the signal handler in a file by itself like this:

    Code:
    static void *Data;
    
    void initializeData (void *p) { 
    	Data = p; 
    }
    
    void handler (int sig) {
    	// access *Data here
    }
    Then you call initializeData() before you register the handler with signal(). Since *Data is declared static, only handler() has access to it.

    However, if you are adverse to globals, you might also want to think about how a signal handler is itself "a global object in every sense" even if it doesn't access any data, implying it has a restricted use value. What signal do you want to catch and why?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    In our program I have used timer for some purpose. I have used "create_time" with signal alarm,after timer expiration, one signal alarm is sent to my process.
    So I have to handle signal in my program.

    Thanks again

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    What's the purpose of the timer? If the signal handler initiates a sequence where you are writing to anything used elsewhere, or reading data that the process might have been writing to at the time, I would think this is subject to the same caveats as threading, namely, that even with a single int, it is possible that you occasionally interrupt a write halfway through (meaning, reading from that int will produce a garbage value), in which case you would be better off actually using a thread and proper thread safe locks on the data.

    Code:
    #include <stdio.h>
    #include <pthread.h>
    #include <unistd.h>
    
    // compile C99 -lpthread
    
    // for passing data to the timer
    struct threadData {
    	pthread_mutex_t *lock;
    	int sleepTime;
    	int *data;
    };
    
    // timer function
    void *timer (void *data) {
    	struct threadData *td = data;
    
    	sleep(td->sleepTime);
    
    	pthread_mutex_lock(td->lock);
    	// reset
    	*(td->data) = 0;
    	pthread_mutex_unlock(td->lock);
    
    	return NULL;
    }
    
    int main(void) {
    	pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
    	int x = 1;
    	struct threadData data = {
    		.lock = &lock,
    		.sleepTime = 3,
    		.data = &x
    	};
    	pthread_t id;
    
    // create and detach the timer
    	pthread_create(&id, NULL, timer, (void*)&data);
    	pthread_detach(id);
    	
    // do stuff until the timer goes off
    	while (1) {
    		pthread_mutex_lock(&lock);
    		if (!x) break;
    		printf("%d\n", x++);
    		pthread_mutex_unlock(&lock);
    	}
    
    	printf("done\n");
    
    	pthread_mutex_destroy(&lock);
    
    	return 0;
    }
    Last edited by MK27; 04-03-2012 at 07:58 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The only way I know to do this portably would see you using a thread to watch a variable triggered by the signal anyway; you should just use "Posix" facilities from the outset.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handler does not print
    By barramundi9 in forum C Programming
    Replies: 4
    Last Post: 07-27-2011, 11:20 PM
  2. Signal handler for assert()
    By jeffcobb in forum Linux Programming
    Replies: 17
    Last Post: 06-10-2010, 08:36 PM
  3. Replies: 9
    Last Post: 10-19-2009, 04:46 PM
  4. using a signal handler to kill processes
    By dinjas in forum C Programming
    Replies: 2
    Last Post: 03-16-2005, 12:58 PM
  5. signal handler
    By falconetti in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 07:54 PM