fork()函数的问题,在虚拟机上好用,手机上不好用!
在虚拟机上,程序执行成功,WEXITSTATUS(status)返回0;在手机上(Linux环境)WEXITSTATUS(status)返回1,cp操作失败。谁知道为什么?一样的代码。
BOOL CopyFile(CHAR* sourcePath, CHAR* destPath)
{
char prog[20] = "/bin/cp ";
char *arg[10];
int i = 0;
int pid;
int res;
MTP_DBG( "prepare to move file from %s to %s\n. ",sourcePath, destPath);
if ( NULL == sourcePath )
return FALSE;
if ( NULL == destPath )
return FALSE;
pid = fork();
if (pid == 0)
{ /* child */
arg[i++] = prog;
arg[i++] = "-f ";
arg[i++] = sourcePath;
arg[i++] = destPath;
execv(prog,arg);
exit(1); /* exec failed */
}
else if (pid != -1)
{
/* parent */
int status;
// wait(&status);
MTP_DBG( "Parent process: before waiting child.\n ") ;
waitpid(pid,&status,0);
res = (WIFEXITED(status)? WEXITSTATUS(status):-1);
MTP_DBG( "Parent process: after waiting child, res = %d\n ", res) ;
return TRUE;
/* strerror(errno) can get the cause */
}
return FALSE;
}
------解决方案--------------------UP