2010-02-02 49 views
0

该方案的目的是fork一个新的子进程,并执行方法,该方法还具有命令行参数。如果我进入/bin/ls --help,我得到的错误:无法使用“的execve()”成功

[email protected]:~/lab/200801076_lab3$ ./a.out 
Enter the name of the executable(with full path)/bin/ls --help 
Starting the executable as a new child process... 
Binary file to be executed: /bin/ls 
/bin/ls: unrecognized option '--help 
' 
Try `/bin/ls --help' for more information. 
Status returned by Child process: 2 
[email protected]:~/lab/200801076_lab3$ 

什么是正确的参数execve()

#include<stdio.h> 
#include<string.h>  //strcpy() used 
#include<malloc.h>  //malloc() used 
#include<unistd.h>  //fork() used 
#include<stdlib.h>  //exit() function used 
#include<sys/wait.h> //waitpid() used 

int main(int argc, char **argv) 
{ 
    char command[256]; 
    char **args=NULL; 
    char *arg; 
    int count=0; 
    char *binary; 
    pid_t pid; 
    printf("Enter the name of the executable(with full path)"); 
    fgets(command,256,stdin); 
    binary=strtok(command," "); 
    args=malloc(sizeof(char*)*10); 
    args[0]=malloc(strlen(binary)+1); 
    strcpy(args[0],binary); 
    while ((arg=strtok(NULL," "))!=NULL) 
    { 
     if (count%10 == 0) args=realloc(args,sizeof(char*)*10); 
     count++; 
     args[count]=malloc(strlen(arg)); 
     strcpy(args[count],arg); 
    } 
    args[++count]=NULL; 
    if ((pid = fork()) == -1) 
    { 
     perror("Error forking...\n"); 
     exit(1); 
    } 
    if (pid == 0) 
    { 
     printf("Starting the executable as a new child process...\n"); 
     printf("Binary file to be executed: %s\n",binary); 
     execve(args[0],args,NULL); 
    } 
    else 
    { 
     int status; 
     waitpid(-1, &status, 0); 
     printf("Status returned by Child process: %d\n",WEXITSTATUS(status)); 
    } 
    return 0; 
} 
+0

如果程序位于同一目录中,即a.out的的目录,参数取.... guyz请帮助快..我有一个任务提交.. – 2010-02-02 17:45:55

+2

这是不值得...要求别人完成任务,甚至不需要自己调试!叹...我已经重申了你的问题,作为'家庭作业'... grrr – t0mm13b 2010-02-02 17:55:42

+0

我自己做了编码..我只是无法删除错误...所以,其实我试过这样做.. – 2010-02-02 18:15:10

回答

3

args数组中的第一个条目应该是程序名称。您的代码将--help作为进程名称调用/bin/ls

+0

应该是execve的参数值是什么让代码正常工作.. 是它..二进制= “/斌/ LS” &ARGS [0] = “/斌/ LS” 和args [1] =“ - -help“... 和args [0]和args [1]应为空终止。 – 2010-02-02 18:17:51

+0

是的。另外,'args [2]'必须是'NULL',你已经做到了。 – ephemient 2010-02-02 19:02:39

1

请检查以确保args是没有得到由realloc呼叫重挫。看到这里的SO关于realloc

编辑: 也回路看起来很滑稽.... 你叫strtok这样的:

 
binary=strtok(command," "); 

更改循环结构使用binary代替如图所示.. 。

 
char *tmpPtr; 
while (binary != NULL){ 
    if (count%10 == 0) tmpPtr=realloc(args,sizeof(char)*10); 
    if (tmpPtr != NULL) args = tmpPtr; 
    count++; 
    args[count-1]=malloc(strlen(binary)+1); 
    strcpy(args[count-1],binary); 
    binary = strtok(command, " "); 
} 

,并使用binary用于复制字符串....

希望这有助于 最好的问候, 汤姆。

1

你的程序有一些明显的错误。例如,声明char **args=NULL;,然后args=realloc(args,sizeof(char)*10);(因为它是char**,您应该是alloc -ing到char*,否?..)。

由于sizeof(char*)通常是4,而sizeof(char)通常是1,所以最终会出现一些严重的内存管理问题(您分配的内存管理问题比您使用的少,最终会写入不应该写的地方)。从那里开始,所有地狱崩溃,你不能指望你的程序的行为是有道理的。

我建议你通过UTIL运行您的程序,例如Valgrind找出内存泄漏并适当地纠正程序。内存问题得到纠正后,您的execve问题可能会消失。

+0

好的..我改了一下代码...现在请告诉..我认为在给execve()的参数时存在一些问题 – 2010-02-02 18:31:16