using a signal handler to kill processes

This is a discussion on using a signal handler to kill processes within the C Programming forums, part of the General Programming Boards category; I'm writing a program that needs to accept PID's of running processes (*NIX) from the command line. So the call ...

  1. #1
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40

    using a signal handler to kill processes

    I'm writing a program that needs to accept PID's of running processes (*NIX) from the command line. So the call to my program would look like
    Code:
    $ ./myprogram 1324 1328 1400
    that part is all fine and dandy, using argc and argv... but then I have to generate a signal (SIGALRM) every second and use the function that handles the signal to send SIGINT to the processes. I'm not sure how to get the PID #'s into my kill function. This is what I've got so far, but I'm help up for the moment unless I can think of a better way to do it. My problem is that the signal function that handles the alarm doesn't let me pass arguments to the function that I want to call (I don't think) Anybody have a better idea of how to approach this? Thanks
    Code:
    #include <stdio.h>
    #include <signal.h>
    #include <sys/time.h>
    
    void killer(int);
    int set_timer(int);
    
    int main(int argc, char* argv[])
    {
      int n;  //will hold PID's passed in as arguments
    
      n = atoi(argv[i]); //need a loop or something here to get all of the args
    
      signal(SIGALRM, killer);
      set_timer(1);
      for ( ; ; );
      return 0;
    }
    
    int set_timer(int seconds)
    {
      struct itimerval timer;
    
      timer.it_interval.tv_sec = seconds;
      timer.it_interval.tv_usec = 0;
      timer.it_value.tv_sec = seconds;
      timer.it_value.tv_usec = 0;
    
      return setitimer(ITIMER_REAL, &timer, NULL);
    }
    
    void killer(int signum)
    {
      for( i = 1; i < ; ++i)
      {
        kill(n, SIGINT);
      }
      return 0;
    }
    straight off the heap

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I know that global variables are to be avoided, but...

    Code:
    # define MAX_PIDS 10
    static int count = 0;
    static int pids[MAX_PIDS];
    
    int main() 
    {
        /* Initialize pids array ... */
    
        return 0;
    }
    
    void killer(int signum)
    {
      for( i = 1; i <count ; ++i)
      {
        kill(pids[i], SIGINT);
      }
      return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    ok. [sshhhh!] I did it with globals [/sshhhh!]

    thnx.
    dinjas
    straight off the heap

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-07-2009, 11:05 AM
  2. Signal handler function - pointer to this gets lost
    By maus in forum C++ Programming
    Replies: 1
    Last Post: 07-01-2009, 10:10 AM
  3. Kill Signal Number? - Help !
    By brett in forum C Programming
    Replies: 5
    Last Post: 06-20-2007, 04:39 AM
  4. signal handler
    By falconetti in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21