The kill system call can be used to send any signal to any process group or process.
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);
For more information checkout: man 2 kill
There are restrictions that prevent you from using kill to send signals to any random process. These are intended to prevent antisocial behavior such as arbitrarily killing off processes belonging to another user. In typical use, kill is used to pass signals between parent, child, and sibling processes, and in these situations you normally do have permission to send signals. The only common exception is when you run a setuid program in a child process; if the program changes its real UID as well as its effective UID, you may not have permission to send a signal. The su program does this.
A process or thread can send a signal to itself with the raise function. The raise function takes just one parameter, a signal number.
In a single-threaded program it is equivalent to kill(getpid(), sig). In a multithreaded program it is equivalent to pthread_kill(pthread_self(), sig). If the signal causes a handler to be called, raise will only return after the signal handler has returned.
#include <signal.h>
int raise(int sig);
For more information checkout: man 3 raise
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
static volatile sig_atomic_t doneflag = 10;
static void setdoneflag(int signo) {
printf("\nIn SignalHandler - setdoneflag\n");
doneflag=0;
}
int main (void) {
signal(SIGINT, setdoneflag);
while(doneflag--)
{
printf("In While loop - %d\n",doneflag);
if(doneflag==5)
raise(2);
else
sleep(1);
}
printf("Program terminating ...\n");
return 0;
}
No comments :
Post a Comment
Your comments are moderated