From ee80db4d9ac87dbb0eedc3a50ab7e5bafac6633d Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sun, 21 Apr 2019 18:40:59 +0300 Subject: [PATCH] Introduce job_poll --- include/shell/job.h | 7 +++++++ shell/job.c | 30 +++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/include/shell/job.h b/include/shell/job.h index 5051405..2189c61 100644 --- a/include/shell/job.h +++ b/include/shell/job.h @@ -43,6 +43,13 @@ bool job_terminated(struct mrsh_job *job); * have terminated. */ bool job_stopped(struct mrsh_job *job); +/** + * Polls the job's current status without blocking. Returns: + * - An integer >= 0 if the job has terminated + * - TASK_STATUS_STOPPED if the job is stopped + * - TASK_STATUS_WAIT if the job is running + */ +int job_poll(struct mrsh_job *job); /** * Wait for the completion of the job. */ diff --git a/shell/job.c b/shell/job.c index ac9eebc..27355b5 100644 --- a/shell/job.c +++ b/shell/job.c @@ -184,8 +184,34 @@ bool job_set_foreground(struct mrsh_job *job, bool foreground, bool cont) { return true; } +int job_poll(struct mrsh_job *job) { + int proc_status = 0; + bool stopped = false; + for (size_t j = 0; j < job->processes.len; ++j) { + struct process *proc = job->processes.data[j]; + proc_status = process_poll(proc); + if (proc_status == TASK_STATUS_WAIT) { + return TASK_STATUS_WAIT; + } + if (proc_status == TASK_STATUS_STOPPED) { + stopped = true; + } + } + + if (stopped) { + return TASK_STATUS_STOPPED; + } + // All processes have terminated, return the last one's status + return proc_status; +} + int job_wait(struct mrsh_job *job) { - while (!job_stopped(job) && !job_terminated(job)) { + while (true) { + int status = job_poll(job); + if (status != TASK_STATUS_WAIT) { + return status; + } + int stat; pid_t pid = waitpid(-1, &stat, WUNTRACED); if (pid == -1) { @@ -198,8 +224,6 @@ int job_wait(struct mrsh_job *job) { update_job(job->state, pid, stat); } - - return 0; // TODO: return the job's status } bool init_job_child_process(struct mrsh_state *state) { -- 2.26.2