Naz*_*rke 0 c linux fork runtime-error exec
int status=0;
int PID = fork();
if(PID == 0)
{
char *path = strcat(pathToken,strcat("/",command));
printf("path: %s\n",path);
execl(path,command,"-l",NULL);
}
else if(PID>0)
{
printf("pid: %d. ",PID);
printf("I'm parent process\n");
wait(&status);
}
Run Code Online (Sandbox Code Playgroud)
输出:
pid: 20027. I'm parent process
Run Code Online (Sandbox Code Playgroud)
为什么不进入if(PID==0)?
您正在尝试修改字符串文字:
strcat("/",command)
Run Code Online (Sandbox Code Playgroud)
这会导致不确定的行为.
strcat的声明是:
char * strcat ( char * destination, const char * source );
Run Code Online (Sandbox Code Playgroud)
它会将源字符串的副本附加到目标字符串.