当前位置: 技术问答>linux和unix
fork()函数的问题,在虚拟机上好用,手机上不好用!
来源: 互联网 发布时间:2016-01-12
本文导语: 在虚拟机上,程序执行成功,WEXITSTATUS(status)返回0;在手机上(Linux环境)WEXITSTATUS(status)返回1,cp操作失败。谁知道为什么?一样的代码。 BOOL CopyFile(CHAR* sourcePath, CHAR* destPath) { char prog[20] = "/bin/cp"; char *...
在虚拟机上,程序执行成功,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 %sn.",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 = %dn", res) ;
return TRUE;
/* strerror(errno) can get the cause */
}
return FALSE;
}
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 %sn.",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 = %dn", res) ;
return TRUE;
/* strerror(errno) can get the cause */
}
return FALSE;
}
|
UP