如何使用 waitpid() 获取子进程的状态?

how to use waitpid() to get the status of child process?

我正在尝试获取子进程的状态。例如,当我运行 "adjsafkl" 是一个不存在的命令时,系统打印"WRONG",但是当我运行 "cp sssa" 该文件夹不存在时要么,但系统不会将此命令标记为错误。我想知道有没有办法修复这个错误?

您可以在 WIFEXITED 后检查 WEXITSTATUS:

WIFEXITED: returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().

WEXITSTATUS: returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main(). This macro should only be employed if WIFEXITED returned true.