一个关于execl函数返回值的问题

cogbee 2013-02-27 02:15:32
百度关于execl函数的百科在这个位置:http://baike.baidu.com/view/6221940.htm#

但是里面的一句话我不是赞同的:

返回值 :成功则不返回值, 失败返回-1,

execl函数都是定义的int,怎么会没有返回值呢。为了验证这一点,我在linux下面做了简易的函数。正确的时候返回值是有的,但是比较大。我不知道这个execl返回值是什么。于是想请教各位大侠。
...全文
756 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pp25210 2015-01-12
  • 打赏
  • 举报
回复
引用 4 楼 cogbee 的回复:
我在linux下做了这么一个程序。

int main()
{
pid_t pid;
int m;
if((pid=fork())<0)
printf("fork error!");
else if(pid==0)
{
if((m=execl("/home/**","**","**",(char *)0))<0)
printf("execl error!");
}
printf("%d\n",m);
if(waitpid(pid,NULL,0)<0)
printf("waitpid error.");
return 0;
}
我的结果是m有输出,一个比较大的数。具体不清楚了。难道这个我哪里弄错了?
m未初始化, 打印出来的是个不可预知的任意值.
cogbee 2013-03-01
  • 打赏
  • 举报
回复
谢谢啦,我试了,的确没有返回值。学习了。
mymtom 2013-03-01
  • 打赏
  • 举报
回复
不是很大的值是楼主打印的不对吧,楼主试试下面的程序

#include <unistd.h>

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

int main(void)
{
    pid_t pid;
    int m;
    if((pid=fork())<0)
        printf("fork error!");
    else if(pid==0)
    {
        if((m=execl("/home/**","**","**",(char *)0))<0)
            perror("execl error!"); /* 改为perror可以打印错误原因 */
        printf("m=%d\n",m);         /* 只在子进程里打印m的值,父进程里不要打印 */
        exit(0);                    /* 子进程在这里该退出了,后面的waitpid应该在父进程里执行 */
    }
    if(waitpid(pid,NULL,0)<0)
        printf("waitpid error.");
    return 0;
}
mymtom 2013-02-28
  • 打赏
  • 举报
回复
返回值类型确实是int, 但是这个不是问题的关键。 关键在于execl会执行另一个程序,如果成功的话,就不会返回了;只有执行失败的时候才会返回-1. #include <unistd.h> int execl(const char *path, const char *arg0, ... /*, (char *)0 */); DESCRIPTION The exec functions replace the current process image with a new process image. The new image is constructed from a regular, executable file called the new process image file. There is no return from a successful exec, because the calling process image is overlaid by the new process image. ... RETURN VALUE If one of the exec functions returns to the calling process image, an error has occurred; the return value is -1, and errno is set to indicate the error.
cogbee 2013-02-28
  • 打赏
  • 举报
回复
引用 3 楼 mymtom 的回复:
返回值类型确实是int, 但是这个不是问题的关键。 关键在于execl会执行另一个程序,如果成功的话,就不会返回了;只有执行失败的时候才会返回-1. #include <unistd.h> int execl(const char *path, const char *arg0, ... /*, (char *)0 */); DESCRIPTION ……
我在linux下做了这么一个程序。 int main() { pid_t pid; int m; if((pid=fork())<0) printf("fork error!"); else if(pid==0) { if((m=execl("/home/**","**","**",(char *)0))<0) printf("execl error!"); } printf("%d\n",m); if(waitpid(pid,NULL,0)<0) printf("waitpid error."); return 0; } 我的结果是m有输出,一个比较大的数。具体不清楚了。难道这个我哪里弄错了?
cogbee 2013-02-27
  • 打赏
  • 举报
回复
引用 1 楼 mymtom 的回复:
execl失败才会返回-1,成功的话是不会返回的。 不知道楼主的简易函数是什么样的。
为什么不会返回值?不是定义的int类型函数吗?
mymtom 2013-02-27
  • 打赏
  • 举报
回复
execl失败才会返回-1,成功的话是不会返回的。 不知道楼主的简易函数是什么样的。

64,746

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧