을 가져올 때 나는 다음과 같은 문제가 실패, 대기 : 내 코드에서, 여기에 라인 (83)에, 나는이 있습니다 check = wait(NULL);
C - 반환 값
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include <sys/stat.h>
#include <sys/types.h>
//---------------------------------
//Function: parse_cmdline(const char* cmdline)
//This function takes the input from stdin
//and returns it as array of strings.
//If stdin is /bin/ls -l /usr/include
//the function will return ["/bin/ls","-l","/usr/include"]
//---------------------------------
char** parse_cmdline(const char* cmdline) {
int count, word_count = 0;
char** line_parsed, line_return;
char *pch, *cmdline_copy = (char*)malloc(sizeof(char)*(strlen(cmdline)+1));
strcpy(cmdline_copy, cmdline);
pch = strtok(cmdline_copy," \n\t\r");
while (pch != NULL) {
++word_count;
pch = strtok(NULL, " \n\t\r");
}
line_parsed = (char**)malloc((word_count+1)*sizeof(char*));
count = 0;
strcpy(cmdline_copy, cmdline);
pch = strtok(cmdline_copy," \n\t\r");
while (pch != NULL) {
line_parsed[count] = (char*)malloc((strlen(pch) + 1)*sizeof(char));
strcpy(line_parsed[count], pch);
++count;
pch = strtok(NULL," \n\t\r");
}
line_parsed[count] = NULL;
free(cmdline_copy);
return line_parsed;
}
int main() {
int count = 0, check;
size_t size;
char* line;
char** cmdline;
while(1) {
check = 0;
printf("$Monkey Eats:< ");
getline(&line, &size, stdin);
cmdline = parse_cmdline(line);
pid_t pid = fork();
if (pid == -1) {
perror("fork");
return -1;
} else if(pid == 0) {
struct stat _stat;
stat(cmdline[0],&_stat);
if(_stat.st_mode & S_IXUSR){
execvp(cmdline[0], cmdline);
}else fprintf(stderr,"%s: Permission denied!\n",cmdline[0]);
perror("");
exit(1);
}else {
check = wait(NULL);
}
count = 0;
while(cmdline[count] != NULL) {
free(cmdline[count]);
++count;
}
free(cmdline);
}
return 0;
}
을 그것은 내게 문제가 있습니다. 나는 그것을 실행하고 나는 명령을 입력 할 때 나는 다음과 같은 메시지가있을 때 : 만 wait(NULL);
이있는 경우
$Monkey Eats:< ls
ls: Permission denied!
No such file or directory
프로그램은 문제없이 정상적으로 실행됩니다. 누군가 문제를 말해 줄 수 있습니까? 고마워요 :)
하지만 난 기다려야 만 한 경우 (NULL); 그것을 확인하지 않고 값을 저장하지 않고도 경로를 입력하지 않아도 정상적으로 작동합니다. – user3132352
'wait (NULL);을 가지고 있다면 어떻게 작동하는지 알 수 있습니까? – woolstar