내가 몇 UNIX 명령을 실행하는 코드의 조각을 실행하는 것을 시도하고있다가,이 명령은 배열 lineArray에 저장된 명령을 실행하지 않습니다 내 함수가 execvp MAN에 따라 명령과 인수를 올바르게 구문 분석한다는 것을 디버그에서 보았지만 배열의 첫 번째 명령 만 출력합니다.execvp는이 예를 들어
모든 종류의 도움을 주실 수 있습니다.
int startProcesses(int background) {
int i = 0;
int j = 0;
int pid;
int status;
char *copyProcessName[256];
int len, var=0;
while(lineArray[i] != NULL) {
while(*(copyProcessName+var) != NULL) {
copyProcessName[var] = NULL;
}
j=0;
copyProcessName[j] = strtok(lineArray[i], " ");
while (copyProcessName[j] != NULL){
j++;
copyProcessName[j] = strtok(NULL, " ");
}
pid = fork();
if (pid == 0) {
// Child Process
execvp(copyProcessName[0], copyProcessName);
fflush(stdout);
i++;
continue;
} else if (!background) {
// Parent Process
waitpid(pid, &status, 0);
i++;
if(WEXITSTATUS(status)) {
printf(CANNOT_RUN_ERROR);
return 1;
}
} else {
i++;
continue;
}
}
return 0;
}
'len = strlen (copyProcessName); ', 어떻게 생각하세요? 'copyProcessName'은'char * [256]'이므로'strlen'의 인자로'char **'로 변환됩니다. 컴파일러가 그것에 대해 불평하지 않았습니까? 또한'execvp'가 반환되면 자식은'continue'가 아니라 죽어야합니다. –