当前位置: 技术问答>linux和unix
还是有一个信号的问题等待求助
来源: 互联网 发布时间:2015-07-28
本文导语: 我的主进程先signal(SIGCHLD,sigfunc), 然后开始 while(1) { accept(……) if(!fork()) { childfunc(newsocket); ……//子进程 } } sigfunc()里面 { signal(SIGCHLD,sigfunc...
我的主进程先signal(SIGCHLD,sigfunc),
然后开始
while(1)
{
accept(……)
if(!fork())
{
childfunc(newsocket);
……//子进程
}
}
sigfunc()里面
{
signal(SIGCHLD,sigfunc);
if(waitpid(0,NULL,WNOHANG)>0)
count--;
}
子进程我只是sleep(n);
可是结果是我的子进程怎么都显示退不出来。。。。。。
请问这是什么原因?
问题很急,请求高手能否指点一下?谢谢
然后开始
while(1)
{
accept(……)
if(!fork())
{
childfunc(newsocket);
……//子进程
}
}
sigfunc()里面
{
signal(SIGCHLD,sigfunc);
if(waitpid(0,NULL,WNOHANG)>0)
count--;
}
子进程我只是sleep(n);
可是结果是我的子进程怎么都显示退不出来。。。。。。
请问这是什么原因?
问题很急,请求高手能否指点一下?谢谢
|
消息处理函数写的不对
应该是在程序的某个地方使用signal(SIGCHLD,sigfunc);的时候
消息处理机制才会根据sigfunc函数明来调用sigfunc()来处理消息
你的消息处理函数本身调用了自己,有点像递规了,不知道这样行不行.
这是个简单的例子,你看看消息处理函数是怎么用的
#include
#include
static void par_sig(int signo);
static void c1_sig(int signo);
static void c2_sig(int signo);
static long p_realt_secs=0,c1_realt_secs=0,c2_realt_secs=0;
static long p_virtt_secs=0,c1_virtt_secs=0,c2_virtt_secs=0;
static long p_proft_secs=0,c1_proft_secs=0,c2_proft_secs=0;
static struct itimerval p_realt,c1_realt,c2_realt;
static struct itimerval p_virtt,c1_virtt,c2_virtt;
static struct itimerval p_proft,c1_proft,c2_proft;
long unsigned fibonacci(unsigned int n);
static void par_sig(int signo)
{
switch(signo)
{
case SIGALRM:
p_realt_secs+=10;
break;
case SIGVTALRM:
p_virtt_secs+=10;
break;
case SIGPROF:
p_proft_secs+=10;
break;
}
}
static void c1_sig(int signo)
{
switch(signo)
{
case SIGALRM:
c1_realt_secs+=10;
break;
case SIGVTALRM:
c1_virtt_secs+=10;
break;
case SIGPROF:
c1_proft_secs+=10;
break;
}
}
static void c2_sig(int signo)
{
switch(signo)
{
case SIGALRM:
c2_realt_secs+=10;
break;
case SIGVTALRM:
c2_virtt_secs+=10;
break;
case SIGPROF:
c2_proft_secs+=10;
break;
}
}
long unsigned fibonacci(unsigned int n)
{
if(n==0)
return 0;
else if(n==1||n==2)
return 1;
else
return (fibonacci(n-1)+fibonacci(n-2));
}
int main()
{
long unsigned fib=0;
int pid1,pid2;
unsigned int fibarg=39;
int status;
struct itimerval v;
long moresec,moremsec,t1,t2;
pid1=fork();
if(pid1==0)
{
signal(SIGALRM,c1_sig);
signal(SIGVTALRM,c1_sig);
signal(SIGPROF,c1_sig);
v.it_interval.tv_sec=10;
v.it_interval.tv_usec=0;
v.it_value.tv_sec=10;
v.it_value.tv_usec=0;
setitimer(ITIMER_REAL,&v,NULL);
setitimer(ITIMER_VIRTUAL,&v,NULL);
setitimer(ITIMER_PROF,&v,NULL);
fib=fibonacci(fibarg);
getitimer(ITIMER_PROF,&c1_proft);
getitimer(ITIMER_REAL,&c1_realt);
getitimer(ITIMER_VIRTUAL,&c1_virtt);
moresec=9-c1_realt.it_value.tv_sec;
moremsec=(1000000-c1_realt.it_value.tv_usec)/1000;
printf("Child 1 fib=%ld,real time=%ld sec,%ld msecn",fib,c1_realt_secs+moresec,moremsec);
moresec=9-c1_proft.it_value.tv_sec;
moremsec=(1000000-c1_proft.it_value.tv_usec)/1000;
printf("Child 1 fib=%ld,cpu time=%ld sec,%ld msecn",fib,c1_proft_secs+moresec,moremsec);
moresec=9-c1_virtt.it_value.tv_sec;
moremsec=(1000000-c1_virtt.it_value.tv_usec)/1000;
printf("Child 1 fib=%ld,user time=%ld sec,%ld msecn",fib,c1_virtt_secs+moresec,moremsec);
t1=(9-c1_proft.it_value.tv_sec)*1000+(1000000-c1_proft.it_value.tv_usec)/1000+c1_proft_secs*10000;
t2=(9-c1_virtt.it_value.tv_sec)*1000+(1000000-c1_virtt.it_value.tv_usec)/1000+c1_virtt_secs*10000;
moresec=(t1-t2)/1000;
moremsec=(t1-t2)%1000;
printf("Child 1 fib=%ld,kernel time=%ld sec,%ld msecn",fib,moresec,moremsec);
fflush(stdout);
exit(0);
}
else
{
pid2=fork();
if(pid2==0)
{
signal(SIGALRM,c2_sig);
signal(SIGVTALRM,c2_sig);
signal(SIGPROF,c2_sig);
v.it_interval.tv_sec=10;
v.it_interval.tv_usec=0;
v.it_value.tv_sec=10;
v.it_value.tv_usec=0;
setitimer(ITIMER_REAL,&v,NULL);
setitimer(ITIMER_VIRTUAL,&v,NULL);
setitimer(ITIMER_PROF,&v,NULL);
fib=fibonacci(fibarg);
getitimer(ITIMER_PROF,&c2_proft);
getitimer(ITIMER_REAL,&c2_realt);
getitimer(ITIMER_VIRTUAL,&c2_virtt);
moresec=9-c2_realt.it_value.tv_sec;
moremsec=(1000000-c2_realt.it_value.tv_usec)/1000;
printf("Child 2 fib=%ld,real time=%ld sec,%ld msecn",fib,c2_realt_secs+moresec,moremsec);
moresec=9-c2_proft.it_value.tv_sec;
moremsec=(1000000-c2_proft.it_value.tv_usec)/1000;
printf("Child 2 fib=%ld,cpu time=%ld sec,%ld msecn",fib,c2_proft_secs+moresec,moremsec);
moresec=9-c2_virtt.it_value.tv_sec;
moremsec=(1000000-c2_virtt.it_value.tv_usec)/1000;
printf("Child 2 fib=%ld,user time=%ld sec,%ld msecn",fib,c2_virtt_secs+moresec,moremsec);
t1=(9-c2_proft.it_value.tv_sec)*1000+(1000000-c2_proft.it_value.tv_usec)/1000+c2_proft_secs*10000;
t2=(9-c2_virtt.it_value.tv_sec)*1000+(1000000-c2_virtt.it_value.tv_usec)/1000+c2_virtt_secs*10000;
moresec=(t1-t2)/1000;
moremsec=(t1-t2)%1000;
printf("Child 2 fib=%ld,kernal time=%ld sec,%ld msecn",fib,moresec,moremsec);
fflush(stdout);
exit(0);
}
else
{
signal(SIGALRM,par_sig);
signal(SIGVTALRM,par_sig);
signal(SIGPROF,par_sig);
v.it_interval.tv_sec=10;
v.it_interval.tv_usec=0;
v.it_value.tv_sec=10;
v.it_value.tv_usec=0;
setitimer(ITIMER_REAL,&v,NULL);
setitimer(ITIMER_VIRTUAL,&v,NULL);
setitimer(ITIMER_PROF,&v,NULL);
fib=fibonacci(fibarg);
getitimer(ITIMER_PROF,&p_proft);
getitimer(ITIMER_REAL,&p_realt);
getitimer(ITIMER_VIRTUAL,&p_virtt);
moresec=9-p_realt.it_value.tv_sec;
moremsec=(1000000-p_realt.it_value.tv_usec)/1000;
printf("Parent fib=%ld,real time=%ld sec,%ld msecn",fib,p_realt_secs+moresec,moremsec);
moresec=9-p_proft.it_value.tv_sec;
moremsec=(1000000-p_proft.it_value.tv_usec)/1000;
printf("Parent fib=%ld,cpu time=%ld sec,%ld msecn",fib,p_proft_secs+moresec,moremsec);
moresec=9-p_virtt.it_value.tv_sec;
moremsec=(1000000-p_virtt.it_value.tv_usec)/1000;
printf("Parent fib=%ld,user time=%ld sec,%ld msecn",fib,p_virtt_secs+moresec,moremsec);
t1=(9-p_proft.it_value.tv_sec)*1000+(1000000-p_proft.it_value.tv_usec)/1000+p_proft_secs*10000;
t2=(9-p_virtt.it_value.tv_sec)*1000+(1000000-p_virtt.it_value.tv_usec)/1000+p_virtt_secs*10000;
moresec=(t1-t2)/1000;
moremsec=(t1-t2)%1000;
printf("Parent fib=%ld,kernel time=%ld sec,%ld msecn",fib,moresec,moremsec);
fflush(stdout);
waitpid(0,&status,0);
waitpid(0,&status,0);
exit(0);
}
printf("This line should never be printedn");
}
}
这是个使用定时器的例子,看看消息处理函数是怎么用的
应该是在程序的某个地方使用signal(SIGCHLD,sigfunc);的时候
消息处理机制才会根据sigfunc函数明来调用sigfunc()来处理消息
你的消息处理函数本身调用了自己,有点像递规了,不知道这样行不行.
这是个简单的例子,你看看消息处理函数是怎么用的
#include
#include
static void par_sig(int signo);
static void c1_sig(int signo);
static void c2_sig(int signo);
static long p_realt_secs=0,c1_realt_secs=0,c2_realt_secs=0;
static long p_virtt_secs=0,c1_virtt_secs=0,c2_virtt_secs=0;
static long p_proft_secs=0,c1_proft_secs=0,c2_proft_secs=0;
static struct itimerval p_realt,c1_realt,c2_realt;
static struct itimerval p_virtt,c1_virtt,c2_virtt;
static struct itimerval p_proft,c1_proft,c2_proft;
long unsigned fibonacci(unsigned int n);
static void par_sig(int signo)
{
switch(signo)
{
case SIGALRM:
p_realt_secs+=10;
break;
case SIGVTALRM:
p_virtt_secs+=10;
break;
case SIGPROF:
p_proft_secs+=10;
break;
}
}
static void c1_sig(int signo)
{
switch(signo)
{
case SIGALRM:
c1_realt_secs+=10;
break;
case SIGVTALRM:
c1_virtt_secs+=10;
break;
case SIGPROF:
c1_proft_secs+=10;
break;
}
}
static void c2_sig(int signo)
{
switch(signo)
{
case SIGALRM:
c2_realt_secs+=10;
break;
case SIGVTALRM:
c2_virtt_secs+=10;
break;
case SIGPROF:
c2_proft_secs+=10;
break;
}
}
long unsigned fibonacci(unsigned int n)
{
if(n==0)
return 0;
else if(n==1||n==2)
return 1;
else
return (fibonacci(n-1)+fibonacci(n-2));
}
int main()
{
long unsigned fib=0;
int pid1,pid2;
unsigned int fibarg=39;
int status;
struct itimerval v;
long moresec,moremsec,t1,t2;
pid1=fork();
if(pid1==0)
{
signal(SIGALRM,c1_sig);
signal(SIGVTALRM,c1_sig);
signal(SIGPROF,c1_sig);
v.it_interval.tv_sec=10;
v.it_interval.tv_usec=0;
v.it_value.tv_sec=10;
v.it_value.tv_usec=0;
setitimer(ITIMER_REAL,&v,NULL);
setitimer(ITIMER_VIRTUAL,&v,NULL);
setitimer(ITIMER_PROF,&v,NULL);
fib=fibonacci(fibarg);
getitimer(ITIMER_PROF,&c1_proft);
getitimer(ITIMER_REAL,&c1_realt);
getitimer(ITIMER_VIRTUAL,&c1_virtt);
moresec=9-c1_realt.it_value.tv_sec;
moremsec=(1000000-c1_realt.it_value.tv_usec)/1000;
printf("Child 1 fib=%ld,real time=%ld sec,%ld msecn",fib,c1_realt_secs+moresec,moremsec);
moresec=9-c1_proft.it_value.tv_sec;
moremsec=(1000000-c1_proft.it_value.tv_usec)/1000;
printf("Child 1 fib=%ld,cpu time=%ld sec,%ld msecn",fib,c1_proft_secs+moresec,moremsec);
moresec=9-c1_virtt.it_value.tv_sec;
moremsec=(1000000-c1_virtt.it_value.tv_usec)/1000;
printf("Child 1 fib=%ld,user time=%ld sec,%ld msecn",fib,c1_virtt_secs+moresec,moremsec);
t1=(9-c1_proft.it_value.tv_sec)*1000+(1000000-c1_proft.it_value.tv_usec)/1000+c1_proft_secs*10000;
t2=(9-c1_virtt.it_value.tv_sec)*1000+(1000000-c1_virtt.it_value.tv_usec)/1000+c1_virtt_secs*10000;
moresec=(t1-t2)/1000;
moremsec=(t1-t2)%1000;
printf("Child 1 fib=%ld,kernel time=%ld sec,%ld msecn",fib,moresec,moremsec);
fflush(stdout);
exit(0);
}
else
{
pid2=fork();
if(pid2==0)
{
signal(SIGALRM,c2_sig);
signal(SIGVTALRM,c2_sig);
signal(SIGPROF,c2_sig);
v.it_interval.tv_sec=10;
v.it_interval.tv_usec=0;
v.it_value.tv_sec=10;
v.it_value.tv_usec=0;
setitimer(ITIMER_REAL,&v,NULL);
setitimer(ITIMER_VIRTUAL,&v,NULL);
setitimer(ITIMER_PROF,&v,NULL);
fib=fibonacci(fibarg);
getitimer(ITIMER_PROF,&c2_proft);
getitimer(ITIMER_REAL,&c2_realt);
getitimer(ITIMER_VIRTUAL,&c2_virtt);
moresec=9-c2_realt.it_value.tv_sec;
moremsec=(1000000-c2_realt.it_value.tv_usec)/1000;
printf("Child 2 fib=%ld,real time=%ld sec,%ld msecn",fib,c2_realt_secs+moresec,moremsec);
moresec=9-c2_proft.it_value.tv_sec;
moremsec=(1000000-c2_proft.it_value.tv_usec)/1000;
printf("Child 2 fib=%ld,cpu time=%ld sec,%ld msecn",fib,c2_proft_secs+moresec,moremsec);
moresec=9-c2_virtt.it_value.tv_sec;
moremsec=(1000000-c2_virtt.it_value.tv_usec)/1000;
printf("Child 2 fib=%ld,user time=%ld sec,%ld msecn",fib,c2_virtt_secs+moresec,moremsec);
t1=(9-c2_proft.it_value.tv_sec)*1000+(1000000-c2_proft.it_value.tv_usec)/1000+c2_proft_secs*10000;
t2=(9-c2_virtt.it_value.tv_sec)*1000+(1000000-c2_virtt.it_value.tv_usec)/1000+c2_virtt_secs*10000;
moresec=(t1-t2)/1000;
moremsec=(t1-t2)%1000;
printf("Child 2 fib=%ld,kernal time=%ld sec,%ld msecn",fib,moresec,moremsec);
fflush(stdout);
exit(0);
}
else
{
signal(SIGALRM,par_sig);
signal(SIGVTALRM,par_sig);
signal(SIGPROF,par_sig);
v.it_interval.tv_sec=10;
v.it_interval.tv_usec=0;
v.it_value.tv_sec=10;
v.it_value.tv_usec=0;
setitimer(ITIMER_REAL,&v,NULL);
setitimer(ITIMER_VIRTUAL,&v,NULL);
setitimer(ITIMER_PROF,&v,NULL);
fib=fibonacci(fibarg);
getitimer(ITIMER_PROF,&p_proft);
getitimer(ITIMER_REAL,&p_realt);
getitimer(ITIMER_VIRTUAL,&p_virtt);
moresec=9-p_realt.it_value.tv_sec;
moremsec=(1000000-p_realt.it_value.tv_usec)/1000;
printf("Parent fib=%ld,real time=%ld sec,%ld msecn",fib,p_realt_secs+moresec,moremsec);
moresec=9-p_proft.it_value.tv_sec;
moremsec=(1000000-p_proft.it_value.tv_usec)/1000;
printf("Parent fib=%ld,cpu time=%ld sec,%ld msecn",fib,p_proft_secs+moresec,moremsec);
moresec=9-p_virtt.it_value.tv_sec;
moremsec=(1000000-p_virtt.it_value.tv_usec)/1000;
printf("Parent fib=%ld,user time=%ld sec,%ld msecn",fib,p_virtt_secs+moresec,moremsec);
t1=(9-p_proft.it_value.tv_sec)*1000+(1000000-p_proft.it_value.tv_usec)/1000+p_proft_secs*10000;
t2=(9-p_virtt.it_value.tv_sec)*1000+(1000000-p_virtt.it_value.tv_usec)/1000+p_virtt_secs*10000;
moresec=(t1-t2)/1000;
moremsec=(t1-t2)%1000;
printf("Parent fib=%ld,kernel time=%ld sec,%ld msecn",fib,moresec,moremsec);
fflush(stdout);
waitpid(0,&status,0);
waitpid(0,&status,0);
exit(0);
}
printf("This line should never be printedn");
}
}
这是个使用定时器的例子,看看消息处理函数是怎么用的
|
你的sigfunc()里面还有,signal(SIGCHLD,sigfunc);
signal的第二个参数是你写的消息处理函数的名字,你的消息处理函数本身似乎不能再调用自己吧
signal的第二个参数是你写的消息处理函数的名字,你的消息处理函数本身似乎不能再调用自己吧