From 280f07f387ddd94e7827f339f486b4d0c455055d Mon Sep 17 00:00:00 2001 From: nimelehin Date: Mon, 16 Nov 2020 19:01:40 +0300 Subject: [PATCH] Add pid_get_last_allocated() --- kernel/task.c | 19 +++++++++++++------ kernel/task.h | 1 + 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/kernel/task.c b/kernel/task.c index 334a7a74f5..65e3ed1d8e 100644 --- a/kernel/task.c +++ b/kernel/task.c @@ -9,6 +9,7 @@ __thread struct task *current; +static dword_t last_allocated_pid = 0; static struct pid pids[MAX_PID + 1] = {}; lock_t pids_lock = LOCK_INITIALIZER; @@ -40,15 +41,21 @@ struct task *pid_get_task(dword_t id) { return task; } +struct pid *pid_get_last_allocated() { + if (!last_allocated_pid) { + return NULL; + } + return pid_get(last_allocated_pid); +} + struct task *task_create_(struct task *parent) { lock(&pids_lock); - static int cur_pid = 0; do { - cur_pid++; - if (cur_pid > MAX_PID) cur_pid = 1; - } while (!pid_empty(&pids[cur_pid])); - struct pid *pid = &pids[cur_pid]; - pid->id = cur_pid; + last_allocated_pid++; + if (last_allocated_pid > MAX_PID) last_allocated_pid = 1; + } while (!pid_empty(&pids[last_allocated_pid])); + struct pid *pid = &pids[last_allocated_pid]; + pid->id = last_allocated_pid; list_init(&pid->session); list_init(&pid->pgroup); diff --git a/kernel/task.h b/kernel/task.h index 6971040318..bb1c510651 100644 --- a/kernel/task.h +++ b/kernel/task.h @@ -171,6 +171,7 @@ struct pid { extern lock_t pids_lock; // these functions must be called with pids_lock struct pid *pid_get(dword_t pid); +struct pid *pid_get_last_allocated(void); struct task *pid_get_task(dword_t pid); struct task *pid_get_task_zombie(dword_t id); // don't return null if the task exists as a zombie