~emersion/mrsh

ref: 9371d6094e78645f0ec0c85b6cd5b6c8ec9638fc mrsh/shell/process.c -rw-r--r-- 1.0 KiB
9371d609Drew DeVault Implement times, : builtins 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <mrsh/array.h>
#include <string.h>
#include <sys/wait.h>
#include "shell.h"

static struct mrsh_array running_processes = {0};

void process_init(struct process *proc, pid_t pid) {
	mrsh_array_add(&running_processes, proc);
	proc->pid = pid;
	proc->finished = false;
	proc->stat = 0;
}

int process_poll(struct process *proc) {
	if (!proc->finished) {
		return -1;
	}
	return WEXITSTATUS(proc->stat);
}

static void process_remove(struct process *proc) {
	for (size_t i = 0; i < running_processes.len; ++i) {
		if (running_processes.data[i] == proc) {
			memmove(&running_processes.data[i], &running_processes.data[i + 1],
				running_processes.len - i - 1);
			--running_processes.len;
			break;
		}
	}
}

void process_finish(struct process *proc) {
	process_remove(proc);
}

void process_notify(pid_t pid, int stat) {
	for (size_t i = 0; i < running_processes.len; ++i) {
		struct process *proc = running_processes.data[i];
		if (proc->pid == pid) {
			proc->finished = true;
			proc->stat = stat;
			process_remove(proc);
			break;
		}
	}
}