gpt4 book ai didi

c WEXITSTATUS() 以 255 退出

转载 作者:行者123 更新时间:2023-11-30 15:10:57 26 4
gpt4 key购买 nike

当执行这段操作系统代码时,我收到信息“Exited with value 255”。我通过键盘收到命令,并且我知道该字符串是正确的。当我收到错误消息时,编程不显示(例如)键盘收到的 ls -l

    printf("Command? ");
scanf(" %99[^\n]", str);

p = fork();
if(p > 0 ){ //Dad wait for the child
wait(&status);
if(WIFEXITED(status)){
printf("%d\n",WEXITSTATUS(status));
}
}else{ //Child execute the execlp
execlp(str, str,NULL);
exit(-1);
}

谢谢大家!标记

最佳答案

execlp()期望参数被分开;您的字符串输入ls -l不是有效的现有可执行程序:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

char *args[] = { "ls", "-l" };
// int main (int argc, char **argv)
int main (void)
{
int p;
int status;

p = fork();
if(p > 0 ){ //Dad wait for the child
wait(&status);
if (WIFEXITED(status)){
printf("%d\n", WEXITSTATUS(status));
}
}else{ //Child execute the execlp
execlp(args[0], args[0], args[1] ,NULL);
exit (-1);
}

exit (0);
}
<小时/>

另请注意 exit(-1) (除了无效之外:您应该使用 EXIT_FAILURE)产生 0xaaaaaaFF 的退出结果;仅较低的几位 (8) 位用于实际退出值;较高的aaaaaa位用于退出原因等。 -->>参见WEXITSTATUS()的定义和 friend <sys/wait.h> .

关于c WEXITSTATUS() 以 255 退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35927229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com