linux信号值头文件位置,Linux C 信号处理机制

一 . 信号

1. 信号:是内核发送给某一进程的一种消息 。

2. 信号机制:是Linux系统中用于进程之间相互通信或操作的一种机制。

3. 信号的来源:信号来源于内核

4. 产生原因: (1)用户通过终端输入 (2)进程执行(3)一个进程调用kill向另一个进程发送信号

5. 信号分类:1)同步信号    异步信号

2)可靠信号 : 信号被递送多次  不可靠信号: 只被递送一次的信号

6. 信号处理方式

A.  按系统默认方式处理 ——每个信号都有一个默认动作,典型的默认动作是终止进程。 man signal 7 列出系统对各信号的默认处理 。

B.  忽略信号——接收信号,但是不做任何操作 。

C.  捕捉信号——程序提前告诉内核,当信号到来时应该调用哪个函数。

二. 信号相关函数

1.  简单的信号处理

函数原型: sighandler_t signal(int signum, sighandler_t handler);

参    数:signum ——要响应的信号,handler  ——信号发生时要执行的操作。

返回值:非-1    执行成功,返回以前的信号处理函数指针

-1        遇到错误

//捕捉信号

#include

#include

#include

#include

void capture(int signum){

printf("SIGINT is capture!\n");

}

int main(int argc, char *argv[]){

int i=10;

if(signal(SIGINT,capture) == SIG_ERR){

printf("Can't catch SIGINT");

exit(1);

}

printf("waiting for signal...\n");

while(i > 0){

printf("Now i=%d \n",i);

sleep(1);

i--;

}

return 0;

}

//忽略信号

#include

#include

#include

int main(int argc,char *argv[]){

int i=10;

signal(SIGINT,SIG_IGN);

printf("waiting for signal ...\n");

while(i > 0){

sleep(1);

i--;

printf("Now i = %d,but you can't stop this program by Ctrl + C\n",i);

}

return 0;

}

//恢复信号的默认处理

#include

#include

void capthensfl(int signum){

printf("SIGINT is capture !\n");

signal(SIGINT,SIG_DFL);

printf("SIGINT now is defaulted !\n");

}

int main(int argc,char *argv[]){

int i=10;

signal(SIGINT,capthensfl);

printf("waiting for signal...\n");

while(i > 0){

sleep(1);

printf("Now i = %d\n",i);

i--;

}

return 0;

}

2. 信号处理函数:指定一个信号的处理函数

函数原型:int sigaction(int signum, struct sigaction *action, struct sigaction *oldaction);

参    数:signum  ,要处理的信号

action        要安装的信号处理操作的结构

oldaction   之前的信号处理操作的结构

返回值:0    成功     -1,失败

说    明:oldaction不为NULL,则用其来存储以前设置的与此信号关联的操作。

action不为NULL: 则指定信号关联的操作为此参数指向的结构 否则,信号处理保持不变。(此方式用来查询当前对指定信号的处理方式)

sigaction的结构:

struct sigaction{

union{         __sighandler_t _sa_handler;

void(*_sa_sigaction)(int , struct siginfo *, void *);

} __u;

sigset_t sa_mask;

unsigned long sa_flags;

};

sa_sigaction的说明: void (*sa_sigaction)(int, struct siginfo *,void *);

第一个参数:要响应的信号 。

第二个参数:记录导致产生信号的原因、类型等 。

第三个参数:信号发生时被中断的上下文环境 信号能传递简单的信息。

//使用sa_sigaction类型函数

#include

#include

#include

#include

void fun(int signo, siginfo_t *info, void *context){

printf("Test for sa_sigaction !\n");

}

int main(int argc,char *argv[]){

struct sigaction action,oldaction;

sigemptyset(&action.sa_mask);

action.sa_flags=SA_SIGINFO;

action.sa_sigaction=fun;

sigaction(SIGINT,&action,&oldaction);

printf("waiting for signal...\n");

while(1){

pause();

}

return 0;

}

3. 初始化信号集

函数原型:int sigemptyset(sigset_t  *set);

int sigfillset(sigset_t  *set);

参    数:set  待初始化的信号集

返回值:0   成功     -1,失败

4. 添加、删除信号

函数原型:int sigaddset(sigset_t *set, int signo);

int sigdelset(sigset_t *set, int signo);

参    数:set  待初始化的信号集 .  signo  待添加/删除的信号编号

返回值:0  成功     -1,失败

5. 修改屏蔽信号集

函数原型:int sigprocmask(int how,sigset_t *set, sigset_t * oldset);

参    数:how   如何修改信号掩码

set 要使用的信号集

oldset   之前的信号集

返回值:0  成功     -1,失败

对参数how的说明:    SIG_BLOCK:信号集中增加set中的信号

SIG_UNBLOCK:信号集中删除set中的信号

SIG_SETMASK:信号集被设置为set信号集

三.  系统调用函数

1. kill  —— 向进程发送一个信号

函数原型:int kill(pid_t pid, int sig);

参    数:pid 目标进程id, sig  要发送的信号

返回值:0  成功     -1,失败

说    明:     pid>0      目标进程的进程号为pid

pid=0      将信号发送给和当前进程在同一进程租的所有进程

pid=-1     将信号发送给系统内的所有进程

pid<0      将信号发送给进程组号PGID为pid绝对值的所有进程

一个进程可使用kill函数将信号发送给另一进程或进程组。

要求:发送信号的进程的用户ID和目标进程的用户ID相同,或发送信号的进程的owner是一个超级用户。

//kill 使用示例

#include

#include

#include

void fun(int signo){

printf("Process capture SIGINT");

signal(SIGINT,SIG_DFL);

}

int main(int argc,char *argv[]){

int pid;

if((pid=fork()) == -1){

perror("fork");

exit(EXIT_FAILURE);

}

else if( pid == 0){

signal(SIGINT,fun);

printf("Child %d waiting for parent %d send signal\n",getpid(),getppid());

pause();

pause();

}

else{

sleep(1);

printf("Parent %d will send signal to child %d \n",getppid(),getpid());

kill(pid,SIGINT);

wait(NULL);

}

return 0;

}

2. raise —— 自举一个信号

函数原型:int raise(int signo);

参    数:signo      待发送的信号

返回值:0  成功     -1,失败

说   明:raise(signo)=kill(getpid(),signo)

3. alarm —— 设置计时器

函数原型:int alarm(int seconds);

参    数:seconds  计时的秒数

返回值:      0    之前未调用过alarm

>0   之前调用alarm设置的闹钟时间的余留秒数

4. pause —— 挂起调用进程

函数原型:int pause(void);

返回值:-1       并将errno设置为EINTR

四. 信号集合操作应用示例

/*  信号的应用示例:

父进程执行文件复制操作,如果收到SIGUSR1信号,就打印出当前的复制进度

子进程每隔一个固定时间向父进程发送SIGUSR1信号

*/

#include

#include

#include

#include

#include

#include

int count;  //当前复制大小

int file_size; //文件的大小

//父进程对SIGUSR1信号的处理函数

void sig_usr(int signum){

float i;

//求出复制进程

i = (float)count/(float)file_size;

printf("current over : %0.0f%%\n",i*100);

}

//子进程对SIGUSR1信号的处理,即向父进程发送SIGUSR1信号

void sig_alarm(int signo){

kill(getppid(),SIGUSR1);

}

//主函数

int main(int argc ,char *argv[]){

pid_t pid;

int i;

int fd_src,fd_des;

char buf[128];

if(argc != 3){

printf("the format must be : command src_file des_file \n");

return -1;

}

//以只读方式打开源文件

if((fd_src = open(argv[1],O_RDONLY)) == -1){

perror("open fd_src");

exit(EXIT_FAILURE);

}

//获取源文件大小

file_size = lseek(fd_src,0,SEEK_END);

//重新设置读写位置为文件头

lseek(fd_src,0,SEEK_SET);

//以读写方式打开目标文件,如果不存在就创建

if((fd_des = open(argv[2],O_RDWR|O_CREAT,0644)) == -1){

perror("open fd_des");

exit(EXIT_FAILURE);

}

//进程创建失败

if((pid = fork() == -1)){

perror("fork");

exit(EXIT_FAILURE);

}

//父进程

else if(pid > 0){

//安装信号

signal(SIGUSR1,sig_usr);

do{

memset(buf,'\0',128);

//复制数据

if((i = read(fd_src,buf,1)) == -1){

perror("read");

exit(EXIT_FAILURE);

}

//如果复制完成,向子进程发送SIGINT信号,终止

else if(i == 0){

kill(pid,SIGINT);

break;

}

else{

//执行复制操作

if(write(fd_des,buf,i) == -1){

perror("write");

exit(EXIT_FAILURE);

}

count += i; //更新已经复制的文件的大小

}

}while(i != 0);

//等待子进程退出

wait(pid,NULL,0);

exit(EXIT_SUCCESS);

}

//子进程

else if(pid == 0){

usleep(1);

signal(SIGALRM,sig_alarm);

ualarm(1,1);

while(i){

;

}

exit(EXIT_SUCCESS);

}

return 0;

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441296.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HDU - 1134 】Game of Connections(JAVA大数加法,卡特兰数)

题干&#xff1a; This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into num…

简述linux系统的安全性,Linux操作系统的安全性有哪些过人之处

01用户/文件权限Linux的敲门砖Linux操作系统的安全性是有目共睹的&#xff0c;相比Windows操作系统&#xff0c;到底Linux有哪些过人之处&#xff1f;这里我们就抛砖引玉&#xff0c;挑选三点重要的特点给大家说明&#xff0c;为什么说Linux操作系统安全性有其他系统无可比拟的…

【qduoj - 夏季学期创新题】C语言课程设计-阶梯问题(dp,高精度大数)

题干&#xff1a; 描述 N级阶梯&#xff0c;人可以一步走一级&#xff0c;也可以一步走两级&#xff0c;求人从阶梯底端走到顶端可以有多少种不同的走法。 输入 一个整数n&#xff0c;代表台阶的阶数。 输出 求人从阶梯底端走到顶端可以有多少种不同的走法&#xff0c;输出结…

linux查询打印机ip,Linux C打印IP地址信息

1、由文件描述符打印IP地址及端口。(参考)#include #include #include #include //由文件描述符打印对应IP地址和端口void PrintAddrByFd(int sockfd){struct sockaddr_in addr_in;socklen_t len sizeof(addr_in);getsockname(sockfd, (struct sockaddr *)&addr_in, &…

【URAL - 1114 】Boxes (dp,组合数学)

题干&#xff1a; N boxes are lined up in a sequence (1 ≤ N ≤ 20). You have A red balls and B blue balls (0 ≤ A ≤ 15, 0 ≤ B ≤ 15). The red balls (and the blue ones) are exactly the same. You can place the balls in the boxes. It is allowed to put in a…

linux强行卸载qt,Linux下卸载QT SDK

unbuntu下卸载QT方法一&#xff1a;you can remove it like this, those developers should add this somewhere ! like next to the download textlinuxmint ~ # cd /optlinuxmint opt # lsqtsdk-2010.04linuxmint opt # sudo ./qtsdk-2010.04/bin/uninstalllinuxmint opt #方…

【CodeForces - 227C】Flying Saucer Segments (思维)

题干&#xff1a; An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives dont even have antennas on their heads!). The flying saucer, on which the brave pioneers set off, consists of three sections. …

清楚linux缓存文件,Linux删除文件 清除缓存

相信很多测试 经常会经历开发叫你清除缓存这种事。那我们要怎么清呢&#xff1f;一、首先&#xff0c;确认你要清除的缓存在哪个目录下&#xff0c;然后切换到该目录下&#xff0c;比如 我现在知道我的的缓存目录是在newerp这个目录下&#xff0c;则如图二、然后 执行命令 rm -…

【HDU - 2546】饭卡 (dp,0-1背包,贪心思想)

电子科大本部食堂的饭卡有一种很诡异的设计&#xff0c;即在购买之前判断余额。如果购买一个商品之前&#xff0c;卡上的剩余金额大于或等于5元&#xff0c;就一定可以购买成功&#xff08;即使购买后卡上余额为负&#xff09;&#xff0c;否则无法购买&#xff08;即使金额足够…

linux的程序员计算器,linux中的计算器

windows系统安装时会自带计算器&#xff0c;在cmd中运行calc即可打开。那么linux系统中有没有计算器呢&#xff1f;答案是肯定的。linux下的图形计算器linux系统一般也会默认安装一个图形界面的计算器&#xff0c;例如红帽系统默认安装的是gcalctool。假如在一个图形桌面环境中…

【HDU - 薛猫猫杯程序设计网络赛】【题解】

A 爬山 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 0 Accepted Submission(s): 0 Problem Description 小Z准备去爬山&#xff0c;在他的面前有N座山&#xff0c;每座山都有对应的高度。他想选择两座高…

win10 linux安卓模拟器,genymotion安卓模拟器在Window10中使用的问题

最近一段时间&#xff0c;把系统升级到了win10&#xff0c;然后悲催的事情出现了&#xff0c;genymotion挂了&#xff0c;根本就不能启动&#xff0c;而且还是2.6版本的genymotion&#xff0c;下面我把遇到的问题总结一下&#xff1a;首先&#xff0c;在我的win10系统中&#x…

【CodeForces - 706C】Hard problem(dp,字典序)

题干&#xff1a; Vasiliy is fond of solving different tasks. Today he found one he wasnt able to solve himself, so he asks you to help. Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical orde…

cross_compile = arm-linux-,cross compile grpc for arm

8种机械键盘轴体对比本人程序员&#xff0c;要买一个写代码的键盘&#xff0c;请问红轴和茶轴怎么选&#xff1f;This post will tell you how to cross compile gPRC static lib for ARM.前段时间尝试交叉编译gRPC遇到了不少的麻烦&#xff0c;写篇post记录一下。gRPCPreparat…

【CodeForces - 349B】Color the Fence (贪心,填数)

题干&#xff1a; Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanyas house. Igor thinks that the larger the number is, the more chance to win Tanyas heart he has. Unfortunately, Igor …

linux键盘映射默认,Linux 中的键盘映射

前面提到&#xff0c;X Window 直接处理了键盘的输入输出端口&#xff0c;因此&#xff0c;在 Linux 虚拟控制台下和 X Window 下使用不同的键盘映射方法。在 Lin对于英语来说&#xff0c;键盘上的字母键直接和英语字母表中的字母对应&#xff0c;但是对于非英语的语种来说&…

【POJ - 2301 】Beat the Spread! (简单数学)

题干&#xff1a; Superbowl Sunday is nearly here. In order to pass the time waiting for the half-time commercials and wardrobe malfunctions, the local hackers have organized a betting pool on the game. Members place their bets on the sum of the two final s…

Linux x8664汇编,Linux Udis86 反汇编引擎使用

前两篇说了capstone/beaengine,这节一起用一用经典的udis86;github:https://github.com/vmt/udis860x01:udis86相比于前面两个&#xff0c;用起来还是比较简单的&#xff0c;使用文档如下所示&#xff1a;Getting StartedBuilding and Installing udis86----------------------…

【HDU - 2200】Eddy's AC难题(简单组合数学)

题干&#xff1a; Eddy是个ACMer,他不仅喜欢做ACM题,而且对于Ranklist中每个人的ac数量也有一定的研究,他在无聊时经常在纸上把Ranklist上每个人的ac题目的数量摘录下来&#xff0c;然后从中选择一部分人(或者全部)按照ac的数量分成两组进行比较&#xff0c;他想使第一组中的最…

C语言用字符串sex储存,2005年计算机等级考试二级C语言全真标准预测试卷(2)

一、选择题(1&#xff5e;40题每题1分&#xff0c;41&#xff5e;50题每题2分&#xff0c;共60分)1.微型计算机的运算器、控制器及内存储器组合在一起&#xff0c;称之为()(本题分值&#xff1a;1分)A.ALUB.CPUC.MPUD.主机【正确答案】D2.下列存储器中&#xff0c;存取速度最快…