This is the first version of the kernel with fully isolated threads for user and kernel tasks, where process 0 is only used for scheduling and interrupt handling. User process have a "buddy" kernel process that handles its tasks in the kernel. This system allows for easy blocking of threads without much need for thought. Just `sswtch` in an infinite read loop, and your userspace process will be blocked. Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
50 lines
798 B
C
50 lines
798 B
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
/* shtest: simple userland shell test */
|
|
#include <stdint.h>
|
|
|
|
typedef int64_t ssize_t;
|
|
typedef int64_t pid_t;
|
|
|
|
int open(const char *pathname, int flags);
|
|
ssize_t read(int fd, void *buf, int count);
|
|
ssize_t write(int fd, void *buf, int count);
|
|
int close(int fd);
|
|
pid_t fork();
|
|
int exit(int status);
|
|
|
|
void promptfd(int fd)
|
|
{
|
|
write(fd, "shtest> ", 8);
|
|
}
|
|
|
|
void _start()
|
|
{
|
|
int fd = open("/dev/tty0", 0);
|
|
if (fd < 0)
|
|
exit(1);
|
|
|
|
promptfd(fd);
|
|
|
|
char buf[256];
|
|
int n;
|
|
|
|
/* fork test */
|
|
pid_t pid = fork();
|
|
if (pid < 0) {
|
|
write(fd, "fork failed\n", 12);
|
|
exit(1);
|
|
}
|
|
|
|
if (pid == 0) {
|
|
write(fd, "child\n", 6);
|
|
while (1)
|
|
;
|
|
} else {
|
|
write(fd, "parent\n", 7);
|
|
}
|
|
|
|
/* rw test */
|
|
while ((n = read(fd, buf, 256)) > 0) {
|
|
write(fd, buf, n);
|
|
}
|
|
}
|