[英]Using fork and execvp in c causing error
我正在嘗試使用fork / execvp從我的C程序中執行另一個程序。 它正在工作,但是在ls輸出我目錄的內容之前,我得到了幾百行:
ls: cannot access : No such file or directory
然后在目錄中正確列出文件。 我要傳遞給execvp:
char **argv = {"ls", "/home/school/"};
char *command = "ls";
由於某些原因,當我使用路徑“ / home / school”時,找不到目錄。 有誰知道為什么會這樣,為什么ls說它無法訪問?
PID = fork();
i = 0;
if(i == numArgs) { doneFlag = 1; }
if (PID == 0){//child process
execvp(command, argv);//needs error checking
}
else if(PID > 0){
wait(PID, 0, 0);//wait for child process to finish execution
}
else if(PID == -1){
printf("ERROR:\n");
switch (errno){
case EAGAIN:
printf("Cannot fork process: System Process Limit Reached\n");
case ENOMEM:
printf("Cannot fork process: Out of memory\n");
}
return 1;
}
execvp()
傳遞的char**
參數必須以NULL
終止。
and functions provide an array of pointers to null-terminated strings that represent the argument list available to the new program. 和函數提供了一個指針數組,這些指針指向以空值結尾的字符串 ,這些字符串表示新程序可用的參數列表。
第一個參數應指向與正在執行的文件關聯的文件名。
be terminated by a NULL pointer. 指針數組以NULL指針終止。 在argv*
的末尾附加0
或NULL
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.