execl的返回值 [英] Return value of execl

查看:129
本文介绍了execl的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行execl函数来通过我的代码编译程序。我想检查汇编是否正确。所以我写了 b = execl(usr / bin / gcc,cc,path,NULL); ,稍后检查 if(b = = -1)。但即使编译出现错误,它也没有进入if语句。
任何想法为什么?
谢谢!

I'm running execl function to compile a program through my code. And I want to check if the compilation went right. so I wrote b = execl("usr/bin/gcc","cc",path,NULL); and later checked if( b==-1). But even though there were errors in the compilations it didnt get into the if statement. Any ideas why? Thank you!

int b=0;
if ((pid1 = fork())<0)
    perror("Error forking");
else {
    if(pid1==0)
        b= execl("/usr/bin/gcc","cc",path,NULL);
    else wait(&status);
}
if(b==-1)
    printf("\n--------\n");


推荐答案

如果 exec 成功,它永远不会返回。成功意味着能够找到并启动命令。如果它不成功,它将返回-1。

If exec succeeds it will NEVER return. Succeeding means be able to find and launch the command. If it doesn't succeed it will return -1.

您需要从状态中提取命令的退出值 code>用于父进程中的 wait

What you need is to extract the exit value of the command from the status used in the wait in the parent process.

您有一些宏来确定状态


  • WIFEXITED(status)会告诉你如果命令通过调用
    exit

  • 而停止,那么您将能够通过$获取退出状态b $ b code> WEXITSTATUS(status)

  • WIFEXITED(status) will tell you if the command stopped by a call to exit
  • and then you will be able to get the exit status with WEXITSTATUS(status).

如果 status 等于0,那么您将知道命令成功运行,任何其他值都意味着该命令无法正常执行任务。

If this status equals to 0 then you will know that the command ran successfully, any other value means that the command wasn't able to do its task normally.

这篇关于execl的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆