UNIX-programming-signals
Signals are a technique used to notify a process that some condition has occurred
Soo if a process divides by zero the signal whose name is SIGFE (floating-point exception) is sent to the process. Then the process has three choices like:
Ignore the Signal, LEt the default action occur. Provide a functioan that is called when the signal occurs.
Many conditions generates the signals. If u want to generate a signal you can do it with calling "kill" function. But there is some limitations like u have to be owner of the other process (or the superuser) to able to sen it a signal.
#include <apue.h>
#include <sys/wait.h>
static void sig_int(int); /* our signal catching func */
int main(void){
char buf[MAXLINE]; /* apue.h */
pid_t pid;
int status;
if (signal(SIGINT, sig_int) == SIG_ERR){
err_sys("signal error");
}
printf("%%");
while (fgets(buf, MAXLINE, stdin) != NULL) {
if (buf[strlen(buf) - 1] == "\n")
buf[strlen(buf) - 1] = 0; /* replace newline with null */
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) {
/* child */
execlp(buf, buf, (char *)0);
err_ret("couldn't execute: %s", buf);
exit(127);
}
/* parent */
if ((pid = waitpid(pid, &status, 0)) < 0)
err_sys("waitpid error");
printf("%% ");
}
exit(0);
}
void sig_int(int signo){
printf("interrupt\n%% ");
}