计算机交互中的信号的传递

来源:互联网 发布:3DM看门狗卡顿优化补丁 编辑:程序博客网 时间:2024/05/18 03:38
/* * 信号: * 信息是通知进程已发生某种条件的一种技术。例如,若某一进程执行除法操作,其除数为零,则将名为SIGFPE的信号发送给该进程。进程如何处理信号有三种选择: * 1.忽略该信号; * 2.按照系统默认方式处理; * 3.提供一个函数,信号发生时则调用该函数。使用这种函数,我们将能知道什么时候产生了信号,并按照所希望的方式来处理; */#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include <stdlib.h>#include <errno.h>#include <signal.h>#include <stdio.h>#include <string.h>#define MAXLINE 100static void sig_int(int);int main(){char buf[MAXLINE];pid_t pid;int status;if(signal(SIGINT,sig_int) == SIG_ERR)perror("signal error");while(fgets(buf,MAXLINE,stdin) != NULL){buf[strlen(buf)-1] = '\0';if(strcmp(buf,"-1") == 0)break;if((pid = fork()) < 0)perror("fork error");else{if(pid == 0){execlp(buf,buf,(char *)0);/* * duplicate the actions of the shell in searching for an executable file if the  * specified filename does not contain a slash(/) character.The followed by the  * list of directories return pathnames specified in the PATH ecvironment variable. * if this variable isn't defined , the path list defaults to the current directory * followed by the list of directories return bu confstr(_CS_PATH)  */exit(127);}}if((pid = waitpid(pid,&status,0)) < 0)perror("waitpid error");}return 0;}void sig_int(int signo){perror("interrupt\n\n");}


程序的执行结果:

lsMySignal  MySignal.cppcd /lsMySignal  MySignal.cpptoptop - 09:42:10 up 43 min,  2 users,  load average: 0.17, 0.16, 0.14Tasks: 174 total,   1 running, 172 sleeping,   0 stopped,   1 zombie%Cpu(s):  1.2 us,  0.5 sy,  0.0 ni, 97.5 id,  0.8 wa,  0.0 hi,  0.1 si,  0.0 stKiB Mem:   5906812 total,  1371072 used,  4535740 free,   145492 buffersKiB Swap:  1998844 total,        0 used,  1998844 free,   488604 cached  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND                                                                                                      63 root      20   0     0    0    0 S   6.4  0.0   0:01.63 kworker/u:6                                                                                                2050 lgy       20   0 1581m 112m  46m S   6.4  1.9   0:41.23 cinnamon                                                                                                   2152 lgy       20   0  268m 4332 3052 S   6.4  0.1   0:04.92 ibus-daemon                                                                                                   1 root      20   0 24592 2528 1376 S   0.0  0.0   0:01.40 init                                                                                                          2 root      20   0     0    0    0 S   0.0  0.0   0:00.00 kthreadd                                                                                                      3 root      20   0     0    0    0 S   0.0  0.0   0:00.16 ksoftirqd/0                                                                                                   6 root      rt   0     0    0    0 S   0.0  0.0   0:00.40 migration/0                                                                                                   7 root      rt   0     0    0    0 S   0.0  0.0   0:00.01 watchdog/0                                                                                                    8 root      rt   0     0    0    0 S   0.0  0.0   0:00.95 migration/1                                                                                                   9 root      20   0     0    0    0 S   0.0  0.0   0:00.00 kworker/1:0                                                                                                  10 root      20   0     0    0    0 S   0.0  0.0   0:00.11 ksoftirqd/1                                                                                                  11 root      rt   0     0    0    0 S   0.0  0.0   0:00.00 watchdog/1                                                                                                   12 root      rt   0     0    0    0 S   0.0  0.0   0:00.90 migration/2                                                                                                  14 root      20   0     0    0    0 S   0.0  0.0   0:00.21 ksoftirqd/2                                                                                                  15 root      rt   0     0    0    0 S   0.0  0.0   0:00.02 watchdog/2                                                                                                   16 root      rt   0     0    0    0 S   0.0  0.0   0:00.34 migration/3                                                                                                  17 root      20   0     0    0    0 S   0.0  0.0   0:00.80 kworker/3:0                                                                                                  18 root      20   0     0    0    0 S   0.0  0.0   0:00.17 ksoftirqd/3                                                                                                  19 root      rt   0     0    0    0 S   0.0  0.0   0:00.00 watchdog/3                                                                                                   20 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 cpuset                                                                                                       21 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 khelper                                                                                                      22 root      20   0     0    0    0 S   0.0  0.0   0:00.00 kdevtmpfs                                                                                                    23 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 netns                                                                                                        25 root      20   0     0    0    0 S   0.0  0.0   0:00.02 sync_supers                                                                                                  26 root      20   0     0    0    0 S   0.0  0.0   0:00.00 bdi-default                                                                                                  27 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kintegrityd                                                                                                  28 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kblockd                                                                                                      29 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 ata_sff                                                                                                      30 root      20   0     0    0    0 S   0.0  0.0   0:00.00 khubd                                                                                                        31 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 md                                                                                                           35 root      20   0     0    0    0 S   0.0  0.0   0:00.00 khungtaskd                                                                                                   36 root      20   0     0    0    0 S   0.0  0.0   0:00.00 kswapd0                                                                                                      37 root      25   5     0    0    0 S   0.0  0.0   0:00.00 ksmd                                                                                                         38 root      39  19     0    0    0 S   0.0  0.0   0:00.00 khugepaged                                                                                                   39 root      20   0     0    0    0 S   0.0  0.0   0:00.00 fsnotify_mark                                                                                             qman printfcpcp: 缺少了文件操作数请尝试执行"cp --help"来获取更多信息。mvmv: 缺少了文件操作数请尝试执行"mv --help"来获取更多信息。