这几天,被公司的两个模块的程序好好的搞了一下,开始以为是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
达到想要的目的!!
问题解决!!!!


posted on 2014-04-04 11:35  醉依危栏听松涛  阅读(796)  评论(0编辑  收藏  举报