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;
}