如何使用execlp

时间:2016-02-21 21:49:37

标签: c exec

我正在尝试在c程序中使用execlp来运行另一个c程序。 exec函数确实调用程序,但它没有正确传递整数参数。我的执行电话是:

int exec_arg_1, exec_arg_2;

if(pid == 0){
    printf("Repeat Number: %d, Process Number: %d\n", exec_arg_1, exec_arg_2);
    execlp( "/home/drlight/Desktop/asp/Assignment_3/philosopher.o", 
       "philosopher.o", &exec_arg_1, &exec_arg_2, NULL );
           printf("Exec didn't work...\n");
    }

我将值赋给exec_arg整数,并在之前打印它们以确保它们正确,但是philosopher.o函数只是从该位置读取0。如果我从命令行运行philosopher.o,它会正常读取参数。

3 个答案:

答案 0 :(得分:3)

程序的参数总是字符串。

int exec_arg_1, exec_arg_2;

if (pid == 0){
    printf("Repeat Number: %d, Process Number: %d\n", exec_arg_1, exec_arg_2);
    char arg1[20], arg2[20];
    snprintf(arg1, sizeof(arg1), "%d", exec_arg_1);
    snprintf(arg2, sizeof(arg2), "%d", exec_arg_2);
    execlp( "/home/drlight/Desktop/asp/Assignment_3/philosopher.o", 
       "philosopher.o", arg_1, arg_2, NULL );
    fprintf(stderr, "Exec didn't work...\n");
    exit(1);
}

注意execlp()实际上只对固定数量的参数有用(或者,至少,当参数数量有一个小的固定上限时)。大多数情况下,execvp()是更好的选择。

答案 1 :(得分:1)

This page包含大量用法示例....

编辑:从链接添加了代码段 来自上面链接的代码片段

static void show_info_page(const char *git_cmd)
{
    const char *page = cmd_to_page(git_cmd);
    setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
    execlp("info", "info", "gitman", page, (char *)NULL);
    die(_("no info viewer handled the request"));
}

最佳做法是首先考虑一下the execlp(3) man page

编辑:在手册页中添加了execlp(3)的说明 FreeBSD手册页解释了execlp()的用法如下

 int
 execlp(const char *file, const char *arg, ... /*, (char *)0 */);

 The initial argument for these functions is the pathname of a file which
 is to be executed.

 The const char *arg and subsequent ellipses in the execl(), execlp(), and
 execle() functions can be thought of as arg0, arg1, ..., argn.  Together
 they describe a list of one or more pointers to null-terminated strings
 that represent the argument list available to the executed program.  The
 first argument, by convention, should point to the file name associated
 with the file being executed.  The list of arguments must be terminated
 by a NULL pointer.

 The functions execlp(), execvp(), and execvP() will duplicate the actions
 of the shell in searching for an executable file if the specified file
 name does not contain a slash ``/'' character.  For execlp() and
 execvp(), search path is the path specified in the environment by
 ``PATH'' variable.  If this variable is not specified, the default path
 is set according to the _PATH_DEFPATH definition in <paths.h>, which is
 set to ``/usr/bin:/bin''

PS:某些信息,例如默认搜索路径,mat会根据您的系统而有所不同

答案 2 :(得分:0)

你的问题是excelp的字符串指针不是整数的arg参数。从联机帮助页

int execlp(const char *file, const char *arg, ...);

在将它们传递给execlp之前,您必须将它们转换为字符串。

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

#define MAXDIGITS 22

main()
{
    int exec_arg_1, exec_arg_2;

    char execStr1[MAXDIGITS + 1];
    char execStr2[MAXDIGITS + 1];

    exec_arg_1 = 750;
    exec_arg_2 = 25;

    snprintf(execStr1, MAXDIGITS + 1, "%d", exec_arg_1);
    snprintf(execStr2, MAXDIGITS + 1, "%d", exec_arg_2);

    printf("Our Strings: %s, %s\n", execStr1, execStr2);
    execlp("/home/drlight/Desktop/asp/Assignment_3/philosopher.o", "philosopher.o", execStr1, execStr2, NULL);
}

您需要确保MAXDIGITS足够大以容纳您的数字的所有小数位数,但对于大多数当前平台上的长度数据,25应该足够了。但请记住,在gcc的未来版本中和/或使用不同的编译器时,这可能会有所不同。不要忘记留下负面的空间。您可以通过导入limits.h并打印INT_MAX和LONG_MAX的值来检查这些最大值。

#include<stdio.h>
#include<limits.h>

main(int argc, char * argv[])
{
    printf("Int max: %d\n", INT_MAX);
    printf("Long max: %ld\n", LONG_MAX);
}