如何在linux中编写一个类似CreateProcess的函数

如题所述

//本函数执行exepath指定的程序 //执行成功和失败无法返回 int execute_new(const char *exepath,const char *arg,unsigned int &retpid) { if(exepath == NULL) { return 0; } int argcount=0; char *argv[1024]={0}; char argsave[1024]={0}; char *argline = argsave; argv[0] = (char *)exepath; argcount++; ////////////分割和提取参数,作为execvp函数的参数 if(arg != NULL) { strncpy(argsave,arg,1024-1); int is_find_space = -1; //-1, if(*argline != ' ') { argv[argcount] = (char *)argline; argcount++; } while(*argline != '\0') { if(*argline == ' ' && *(argline+1) != ' ' && *(argline+1) != '\0') { argv[argcount] = (char *)(argline+1); argcount++; } if(*argline == ' ') { *argline='\0'; } argline++; } } pid_t pid; int status; if((pid = fork())<0){ status = -1; } else if(pid == 0){ execv(exepath,argv); _exit(127); //子进程正常执行则不会执行此语句 } else{ retpid = pid; waitpid(pid, &status, WNOHANG|WUNTRACED); } return status; }
温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜