apps: Add ps (uses pstat)

This commit is contained in:
apio 2022-10-22 14:26:59 +02:00
parent 6ac57a2f5c
commit 727e227b09
3 changed files with 45 additions and 35 deletions

View File

@ -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

View File

@ -1,34 +0,0 @@
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
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;
}

44
apps/src/ps.c Normal file
View File

@ -0,0 +1,44 @@
#include <errno.h>
#include <luna/pstat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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); }
}
}