为什么 execv 可能会崩溃?

发布于 2024-09-25 23:05:37 字数 377 浏览 2 评论 0原文

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main (int argc, const char * argv[])
{
    printf("start\n");
    char *const parmList[] = {"/bin/ls", "-l", NULL};
    execv("/bin/ls", parmList);
    return 0;
}

我用 GCC4.2 编译 你知道为什么这可能会崩溃吗?我在 xcode 中没有收到任何错误消息。

编辑:用户错误。 “崩溃”意味着 xcode 在运行程序时冻结了。按继续效果很好。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main (int argc, const char * argv[])
{
    printf("start\n");
    char *const parmList[] = {"/bin/ls", "-l", NULL};
    execv("/bin/ls", parmList);
    return 0;
}

I compiled with GCC4.2 Any ideas why this might crash? I'm not getting any error messages in xcode.

EDIT: user error. "crash" meant xcode froze when it ran the program. Pressing continue works fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猛虎独行 2024-10-02 23:05:37

该代码在我的环境(Ubuntu 10 下的 gcc 4.4.3)中运行和编译良好。这让我相信您遇到的问题与您认为的问题不同:-)


pax@pax-desktop:~$ ./testprog
start
total 2152
drwxr-xr-x 11 pax pax    4096 2010-10-02 08:23 Pax
: :
----r-S---  1 pax pax       0 2010-08-23 18:58 xyz

pax@pax-desktop:~$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

尝试以下代码以查看实际的 exec 失败。如果是,它应该告诉你原因。如果不是,那么您根本看不到 rc 输出。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

int main (int argc, const char * argv[])
{
    int rc;
    printf("start\n");
    char *const parmList[] = {"/bin/ls", "-l", NULL};
    rc = execv("/bin/ls", parmList);
    printf ("rc = %d, errno = %d\n", rc, errno);
    return 0;
}

还要检查以确保 /bin/ls 是您期望的内容(例如,可执行文件,而不是脚本)。

值得澄清一下“崩溃”的含义。它只是不产生任何输出吗?它转储核心文件吗?它是否会使您的整个操作系统瘫痪,导致重新启动?

That code runs and compiles fine in my environment, gcc 4.4.3 under Ubuntu 10. That leads me to believe that you have a different problem from the one you think you have :-)


pax@pax-desktop:~$ ./testprog
start
total 2152
drwxr-xr-x 11 pax pax    4096 2010-10-02 08:23 Pax
: :
----r-S---  1 pax pax       0 2010-08-23 18:58 xyz

pax@pax-desktop:~$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Try the following code to see if the actual exec is failing. If it is, it should tell you why. If it isn't, then you won't see the rc output at all.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

int main (int argc, const char * argv[])
{
    int rc;
    printf("start\n");
    char *const parmList[] = {"/bin/ls", "-l", NULL};
    rc = execv("/bin/ls", parmList);
    printf ("rc = %d, errno = %d\n", rc, errno);
    return 0;
}

Also check to make sure the /bin/ls is what you expect it to be (an executable, not a script, for example).

And it's worth clarifying what you mean by "crash". Is it just not producing any output? It it dumping a core file? Is it bringing your entire OS to its knees, causing a reboot?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文