简体   繁体   English

使用execlp()运行C程序?

[英]Running a C program using execlp()?

So I have ac program called "hello.c" that does nothing but "exit(2)" at the end. 因此,我有一个名为“ hello.c”的交流程序,该程序最后只执行“ exit(2)”。 And I tried to run it in my child process, and then pass the return value into my parent process and print the appropriate messages. 我尝试在子进程中运行它,然后将返回值传递给父进程并打印适当的消息。 But somehow the program always exit at 0 and print the "exit at 0" message. 但是以某种方式,程序总是在0退出并显示“在0退出”消息。 Can someone tell me why? 有人可以告诉我为什么吗?

pid_t pid;
int status;

pid = fork(); 

if (pid == -1){
    perror("fork");
    exit(1);
} 
if (pid >0){
    /*abusive parents*/
    waitpid(pid, &status, 0);

    if(WEXITSTATUS(status)==3){
        printf("exit at 3\n");
    }
    if(WEXITSTATUS(status)==2){
        printf("exit at 2\n");
    }
    if(!(WEXITSTATUS(status))){
        printf("exit at 0\n");
    }

}
else{
    /*stupid child*/
    execlp("hello.c",NULL);
}


return 0;}

Here is the hello.c: 这是hello.c:

int main(void){
   int teemo=0;

   exit(2);
}

You cannot run a C source file, you need to compile (and link) it to generate an executable file, and run this file. 您无法运行C源文件,需要对其进行编译(和链接)以生成可执行文件,然后运行该文件。 Therefore, you should do something like 因此,您应该做类似的事情

  1. Compiling hello.c first (I suppose you are using gcc ) 首先编译hello.c (我想您正在使用gcc

     gcc -o hello hello.c 
  2. Runing hello in your first program like this 在第一个程序中像这样hello

     execlp("./hello", "hello", NULL); 

Then you should get what you expected. 然后,您应该得到期望的结果。

(Not only) to better understand what is happeing in your code, add error checking. (不仅)要更好地理解代码中的含义,请添加错误检查。

In your specific case add it at least to the call to execlp() . 在您的特定情况下,至少将其添加到对execlp()的调用中。 To do so first have a look at exec*() s documentation: 为此,请先查看exec*()的文档:

RETURN VALUE 返回值

The exec() functions only return if an error has have occurred. exec()函数仅在发生错误时返回。 The return value is -1, and errno is set to indicate the error. 返回值为-1,并且将errno设置为指示错误。

And then enhance your code to cover this case: 然后增强您的代码以解决这种情况:

    ...
  }
  else /* child */
  {
    if (-1 == execlp("hello.c"))
    {
      perror("execlp() failed");
      return 1;
    }
  }

  return 0;
}

You will observer an error message regarding the call to execlp() . 您将观察到有关execlp()调用的错误消息。

This dues to hello.c most probably not being an executable, but an ordinary text file, C- source . 这是由于hello.c最有可能不是可执行文件,而是普通文本文件C- source


Looking at the documentation closer one reads: 仔细阅读文档,您会读到:

The list of arguments must be terminated by a null pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL. 参数列表必须以空指针终止,并且由于它们是可变参数函数,因此该指针必须强制转换为(char *)NULL。

From this we learn that a call to the execl*() members of the exec*() family of functions shall always end like this: 从中我们了解到,对exec*()函数家族的execl*()成员的调用将始终像这样结束:

execlp(..., (char *) NULL)

Just to add why its returning zero always in this case 仅添加为什么在这种情况下总是返回零

This is because when execlp was failing and returning -1, parent process was using return value at the end of the main function ie 0 in this case. 这是因为当execlp失败并返回-1时,父进程在主函数的末尾使用返回值,即在这种情况下为0。 If you change this to 2 or 3 you will see program hitting that code with if check ==3 or ==2. 如果将其更改为2或3,则如果check == 3或== 2,您将看到程序按该代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM