[英]Error with fork, execvp in C?
我在使用execvp和fork時遇到問題。 當我運行代碼時,即使文件已創建,exec也無法工作。 它僅返回“錯誤地址”錯誤。 另外,當我運行printf(“ in child”)時,它不會出現。 我的代碼如何在不打印“ in the child”的情況下進入execvp?
pid = fork();
switch(pid) {
case -1:
fprintf(stderr,"ERROR WITH FORK\n");
exit(1);
break;
case 0:
printf("in the child");
fd = open(filename,O_CREAT | O_APPEND,0777);
if(dup2(fd,1) < 0)
{
fprintf(stderr,"dup error: %s",strerror(errno));
}
if(execvp(command,args) == -1) //is null terminated
{
fprintf(stderr,"exec error %s\n",strerror(errno));
}
break;
default:
wait(NULL);
break;
指針數組( execvp()
的第二個參數)必須以NULL
指針終止。 在args
末尾附加0
或NULL
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.