我正在尝试在xCode中访问子项的退出状态,但我不断获得0
。但是代码在终端内部运行。我错过了什么吗?
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char * argv[]) {
int pid, val1;
char string[11];
switch ((pid = fork())) {
case -1:
printf("An error occured.");
break;
case 0:
printf("Enter max 10 characters: ");
scanf("%10[^\n]%*c", string);
exit((int)strlen(string));
default:
wait(&val1);
printf("Child %d died. Count = %d\n", pid, WEXITSTATUS(val1));
break;
}
return EXIT_SUCCESS;
}
xCode的控制台结果:
Enter max 10 characters: Hello
Child 19493 died. Count = 0
Program ended with exit code: 0
在终端内:
Enter max 10 characters: Hello
Child 19504 died. Count = 5