2016-02-21 133 views
2

我想在c程序中使用execlp来运行另一个c程序。 exec函数调用程序,但它不能正确传递整数参数。我exec调用是:如何使用execlp将命令行参数传递给C程序

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,它通常会读取参数。

+2

为什么不看看'execlp'的手册页来查看它需要的参数类型?你调用未定义的行为。 – Olaf

+0

明白Unix/Linux开发者只能处理文本。给定一个整数的二进制表示,他们的大脑会爆炸。 –

+0

我试着传递字符数组,但同样的问题发生。另外,我认为整数和字符在C中的大多数情况下是可以互换的。我没有收到任何编译器警告。 – Spence123

回答

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

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)来回MTHE手册页 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:一些信息,如默认的搜索路径,垫变化根据您的系统

+0

请注意,只有链接的答案被认为是SO上的低质量,可能被标记为删除。官方声明:“虽然这个链接可能回答这个问题,但最好在这里包含答案的重要部分,并提供参考链接。如果链接页面更改,则仅链接答案可能会失效。“ – kaylum

+0

编辑帖子并提供更多信息 –

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); 
} 
相关问题