그래서 교수님이 나에게 준 코드를 실행하려고합니다. 그것은 간단합니다. 포크 (fork)는 포크가 제대로 작동하는지 확인한 다음 별도의 파일에서 다른 코드를 실행합니다.프로세스 포크가 원하는 코드를 실행하지 않습니다.
내 OS X 10.9.5 시스템에서 두 번째 코드가 실행되지 않습니다. 다음은 프로그램의 모두 :
exercise.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t child = fork();
if ((int)child < 0) {
fprintf(stderr, "fork error!\n");
exit(0);
} else if ((int)child > 0) {
int status;
(void) waitpid(child, &status, 0);
if (WIFEXITED(status)) {
printf("child %d exited normally and returned %d\n",
child, WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("\nchild %d was killed by signal number %d and %s leave a core dump\n",
child, WTERMSIG(status), (WCOREDUMP(status) ? "did" : "didn't"));
} else {
printf("child %d is dead and I don't know why!\n", child);
}
} else {
char *argv[] = { "./getcode" };
execve(argv[0], argv, NULL);
}
return 0;
}
그리고 getcode.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
int rc = 256;
printf("I am child process %d\n", getpid());
while ((rc > 255) || (rc < 0)) {
printf("please type an integer from 0 to 255: ");
scanf("%d", &rc);
}
exit(rc);
}
는 내가 명령을 모두 컴파일 :
gcc -Wall -pedantic-errors exercise.c -o exercise
및
gcc -Wall -pedantic-errors getcode.c -o getcode
불행하게도, 난 다시 자식 프로세스에서 얻을 수있는 유일한 것은 내가 당황 해요 0
./exercise
child 903 exited normally and returned 0
의 리턴 코드입니다. 누구든지 도와 줄 수 있습니까?
EDIT : 좋아요, 요청에 따라 perror("execve")
을 포함 시켰으며, execve: Bad address
을 반환합니다. 어떻게 해결할 수 있습니까?
EDIT2 : 좋습니다. 나는 그것을 고쳤다. 위 코드의 내용을 다음과 같이 변경했습니다.
char *argv[] = { "./getcode",NULL };
execve(argv[0], argv, NULL);
null 종료로 인해 argv 문제가 해결되었습니다.
'char * argv [] = { "./getcode", NULL};' – wildplasser