From 727e227b09ed0c303e87728af531c6c3a025fd36 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 22 Oct 2022 14:26:59 +0200 Subject: [PATCH] apps: Add ps (uses pstat) --- apps/Makefile | 2 +- apps/src/block.c | 34 ---------------------------------- apps/src/ps.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 35 deletions(-) delete mode 100644 apps/src/block.c create mode 100644 apps/src/ps.c diff --git a/apps/Makefile b/apps/Makefile index ce699bd4..601b773a 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -1,4 +1,4 @@ -APPS := init sym sh crash uname uptime hello block +APPS := init sym sh crash uname uptime hello ps APPS_DIR := $(LUNA_ROOT)/apps APPS_SRC := $(APPS_DIR)/src diff --git a/apps/src/block.c b/apps/src/block.c deleted file mode 100644 index 1af9e98e..00000000 --- a/apps/src/block.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include - -int main() -{ - int fd = open("/dev/kbd", O_RDONLY | O_NONBLOCK); - if (fd < 0) - { - perror("open"); - return 1; - } - - FILE* fp = fdopen(fd, "r"); - if (!fp) - { - perror("fdopen"); - close(fd); - return 1; - } - - char buf[32]; - fread(buf, sizeof(buf) - 1, 1, fp); - if (ferror(fp)) - { - perror("fread"); - fclose(fp); - return 1; - } - - fclose(fp); - - return 0; -} \ No newline at end of file diff --git a/apps/src/ps.c b/apps/src/ps.c new file mode 100644 index 00000000..caf7febe --- /dev/null +++ b/apps/src/ps.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +pid_t get_current_max_threads() +{ + pid_t result = pstat(-1, NULL); + if (result < 0) + { + perror("pstat(-1)"); + exit(1); + } + return result; +} + +void display_process(struct pstat* pstatbuf) +{ + printf("%ld %ld %s %s (%ld ms)\n", pstatbuf->pt_pid, pstatbuf->pt_ppid, pstatbuf->pt_name, pstname(pstatbuf), + pstatbuf->pt_time); +} + +int try_pstat(pid_t pid, struct pstat* pstatbuf) +{ + pid_t result = pstat(pid, pstatbuf); + if (result < 0) + { + if (errno == ESRCH) return 0; + perror("pstat"); + exit(1); + } + return 1; +} + +int main() +{ + struct pstat pst; + pid_t max = get_current_max_threads(); + for (pid_t pid = 0; pid <= max; pid++) + { + if (try_pstat(pid, &pst)) { display_process(&pst); } + } +} \ No newline at end of file