Skip to content

Commit

Permalink
Add pid_get_last_allocated()
Browse files Browse the repository at this point in the history
  • Loading branch information
nimelehin committed Nov 20, 2020
1 parent 090f58a commit 280f07f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
19 changes: 13 additions & 6 deletions kernel/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions kernel/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 280f07f

Please sign in to comment.