在构建一个Shell程序时,我遇到了识别进程状态的问题。我所面临的问题描述如下:我有一个子进程列表,我试图使用
waitpid
和WNOHANG
来确定它们的状态。我希望区分三种状态:终止
、运行
和暂停
。(如下代码所定义)
我希望将进程状态更改为上述三种之一,但现在这个函数会将正在运行的进程状态更改为终止
,并且该函数也无法识别已暂停的进程。
我想知道我做错了什么,应该如何编写updateProcessList
函数来实现它?#define TERMINATED -1
#define RUNNING 1
#define SUSPENDED 0
typedef struct process{
cmdLine* cmd; /* the parsed command line*/
pid_t pid; /* the process id that is running the command*/
int status; /* status of the process: RUNNING/SUSPENDED/TERMINATED */
struct process *next; /* next process in chain */
} process;
void updateProcessList(process **process_list) {
process *p = *process_list;
int code = 0, status = 0,pidd = 0;
while (p) {
pidd = p->pid;
code = waitpid(pidd, &status, WNOHANG);
if (code == -1) { /* child terminated*/
p->status = TERMINATED;
} else if(WIFEXITED(status)){
p->status = TERMINATED;
}else if(WIFSTOPPED(status)){
p->status = SUSPENDED;
}
p = p->next;
}
}