2010-02-02 50 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