From 81a58004d51547d074b4218f906b0b95f2b2c5dc Mon Sep 17 00:00:00 2001 From: dzwdz Date: Thu, 4 Aug 2022 15:58:54 +0200 Subject: syscalls: add _syscall_sleep() --- src/user/app/shell/builtins.c | 9 +++++++++ src/user/app/tests/tests.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) (limited to 'src/user/app') diff --git a/src/user/app/shell/builtins.c b/src/user/app/shell/builtins.c index 1a46003..d4de354 100644 --- a/src/user/app/shell/builtins.c +++ b/src/user/app/shell/builtins.c @@ -117,6 +117,14 @@ static void cmd_ls(int argc, char **argv) { } } +static void cmd_sleep(int argc, char **argv) { + if (argc < 2) { + eprintf("no arguments"); + return; + } + _syscall_sleep(strtol(argv[1], NULL, 0) * 1000); +} + static void cmd_touch(int argc, char **argv) { if (argc <= 1) { eprintf("no arguments"); @@ -135,6 +143,7 @@ struct builtin builtins[] = { {"echo", cmd_echo}, {"hexdump", cmd_hexdump}, {"ls", cmd_ls}, + {"sleep", cmd_sleep}, {"touch", cmd_touch}, {NULL, NULL}, }; diff --git a/src/user/app/tests/tests.c b/src/user/app/tests/tests.c index d655ab9..5c40bcd 100644 --- a/src/user/app/tests/tests.c +++ b/src/user/app/tests/tests.c @@ -254,7 +254,37 @@ static void test_strtol(void) { assert(!strcmp("hello", end)); assert(01234 == strtol(" 01234hello", &end, 0)); assert(!strcmp("hello", end)); +} + +static void test_sleep(void) { + // TODO yet another of those fake tests that you have to verify by hand + if (!fork()) { + if (!fork()) { + _syscall_sleep(100); + printf("1\n"); + _syscall_sleep(200); + printf("3\n"); + _syscall_sleep(200); + printf("5\n"); + _syscall_exit(0); + } + if (!fork()) { + printf("0\n"); + _syscall_sleep(200); + printf("2\n"); + _syscall_sleep(200); + printf("4\n"); + /* get killed while asleep + * a peaceful death, i suppose. */ + for (;;) _syscall_sleep(1000000000); + } + _syscall_await(); + _syscall_exit(0); + } + /* this part checks if, after killing an asleep process, other processes can still wake up */ + _syscall_sleep(600); + printf("6\n"); } static void test_misc(void) { @@ -276,6 +306,7 @@ int main(void) { run_forked(test_execbuf); run_forked(test_printf); run_forked(test_strtol); + run_forked(test_sleep); run_forked(test_misc); return 1; } -- cgit v1.2.3