这几天,被公司的两个模块的程序好好的搞了一下,开始以为是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. Java中jspf文件的作用

    转自:https://blog.csdn.net/xzmeasy/article/details/75103431 为什么要用jspf文件 写jsp页面时,是不是:css和js引用特别多,而且有些页面 ...

  2. (Go)09.指针赋值修改示例

      答案: 1 package main 2 import ( 3 "fmt" 4 ) 5 6 7 func modify(p *int) { 8 fmt.Println(p) 9 ...

  3. Drainage Ditches(网络流(EK算法))

    计算最大流,EK算法模板题. #include <stdio.h> #include <string.h> #include <queue> using names ...

  4. POJ 1330 Tarjan LCA、ST表(其实可以数组模拟)

    题意:给你一棵树,求两个点的最近公共祖先. 思路:因为只有一组询问,直接数组模拟好了. (写得比较乱) 原题请戳这里 #include <cstdio> #include <bits ...

  5. 5.30获取openid和createTime--mybatis自动生成接口和映射【这里需要自定义】

    自定义sql获取数据:        dao:            前提是反向成了代码:                A : 接口PhoneModelMapper extends IBaseMap ...

  6. [hihocoder][Offer收割]编程练习赛50

    循环数组 计算a[i]的前缀和s[i],计算l[i]为1~i-1中最小的s值,r[i]为i~n中最大的s值. 则a[i]~a[n]满足性质的条件为r[i]-s[i-1]>0,a[1]~a[i-1 ...

  7. Java中last_insert_id的使用

    last_insert_id的作用是:在当前表中的主键是自增时,插入一条新记录的同时使用last_insert_id可以获取当前的新记录的主键id. 下面是一个例子: import java.sql. ...

  8. JDBC: 批量处理提高SQL处理速度

    引用:忘了 当需要成批插入或者更新记录时.可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理.通常情况下比单独提交处理更有效率 JDBC的批量处理语句包括下面两个方法: a ...

  9. SLAM: 单目视觉SLAM的方案分类《机器人手册》

    摘抄知乎上一段有趣的话:     如果你出门问别人『学习SLAM需要哪些基础?』之类的问题,一定会有很热心的大哥大姐过来摸摸你的头,肩或者腰(不重要),一脸神秘地从怀里拿出一本比馒头还厚的<Mu ...

  10. 《计算机图形学基础(OpenGL版)》勘误表

    第1版第1次印刷: 所在页码 所在行 原内容 更正为 41 16 k=Δx/Δy k=Δy/Δx 46 6 s-t=2Δy/Δx(xi+1)+2b+2yi-1 s-t=2Δy/Δx(xi+1)+2b- ...