这几天,被公司的两个模块的程序好好的搞了一下,开始以为是SHELL的问题,仔细研究了以下,原来没有想象的那么复杂!!!

      关键在使用的信号SIGALRM 上,两个进程都用可SIGALRM 信号,一个进程在处理函数上启动了另一个进程,导致发给被启动进程的SIGALRM 被屏蔽掉了,最后的结果是被启动进程不能正常运行!!!!

     查了查书,发现还有个SIGPROF,呵呵.打算用SIGPROF来作为一个进程的信号,先写个测试程序看看管不管用

sigprof.c代码如下:
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
void prompt_info(int signo)
{
printf("can i be called!!!\n");
system("./test");
}
void init_sigaction(void)
{
struct sigaction act;
act.sa_handler=prompt_info;
act.sa_flags=0;
sigemptyset(&act.sa_mask);
sigaction(SIGPROF,&act,NULL);
}
void init_time()
{
struct itimerval value;
value.it_value.tv_sec=2;
value.it_value.tv_usec=0;
value.it_interval=value.it_value;
setitimer(ITIMER_PROF,&value,NULL);
}
int main()
{
init_sigaction();
init_time();
while(1);
exit(0);
}
在RED HAT 9.0下编译
gcc -o sigprof sigprof.c
test.c的代码如下:
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
void prompt_info()
{
//system("./sigprof");
printf("this is a test!!!!!!\n");
}
void init_sigaction(void)
{
struct sigaction act;
act.sa_handler=prompt_info;
act.sa_flags=0;
sigemptyset(&act.sa_mask);
sigaction(SIGALRM,&act,NULL);
}
void init_time()
{
struct itimerval value;
value.it_value.tv_sec=20;
value.it_value.tv_usec=0;
value.it_interval=value.it_value;
setitimer(ITIMER_REAL,&value,NULL);
}
int main()
{
init_sigaction();
init_time();
while(1);
exit(0);
}
在RED HAT 9.0下编译
gcc -o test test.c
运行./sigprof
结果如下:
can i be called!!!
this is a test!!!!!!
this is a test!!!!!!
this is a test!!!!!!
this is a test!!!!!!
this is a test!!!!!!
this is a test!!!!!!
this is a test!!!!!!
this is a test!!!!!!
this is a test!!!!!!
this is a test!!!!!!
this is a test!!!!!!
看来信号还是被屏蔽掉了!!!! 两者的关系对调一下: sigprof.c代码如下:
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
void prompt_info(int signo)
{
printf("can i be called!!!\n");
//system("./test");
}
void init_sigaction(void)
{
struct sigaction act;
act.sa_handler=prompt_info;
act.sa_flags=0;
sigemptyset(&act.sa_mask);
sigaction(SIGPROF,&act,NULL);
}
void init_time()
{
struct itimerval value;
value.it_value.tv_sec=2;
value.it_value.tv_usec=0;
value.it_interval=value.it_value;
setitimer(ITIMER_PROF,&value,NULL);
}
int main()
{
init_sigaction();
init_time();
while(1);
exit(0);
}
在RED HAT 9.0下编译
gcc -o sigprof sigprof.c
test.c的代码如下:
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
void prompt_info()
{
system("./sigprof");
printf("this is a test!!!!!!\n");
}
void init_sigaction(void)
{
struct sigaction act;
act.sa_handler=prompt_info;
act.sa_flags=0;
sigemptyset(&act.sa_mask);
sigaction(SIGALRM,&act,NULL);
}
void init_time()
{
struct itimerval value;
value.it_value.tv_sec=20;
value.it_value.tv_usec=0;
value.it_interval=value.it_value;
setitimer(ITIMER_REAL,&value,NULL);
}
int main()
{
init_sigaction();
init_time();
while(1);
exit(0);
}
在RED HAT 9.0下编译
gcc -o test test.c 运行./test
can i be called!!!
can i be called!!!
can i be called!!!
can i be called!!!
can i be called!!!
can i be called!!!
can i be called!!!
can i be called!!!
can i be called!!!
can i be called!!!
can i be called!!!
can i be called!!!
can i be called!!!
can i be called!!!
can i be called!!!
can i be called!!!
还是失败!!!!!!
换个思维,不在处理函数启动另一个进程
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
int i=0;
void prompt_info()
{
i++;
//system("./sigprof");
printf("this is a test!!!!!!\n");
}
void init_sigaction(void)
{
struct sigaction act;
act.sa_handler=prompt_info;
act.sa_flags=0;
sigemptyset(&act.sa_mask);
sigaction(SIGALRM,&act,NULL);
}
void init_time()
{
struct itimerval value;
value.it_value.tv_sec=2;
value.it_value.tv_usec=0;
value.it_interval=value.it_value;
setitimer(ITIMER_REAL,&value,NULL);
}
int main()
{
init_sigaction();
init_time();
while(1)
{
if(i==1) system("./sigprof");
};
exit(0);
}
在RED HAT 9.0下编译
gcc -o test test.c 运行./test
结果:
this is a test!!!!!!
this is a test!!!!!!
can i be called!!!
this is a test!!!!!!
can i be called!!!
this is a test!!!!!!
can i be called!!!
this is a test!!!!!!
can i be called!!!
this is a test!!!!!!
can i be called!!!
this is a test!!!!!!
can i be called!!!
this is a test!!!!!! OK
达到想要的目的!!
问题解决!!!!

SIGPROF和SIGALRM使用总结的更多相关文章

  1. PHP 信号管理

    .note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...

  2. 【转载】Linux 信号列表

    转自:http://blog.csdn.net/muge0913/article/details/7322710 信号及其简介 信号是一种进程通信的方法,他应用于异步事件的处理.信号的实现是一种软中断 ...

  3. (八) 一起学 Unix 环境高级编程 (APUE) 之 信号

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  4. linux kill信号列表

    linux kill信号列表 $ kill -l1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL5) SIGTRAP      6) ...

  5. linux 信号列表和基本作用

    我们运行如下命令,可看到Linux支持的信号列表: $ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7 ...

  6. iOS异常捕获

    文章目录 一. 系统Crash 二. 处理signal 下面是一些信号说明 关键点注意 三. 实战 四. Crash Callstack分析 – 进⼀一步分析 五. demo地址 六. 参考文献 前言 ...

  7. Linux信号列表

    我们运行如下命令,可看到Linux支持的信号列表: ~$ kill -l1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL5) SIGTRAP 6) SIGABRT 7) ...

  8. 【转】linux trap

    在有些情况下,我们不希望自己的shell脚本在运行时刻被中断,比如说我们写得shell脚 本设为某一用户的默认shell,使这一用户进入系统后只能作某一项工作,如数据库备份, 我 们可不希望用户使用c ...

  9. linux-信号。

    信号 信号是在软件层次上对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的. 信号是异步的,一个进程不必通过任何操作来等待信号的到达,事实上,进程也不知道信号到 ...

随机推荐

  1. luogu1005 矩阵取数游戏

    题目大意 一个矩阵,每次从每一行的行首或行尾取一个数,每一行的价值为 取的数*2^当前取数的次数,每一次的价值为每一行的价值的和.求得到的价值的最大值. 思路 #include <cstdio& ...

  2. kafka的topic和分区策略——log entry和消息id索引文件

    Topic在逻辑上可以被认为是一个在的queue,每条消费都必须指定它的topic,可以简单理解为必须指明把这条消息放进哪个queue里. 为了使得Kafka的吞吐率可以水平扩展,物理上把topic分 ...

  3. B1060 [ZJOI2007]时态同步 dfs

    两遍dfs,第一遍有点像找重链,第二遍维护答案,每个点维护一个当前深度,然后就没啥了. ps:memset(lst,-1,sizeof(lst));这一句多余的话让我debug半天... 题干: De ...

  4. python的enumerate()函数

    其中的一篇是这样的:一般情况下,如果要对一个列表或者数组既要遍历索引又要遍历元素时,可以用enumerate 比如: for index,value in enumerate(list):       ...

  5. 关于函数提升在if语句中的表现

    函数声明创建的函数在现代浏览器,在if语句中函数的声明不会提升,但是在老的IE版本中,if语句中的函数声明会提升 函数表达式在不同浏览器中函数声明都不会被提升,解决了不同浏览器的兼容性问题 关于函数提 ...

  6. TCP/IP详解(二)

    首先,不得不吐槽一下中文版的翻译,把英文版的很多部分的删除了.中文版的pdf只有400多页,英文版有1000多页.迫于时间,只有先将就着看中文版,但是遇到不懂的地方,一定要对照英文版来看. 滑动窗口协 ...

  7. ROS-URDF-Xacro

    前言:Xacro是一种宏语言,允许代码复用,使用Xacro可以减少URDF文件中的代码量. 参考自:http://wiki.ros.org/urdf/Tutorials/Using%20Xacro%2 ...

  8. unity3d引擎中slua的使用

    SLua是开源软件,没有反射,没有额外GC,采用静态代码生成,可以用于游戏核心逻辑,完整支持4.6+ UI系统. 1.下载安装 http://www.slua.net/ https://github. ...

  9. css3动画之1--animation小例子

    1.首先看效果 2.代码及分析 <style type="text/css"> #div1 { margin:100px; position: absolute; te ...

  10. RecyclerView的基础用法

    为了让RecyclerView可以在所有的Android版本中都能使用,Android开发团队将RecyclerView定义在support.v7包当中.在使用该控件时需要打开当前Modile的bui ...