<分区>
来自 APUE 的 Unix system
函数的实现:
Figure 8.22 The
system
function, without signal handling
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
int
system(const char *cmdstring) /* version without signal handling */
{
pid_t pid;
int status;
if (cmdstring == NULL)
return(1); /* always a command processor with UNIX */
if ((pid = fork()) < 0) {
status = -1; /* probably out of processes */
} else if (pid == 0) { /* child */
execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
_exit(127); /* execl error */
} else { /* parent */
while (waitpid(pid, &status, 0) < 0) {
if (errno != EINTR) {
status = -1; /* error other than EINTR from waitpid() */
break;
}
}
// if(waitpid(pid, &status, 0) < 0){
// if(errno != EINTR){
// status = -1;
// }
// }
}
return(status);
}
为什么它对 waitpid
使用 while
循环而不是我在注释中添加的 if
语句?我尝试了 if
,到目前为止没有出错。