IPC: signal example:
Signals are software generated interrupts that are sent to a process when a event happens. Signals can be synchronously generated by an error in an application, such as SIGFPE and SIGSEGV, but most signals are asynchronous. Signals can be posted to a process when the system detects a software event, such as a user entering an interrupt or stop or a kill request from another process. Signals can also be come directly from the OS kernel when a hardware event such as a bus error or an illegal instruction is encountered. The system defines a set of signals that can be posted to a process. Signal delivery is analogous to hardware interrupts in that a signal can be blocked from being delivered in the future. Most signals cause termination of the receiving process if no action is taken by the process in response to the signal. Some signals stop the receiving process and other signals can be ignored. Each signal has a default action which is one of the following:
why we need to handle the singal:
This program is a simple demonstration of signals. Signals are like sofware interrupts, that are used to inform a process of the occurence of an event. Programs can be designed to catch a a signal, by providing a function to handle it.
For example, when shutting down a Linux box, the SIGTERM signal is sent to all processes. Processes that catch this signal, can properly terminate (e.g. de-allocate resources, close all open files). sighandler_t signal(int signum, sighandler_t handler);
pgm example1:
fork() creates the child process from the parent. The pid can be checked to decide whether it is the child (== 0) or the parent (pid = child process id).
The parent can then send messages to child using the pid and kill().
The child picks up these signals with signal() and calls appropriate functions.
An example of communicating process using signals is sig_talk.c:
http://www.cs.cf.ac.uk/Dave/C/node24.html
http://www.amparo.net/ce155/signals-ex.html
www.amparo.net/ce155/signals-ex.html
IPC:Interrupts and Signals:
In this section will look at ways in which two processes can communicate. When a process terminates abnormally it usually tries to send a signal indicating what went wrong. C programs (and UNIX) can trap these for diagnostics. Also user specified communication can take place in this way.Signals are software generated interrupts that are sent to a process when a event happens. Signals can be synchronously generated by an error in an application, such as SIGFPE and SIGSEGV, but most signals are asynchronous. Signals can be posted to a process when the system detects a software event, such as a user entering an interrupt or stop or a kill request from another process. Signals can also be come directly from the OS kernel when a hardware event such as a bus error or an illegal instruction is encountered. The system defines a set of signals that can be posted to a process. Signal delivery is analogous to hardware interrupts in that a signal can be blocked from being delivered in the future. Most signals cause termination of the receiving process if no action is taken by the process in response to the signal. Some signals stop the receiving process and other signals can be ignored. Each signal has a default action which is one of the following:
- The signal is discarded after being received
- The process is terminated after the signal is received
- A core file is written, then the process is terminated
- Stop the process after the signal is received
- Hardware conditions
- Software conditions
- Input/output notification
- Process control
- Resource control
why we need to handle the singal:
This program is a simple demonstration of signals. Signals are like sofware interrupts, that are used to inform a process of the occurence of an event. Programs can be designed to catch a a signal, by providing a function to handle it.
For example, when shutting down a Linux box, the SIGTERM signal is sent to all processes. Processes that catch this signal, can properly terminate (e.g. de-allocate resources, close all open files). sighandler_t signal(int signum, sighandler_t handler);
pgm example1:
/* Includes */ #include <stdio.h> /* Input/Output */ #include <stdlib.h> /* General Utilities */ #include <signal.h> /* Signal handling */ /* This will be our new SIGINT handler. SIGINT is generated when user presses Ctrl-C. Normally, program will exit with Ctrl-C. With our new handler, it won't exit. */ void mysigint() { printf("I caught the SIGINT signal!\n"); return; } /* Our own SIGKILL handler */ void mysigkill() { printf("I caught the SIGKILL signal!\n"); return; } /* Our own SIGHUP handler */ void mysighup() { printf("I caught the SIGHUP signal!\n"); return; } /* Our own SIGTERM handler */ void mysigterm() { printf("I caught the SIGTERM signal!\n"); return; } int main() { /* Use the signal() call to associate our own functions with the SIGINT, SIGHUP, and SIGTERM signals */ if (signal(SIGINT, mysigint) == SIG_ERR) printf("Cannot handle SIGINT!\n"); if (signal(SIGHUP, mysighup) == SIG_ERR) printf("Cannot handle SIGHUP!\n"); if (signal(SIGTERM, mysigterm) == SIG_ERR) printf("Cannot handle SIGTERM!\n"); /* can SIGKILL be handled by our own function? */ if (signal(SIGKILL, mysigkill) == SIG_ERR) printf("Cannot handle SIGKILL!\n"); while(1); /* infinite loop */ /* exit */ exit(0); } /* main() */
pgm2:
#include <stdio.h> #include <signal.h> void sigproc(void); void quitproc(void); main() { signal(SIGINT, sigproc); signal(SIGQUIT, quitproc); printf("ctrl-c disabled use ctrl-\\ to quit\n"); for(;;); /* infinite loop */} void sigproc() { signal(SIGINT, sigproc); /* */ /* NOTE some versions of UNIX will reset signal to default after each call. So for portability reset signal each time */ printf("you have pressed ctrl-c \n"); } void quitproc() { printf("ctrl-\\ pressed to quit\n"); exit(0); /* normal exit status */ }
PGM3:
sig_talk.c -- complete example program (fork ex)
Let us now write a program that communicates between child and parent processes using kill() and signal().
fork() creates the child process from the parent. The pid can be checked to decide whether it is the child (== 0) or the parent (pid = child process id).
The parent can then send messages to child using the pid and kill().
The child picks up these signals with signal() and calls appropriate functions.
An example of communicating process using signals is sig_talk.c:
/* sig_talk.c --- Example of how 2 processes can talk */ /* to each other using kill() and signal() */ /* We will fork() 2 process and let the parent send a few */ /* signals to it`s child */ /* cc sig_talk.c -o sig_talk */ #include <stdio.h> #include <signal.h> void sighup(); /* routines child will call upon sigtrap */ void sigint(); void sigquit(); main() { int pid; /* get child process */ if ((pid = fork()) < 0) { perror("fork"); exit(1); } if (pid == 0) { /* child */ signal(SIGHUP,sighup); /* set function calls */ signal(SIGINT,sigint); signal(SIGQUIT, sigquit); for(;;); /* loop for ever */ } else /* parent */ { /* pid hold id of child */ printf("\nPARENT: sending SIGHUP\n\n"); kill(pid,SIGHUP); sleep(3); /* pause for 3 secs */ printf("\nPARENT: sending SIGINT\n\n"); kill(pid,SIGINT); sleep(3); /* pause for 3 secs */ printf("\nPARENT: sending SIGQUIT\n\n"); kill(pid,SIGQUIT); sleep(3); } } void sighup() { signal(SIGHUP,sighup); /* reset signal */ printf("CHILD: I have received a SIGHUP\n"); } void sigint() { signal(SIGINT,sigint); /* reset signal */ printf("CHILD: I have received a SIGINT\n"); } void sigquit() { printf("My DADDY has Killed me!!!\n"); exit(0); }
http://www.cs.cf.ac.uk/Dave/C/node24.html
http://www.amparo.net/ce155/signals-ex.html
www.amparo.net/ce155/signals-ex.html
No comments:
Post a Comment