ccplusplus.com
Learn C, C++ Concepts
Sunday, October 9, 2011
kill function example
/************************************************************************************** * File : kill-function-example.c * Author : Saurabh Gupta * Desc : kill function example c * kill function in c * Source : http://saurabhgupta0527.blogspot.com/2011/08/posix-signal-tutorial.html * Created : PM 06:23 09 October 2011 **************************************************************************************/ #include
#include
#include
#include
void sighup(); void sigint(); void sigquit(); int main() { int nPid; /* * get child process */ if ((nPid = fork()) < 0) { perror("fork"); exit(1); } if (nPid == 0) { signal(SIGHUP,sighup); signal(SIGINT,sigint); signal(SIGQUIT,sigquit); for(;;); } else {/* parent */ printf("\nSending SIGHUP\n"); /* nPid hold id of child */ kill(nPid,SIGHUP); sleep(2); /* pause for 3 secs */ printf("\nSending SIGINT\n"); kill(nPid,SIGINT); sleep(2); /* pause for 3 secs */ printf("\nSending SIGQUIT\n"); kill(nPid,SIGQUIT); printf ("\n"); sleep(2); } return 0; } void sighup() { /* * reset signal */ signal(SIGHUP,sighup); printf("CHILD: Received a SIGHUP\n"); } void sigint(){ /* * reset signal */ signal(SIGINT,sigint); printf("CHILD: Received a SIGINT\n"); } void sigquit() { printf("Got Killed\n"); exit(0); } /* * * [sgupta@rhel6x64 signal]$ gcc kill-function-example.c -o kill-function-example [sgupta@rhel6x64 signal]$ ./kill-function-example Sending SIGHUP Sending SIGINT Sending SIGQUIT [sgupta@rhel6x64 signal]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment