c - C程序中的SIGXCPU错误

标签 c recursion infinite-loop

以下程序未产生输出。它进入 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/

相关文章:

php - 为什么我的 FB 应用程序在 IE 中永远循环?

javascript - 列表操作导致无限循环

c - 对 send() 的多次调用合并为对 recv() 的一次调用

C 程序声称文件不存在,但它是吗?

c - 套接字 UDP : Using Sender Info from Recvfrom() in Sendto() Fails

list - 这是 F# 中尾递归的一个很好的例子吗?

java - 如何停止java桌面应用程序中移动鼠标的无限循环

c - 如何让 sscanf 在具有结构的 unix 中运行?

java - 硬币方法中的递归方法

python - 为什么 C 和 Python 中的递归 tetration 比迭代 tetration 更快?