Add a few example apps/demos in C, and make the kernel choose a random one to demonstrate every boot

This commit is contained in:
apio 2022-10-03 21:24:38 +02:00
parent 885e39f60f
commit 5d8b825659
6 changed files with 133 additions and 2 deletions

View File

@ -1,4 +1,4 @@
APPS := init
APPS := init fib leap art memeater
APPS_DIR := $(LUNA_ROOT)/apps
APPS_SRC := $(APPS_DIR)/src

18
apps/src/art.c Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>
int main()
{
sleep(1);
syscall(SYS_paint, 0, 0, 400, 300, 0x000000FF);
sleep(1);
printf("Hello, colorful world!\n");
sleep(1);
printf("Press any key to restart.");
}

34
apps/src/fib.c Normal file
View File

@ -0,0 +1,34 @@
#include <luna.h>
#include <stdio.h>
#include <unistd.h>
void fib_next(unsigned long int* a, unsigned long int* b)
{
unsigned long int _a = *a;
unsigned long int _b = *b;
*a = *b;
*b = _a + _b;
}
int main()
{
unsigned long int fib_a = 1;
unsigned long int fib_b = 1;
printf("Calculating the 50 first Fibonacci numbers...\n");
sleep(2);
printf("%lu\n", fib_a);
msleep(500);
for (int i = 0; i < 49; i++)
{
printf("%lu\n", fib_b);
fib_next(&fib_a, &fib_b);
msleep(500);
}
printf("\nDone, press any key to restart.\n");
}

38
apps/src/leap.c Normal file
View File

@ -0,0 +1,38 @@
#include <luna.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>
unsigned long random_year() // Return a year from 1000 to 2500.
{
unsigned long result = syscall(SYS_rand);
return (result % 1500) + 1000;
}
int is_leap(unsigned long year)
{
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
int main()
{
printf("Welcome to the Luna Leap Year Program!\n\n");
sleep(1);
printf("Choosing a random year between 1000 and 2500... ");
msleep(500);
unsigned long year = random_year();
printf("%lu!\n\n", year);
sleep(1);
printf("%lu is %s\n", year, is_leap(year) ? "a leap year!" : "not a leap year :(");
msleep(500);
printf("Press any key to restart.\n");
}

17
apps/src/memeater.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define CHUNK 4194304 // 4 MB
int main()
{
printf("Welcome to memeater! This little program is designed to eat up all the memory on your system.\n");
sleep(1);
void* allocated = malloc(CHUNK);
do {
printf("Allocating 4 MB of memory... %lx\n", (unsigned long)allocated);
sleep(1);
} while ((allocated = malloc(CHUNK)));
printf("Out of memory.\n");
}

View File

@ -124,7 +124,31 @@ extern "C" void _start()
}
});
Scheduler::load_user_task("bin/init");
int demo = Mersenne::get() % 5;
switch (demo)
{
case 0:
kinfoln("Loading demo: example init program");
Scheduler::load_user_task("bin/init");
break;
case 1:
kinfoln("Loading demo: first 50 fibonacci numbers");
Scheduler::load_user_task("bin/fib");
break;
case 2:
kinfoln("Loading demo: leap year calculator");
Scheduler::load_user_task("bin/leap");
break;
case 3:
kinfoln("Loading demo: painting program");
Scheduler::load_user_task("bin/art");
break;
case 4:
kinfoln("Loading demo: memory eating program");
Scheduler::load_user_task("bin/memeater");
break;
}
kinfoln("Prepared scheduler tasks");