【问题标题】:Stack overflow in thread 1: can't grow stack to 0xffe601ff8 Valgrind Error线程 1 中的堆栈溢出:无法将堆栈增长到 0xffe601ff8 Valgrind 错误
【发布时间】:2014-11-09 03:00:45
【问题描述】:

我是 C 编程新手,感谢任何帮助。该代码将用于检查 pid 是否仍处于活动状态。提供 pid 文件路径的参数通过命令行传递。请在下面查看我的 Valgrind 和 GDB 错误以及代码。

**Valgrind Error**
==6553== Memcheck, a memory error detector
==6553== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==6553== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==6553== Command: ./pid1108_2 /var/run/httpd/httpd.pid
==6553== 
==6553== Stack overflow in thread 1: can't grow stack to 0xffe601ff8
==6553== 
==6553== Process terminating with default action of signal 11 (SIGSEGV)
==6553==  Access not within mapped region at address 0xFFE601FF8
==6553==    at 0x40069F: kill (in /home/ehubbard/C_Checks/pid1108_2)
==6553==  If you believe this happened as a result of a stack
==6553==  overflow in your program's main thread (unlikely but
==6553==  possible), you can try to increase the size of the
==6553==  main thread stack using the --main-stacksize= flag.
==6553==  The main thread stack size used in this run was 10485760.
==6553== Stack overflow in thread 1: can't grow stack to 0xffe601ff0
==6553== 
==6553== Process terminating with default action of signal 11 (SIGSEGV)
==6553==  Access not within mapped region at address 0xFFE601FF0
==6553==    at 0x4801661: _vgnU_freeres (vg_preloaded.c:58)
==6553==  If you believe this happened as a result of a stack
==6553==  overflow in your program's main thread (unlikely but
==6553==  possible), you can try to increase the size of the
==6553==  main thread stack using the --main-stacksize= flag.
==6553==  The main thread stack size used in this run was 10485760.
==6553== 
==6553== HEAP SUMMARY:
==6553==     in use at exit: 0 bytes in 0 blocks
==6553==   total heap usage: 1 allocs, 1 frees, 568 bytes allocated
==6553== 
==6553== All heap blocks were freed -- no leaks are possible
==6553== 
==6553== For counts of detected and suppressed errors, rerun with: -v
==6553== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

**GDB Error**
Program received signal SIGSEGV, Segmentation fault.
0x000000000040068f in kill ()`enter code here`
#include <stdio.h>      //Needed for standard I/O
#include <stdlib.h>     //Needed for exit
#include <sys/types.h>  //Needed for kill function
#include <signal.h>     //Needed for kill function
#include <inttypes.h>
#include <iso646.h>

int kill(pid_t pid, int sig);

int main(int argc, char *argv[])
{
    FILE *fp;
    int pid;

    fp = fopen(argv[1], "r");

    if (fp == NULL){
        printf("Pid file doesn't exist:");
        return 2;}
    else {
          fscanf(fp, "%d", &pid);
          printf("Pid number is %d", pid);
          fclose(fp);
         }
    kill(pid, 0);

}

int kill(pid_t pid, int sig)
{
    if ((kill (pid, sig)) == -1){
         printf("Pid %d is no longer valid", pid);
         return 2;
    }
    else if ((kill (pid, sig)) == 0){
         printf("Pid %d is active.", pid);
         return 0;
    }
    else{
         printf("Could not determine value!");
         return 2;
    }
}

【问题讨论】:

  • kill() 不断用相同的参数调用自己。

标签: c++ c segmentation-fault stack-overflow


【解决方案1】:

从您的自定义 kill 中调用 kill 会导致无限递归。你应该调用你的自定义kill 别的东西,比如custom_kill,然后从main 调用它,然后对kill 的调用将转到正确的Unix kill(2)(或者如果链接不正确,它们将失败正确设置)。

【讨论】:

    【解决方案2】:

    问题的原因是函数kill() 中的无限循环(递归)。很难说你想要做什么,但现在kill() 的这个实现在第一行(if ((kill (pid, sig)) == -1){)中调用了自己,并且没有条件停止这个无限递归。因此,当系统有足够的内存继续运行时,它就可以工作。要修复它,您需要更正此函数的逻辑。

    如果您尝试从自己的函数调用外部函数kill(),重命名函数会更容易:

    int my_kill(pid_t pid, int sig)
    {
      // your current code
    }
    

    【讨论】:

    • 无限递归可能是比循环更好的描述。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2019-05-18
    • 2014-01-26
    相关资源
    最近更新 更多