Chinaunix首页 | 论坛 | 博客
  • 博客访问: 115855
  • 博文数量: 64
  • 博客积分: 186
  • 博客等级: 入伍新兵
  • 技术积分: 120
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 17:11
文章分类

全部博文(64)

文章存档

2014年(54)

2013年(2)

2012年(8)

我的朋友

分类: LINUX

2012-05-30 09:18:56

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

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

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

  sigprof.c代码如下:

#include

#include

#include

#include

#include

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

#include

#include

#include

#include

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

#include

#include

#include

#include

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

#include

#include

#include

#include

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

#include

#include

#include

#include

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

达到想要的目的!!

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

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

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

  sigprof.c代码如下:

#include

#include

#include

#include

#include

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

#include

#include

#include

#include

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

#include

#include

#include

#include

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

#include

#include

#include

#include

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

#include

#include

#include

#include

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

达到想要的目的!!

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

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

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

  sigprof.c代码如下:

#include

#include

#include

#include

#include

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

#include

#include

#include

#include

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!!!!!!
看来信号还是被屏蔽掉了!!!!

 //进入信号处理程序一直没有出来,进入信号处理程序一直没有出来 启动程序的发出的SIGALARM 被暂存了,等信号处理程序返回,才能继续调用信号处理程序,这里信号处理程序不能被中断

两者的关系对调一下:

 

  sigprof.c代码如下:

#include

#include

#include

#include

#include

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

#include

#include

#include

#include

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

#include

#include

#include

#include

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

达到想要的目的!!

问题解决!!!!

阅读(2843) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~