以下程序未产生输出。它进入 for 循环并获取一个值(通过 scanf
),但此后代码块停止执行。 Ideone(在线编译器和调试工具)说生成了 SIGXCPU 信号。
#include <stdio.h>
#include <stdlib.h>
long fact(int);
int z(int);
int main()
{
int i, n;
scanf("%d",&n);
int a[10];long b[10];
int c[10];
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
b[i]=fact(a[i]);
c[i]=z(b[i]);
}
printf("\n");
for(i=0; i<n; i++)
{
printf("%d", c[i]);
}
return 0;
}
long fact(int m)
{
if (m==1) return 1;
else return (m*fact(m-1));
}
int z (int s)
{
int c=0, temp;
temp=s%10;
if(temp!=0) return c;
else
{
c++; z(temp);
}
}
SIGXCPU 信号是什么意思?
最佳答案
SIGXCPU
信号在超过其消耗处理器时间限制 (RLIMIT_CPU
) 后每秒发送给一个进程,或者对于实时进程,超过其不休眠运行的限制。
这里的问题是您的递归 z
函数不会停止并一次又一次地调用自身(并导致堆栈溢出)。修复它的停止条件。
来自signal手册页:
Signal | Default Action | Description -------+----------------+------------------------- SIGXCPU| A | CPU time limit exceeded.
The default actions are as follows:
A - Abnormal termination of the process. Additionally, implementation-defined abnormal termination actions, such as creation of a core file, may occur.
关于c - C程序中的SIGXCPU错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15697410/