update name of code to labcodes

This commit is contained in:
chyyuu
2013-09-17 22:21:48 +08:00
parent 759eca9dda
commit 3f8d5876b9
726 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
#include <stdio.h>
#include <ulib.h>
int
main(void) {
int pid, exit_code;
if ((pid = fork()) == 0) {
cprintf("fork ok.\n");
int i;
for (i = 0; i < 10; i ++) {
yield();
}
exit(0xbeaf);
}
assert(pid > 0);
assert(waitpid(-1, NULL) != 0);
assert(waitpid(pid, (void *)0xC0000000) != 0);
assert(waitpid(pid, &exit_code) == 0 && exit_code == 0xbeaf);
cprintf("badarg pass.\n");
return 0;
}

View File

@@ -0,0 +1,11 @@
#include <stdio.h>
#include <ulib.h>
/* try to load the kernel's TSS selector into the DS register */
int
main(void) {
asm volatile("movw $0x28,%ax; movw %ax,%ds");
panic("FAIL: T.T\n");
}

View File

@@ -0,0 +1,11 @@
#include <stdio.h>
#include <ulib.h>
int zero;
int
main(void) {
cprintf("value is %d.\n", 1 / zero);
panic("FAIL: T.T\n");
}

34
labcodes/lab6/user/exit.c Normal file
View File

@@ -0,0 +1,34 @@
#include <stdio.h>
#include <ulib.h>
int magic = -0x10384;
int
main(void) {
int pid, code;
cprintf("I am the parent. Forking the child...\n");
if ((pid = fork()) == 0) {
cprintf("I am the child.\n");
yield();
yield();
yield();
yield();
yield();
yield();
yield();
exit(magic);
}
else {
cprintf("I am parent, fork a child pid %d\n",pid);
}
assert(pid > 0);
cprintf("I am the parent, waiting now..\n");
assert(waitpid(pid, &code) == 0 && code == magic);
assert(waitpid(pid, &code) != 0 && wait() != 0);
cprintf("waitpid %d ok.\n", pid);
cprintf("exit pass.\n");
return 0;
}

View File

@@ -0,0 +1,9 @@
#include <stdio.h>
#include <ulib.h>
int
main(void) {
cprintf("I read %8x from 0.\n", *(unsigned int *)0);
panic("FAIL: T.T\n");
}

View File

@@ -0,0 +1,9 @@
#include <stdio.h>
#include <ulib.h>
int
main(void) {
cprintf("I read %08x from 0xfac00000!\n", *(unsigned *)0xfac00000);
panic("FAIL: T.T\n");
}

View File

@@ -0,0 +1,34 @@
#include <ulib.h>
#include <stdio.h>
const int max_child = 32;
int
main(void) {
int n, pid;
for (n = 0; n < max_child; n ++) {
if ((pid = fork()) == 0) {
cprintf("I am child %d\n", n);
exit(0);
}
assert(pid > 0);
}
if (n > max_child) {
panic("fork claimed to work %d times!\n", n);
}
for (; n > 0; n --) {
if (wait() != 0) {
panic("wait stopped early\n");
}
}
if (wait() == 0) {
panic("wait got too many\n");
}
cprintf("forktest pass.\n");
return 0;
}

View File

@@ -0,0 +1,37 @@
#include <ulib.h>
#include <stdio.h>
#include <string.h>
#define DEPTH 4
void forktree(const char *cur);
void
forkchild(const char *cur, char branch) {
char nxt[DEPTH + 1];
if (strlen(cur) >= DEPTH)
return;
snprintf(nxt, DEPTH + 1, "%s%c", cur, branch);
if (fork() == 0) {
forktree(nxt);
yield();
exit(0);
}
}
void
forktree(const char *cur) {
cprintf("%04x: I am '%s'\n", getpid(), cur);
forkchild(cur, '0');
forkchild(cur, '1');
}
int
main(void) {
forktree("");
return 0;
}

View File

@@ -0,0 +1,11 @@
#include <stdio.h>
#include <ulib.h>
int
main(void) {
cprintf("Hello world!!.\n");
cprintf("I am process %d.\n", getpid());
cprintf("hello pass.\n");
return 0;
}

View File

@@ -0,0 +1,14 @@
.text
.globl _start
_start:
# set ebp for backtrace
movl $0x0, %ebp
# move down the esp register
# since it may cause page fault in backtrace
subl $0x20, %esp
# call user-program function
call umain
1: jmp 1b

View File

@@ -0,0 +1,28 @@
#include <defs.h>
#include <stdarg.h>
#include <stdio.h>
#include <ulib.h>
#include <error.h>
void
__panic(const char *file, int line, const char *fmt, ...) {
// print the 'message'
va_list ap;
va_start(ap, fmt);
cprintf("user panic at %s:%d:\n ", file, line);
vcprintf(fmt, ap);
cprintf("\n");
va_end(ap);
exit(-E_PANIC);
}
void
__warn(const char *file, int line, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
cprintf("user warning at %s:%d:\n ", file, line);
vcprintf(fmt, ap);
cprintf("\n");
va_end(ap);
}

View File

@@ -0,0 +1,62 @@
#include <defs.h>
#include <stdio.h>
#include <syscall.h>
/* *
* cputch - writes a single character @c to stdout, and it will
* increace the value of counter pointed by @cnt.
* */
static void
cputch(int c, int *cnt) {
sys_putc(c);
(*cnt) ++;
}
/* *
* vcprintf - format a string and writes it to stdout
*
* The return value is the number of characters which would be
* written to stdout.
*
* Call this function if you are already dealing with a va_list.
* Or you probably want cprintf() instead.
* */
int
vcprintf(const char *fmt, va_list ap) {
int cnt = 0;
vprintfmt((void*)cputch, &cnt, fmt, ap);
return cnt;
}
/* *
* cprintf - formats a string and writes it to stdout
*
* The return value is the number of characters which would be
* written to stdout.
* */
int
cprintf(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
int cnt = vcprintf(fmt, ap);
va_end(ap);
return cnt;
}
/* *
* cputs- writes the string pointed by @str to stdout and
* appends a newline character.
* */
int
cputs(const char *str) {
int cnt = 0;
char c;
while ((c = *str ++) != '\0') {
cputch(c, &cnt);
}
cputch('\n', &cnt);
return cnt;
}

View File

@@ -0,0 +1,82 @@
#include <defs.h>
#include <unistd.h>
#include <stdarg.h>
#include <syscall.h>
#define MAX_ARGS 5
static inline int
syscall(int num, ...) {
va_list ap;
va_start(ap, num);
uint32_t a[MAX_ARGS];
int i, ret;
for (i = 0; i < MAX_ARGS; i ++) {
a[i] = va_arg(ap, uint32_t);
}
va_end(ap);
asm volatile (
"int %1;"
: "=a" (ret)
: "i" (T_SYSCALL),
"a" (num),
"d" (a[0]),
"c" (a[1]),
"b" (a[2]),
"D" (a[3]),
"S" (a[4])
: "cc", "memory");
return ret;
}
int
sys_exit(int error_code) {
return syscall(SYS_exit, error_code);
}
int
sys_fork(void) {
return syscall(SYS_fork);
}
int
sys_wait(int pid, int *store) {
return syscall(SYS_wait, pid, store);
}
int
sys_yield(void) {
return syscall(SYS_yield);
}
int
sys_kill(int pid) {
return syscall(SYS_kill, pid);
}
int
sys_getpid(void) {
return syscall(SYS_getpid);
}
int
sys_putc(int c) {
return syscall(SYS_putc, c);
}
int
sys_pgdir(void) {
return syscall(SYS_pgdir);
}
int
sys_gettime(void) {
return syscall(SYS_gettime);
}
void
sys_lab6_set_priority(uint32_t priority)
{
syscall(SYS_lab6_set_priority, priority);
}

View File

@@ -0,0 +1,17 @@
#ifndef __USER_LIBS_SYSCALL_H__
#define __USER_LIBS_SYSCALL_H__
int sys_exit(int error_code);
int sys_fork(void);
int sys_wait(int pid, int *store);
int sys_yield(void);
int sys_kill(int pid);
int sys_getpid(void);
int sys_putc(int c);
int sys_pgdir(void);
int sys_gettime(void);
/* FOR LAB6 ONLY */
void sys_lab6_set_priority(uint32_t priority);
#endif /* !__USER_LIBS_SYSCALL_H__ */

View File

@@ -0,0 +1,58 @@
#include <defs.h>
#include <syscall.h>
#include <stdio.h>
#include <ulib.h>
void
exit(int error_code) {
sys_exit(error_code);
cprintf("BUG: exit failed.\n");
while (1);
}
int
fork(void) {
return sys_fork();
}
int
wait(void) {
return sys_wait(0, NULL);
}
int
waitpid(int pid, int *store) {
return sys_wait(pid, store);
}
void
yield(void) {
sys_yield();
}
int
kill(int pid) {
return sys_kill(pid);
}
int
getpid(void) {
return sys_getpid();
}
//print_pgdir - print the PDT&PT
void
print_pgdir(void) {
sys_pgdir();
}
unsigned int
gettime_msec(void) {
return (unsigned int)sys_gettime();
}
void
lab6_set_priority(uint32_t priority)
{
sys_lab6_set_priority(priority);
}

View File

@@ -0,0 +1,38 @@
#ifndef __USER_LIBS_ULIB_H__
#define __USER_LIBS_ULIB_H__
#include <defs.h>
void __warn(const char *file, int line, const char *fmt, ...);
void __noreturn __panic(const char *file, int line, const char *fmt, ...);
#define warn(...) \
__warn(__FILE__, __LINE__, __VA_ARGS__)
#define panic(...) \
__panic(__FILE__, __LINE__, __VA_ARGS__)
#define assert(x) \
do { \
if (!(x)) { \
panic("assertion failed: %s", #x); \
} \
} while (0)
// static_assert(x) will generate a compile-time error if 'x' is false.
#define static_assert(x) \
switch (x) { case 0: case (x): ; }
void __noreturn exit(int error_code);
int fork(void);
int wait(void);
int waitpid(int pid, int *store);
void yield(void);
int kill(int pid);
int getpid(void);
void print_pgdir(void);
unsigned int gettime_msec(void);
void lab6_set_priority(uint32_t priority);
#endif /* !__USER_LIBS_ULIB_H__ */

View File

@@ -0,0 +1,10 @@
#include <ulib.h>
int main(void);
void
umain(void) {
int ret = main();
exit(ret);
}

View File

@@ -0,0 +1,84 @@
#include <ulib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MATSIZE 10
static int mata[MATSIZE][MATSIZE];
static int matb[MATSIZE][MATSIZE];
static int matc[MATSIZE][MATSIZE];
void
work(unsigned int times) {
int i, j, k, size = MATSIZE;
for (i = 0; i < size; i ++) {
for (j = 0; j < size; j ++) {
mata[i][j] = matb[i][j] = 1;
}
}
yield();
cprintf("pid %d is running (%d times)!.\n", getpid(), times);
while (times -- > 0) {
for (i = 0; i < size; i ++) {
for (j = 0; j < size; j ++) {
matc[i][j] = 0;
for (k = 0; k < size; k ++) {
matc[i][j] += mata[i][k] * matb[k][j];
}
}
}
for (i = 0; i < size; i ++) {
for (j = 0; j < size; j ++) {
mata[i][j] = matb[i][j] = matc[i][j];
}
}
}
cprintf("pid %d done!.\n", getpid());
exit(0);
}
const int total = 21;
int
main(void) {
int pids[total];
memset(pids, 0, sizeof(pids));
int i;
for (i = 0; i < total; i ++) {
if ((pids[i] = fork()) == 0) {
srand(i * i);
int times = (((unsigned int)rand()) % total);
times = (times * times + 10) * 100;
work(times);
}
if (pids[i] < 0) {
goto failed;
}
}
cprintf("fork ok.\n");
for (i = 0; i < total; i ++) {
if (wait() != 0) {
cprintf("wait failed.\n");
goto failed;
}
}
cprintf("matrix pass.\n");
return 0;
failed:
for (i = 0; i < total; i ++) {
if (pids[i] > 0) {
kill(pids[i]);
}
}
panic("FAIL: T.T\n");
}

View File

@@ -0,0 +1,11 @@
#include <stdio.h>
#include <ulib.h>
int
main(void) {
cprintf("I am %d, print pgdir.\n", getpid());
print_pgdir();
cprintf("pgdir pass.\n");
return 0;
}

View File

@@ -0,0 +1,77 @@
#include <ulib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TOTAL 5
/* to get enough accuracy, MAX_TIME (the running time of each process) should >1000 mseconds. */
#define MAX_TIME 2000
unsigned int acc[TOTAL];
int status[TOTAL];
int pids[TOTAL];
static void
spin_delay(void)
{
int i;
volatile int j;
for (i = 0; i != 200; ++ i)
{
j = !j;
}
}
int
main(void) {
int i,time;
memset(pids, 0, sizeof(pids));
lab6_set_priority(TOTAL + 1);
for (i = 0; i < TOTAL; i ++) {
acc[i]=0;
if ((pids[i] = fork()) == 0) {
lab6_set_priority(i + 1);
acc[i] = 0;
while (1) {
spin_delay();
++ acc[i];
if(acc[i]%4000==0) {
if((time=gettime_msec())>MAX_TIME) {
cprintf("child pid %d, acc %d, time %d\n",getpid(),acc[i],time);
exit(acc[i]);
}
}
}
}
if (pids[i] < 0) {
goto failed;
}
}
cprintf("main: fork ok,now need to wait pids.\n");
for (i = 0; i < TOTAL; i ++) {
status[i]=0;
waitpid(pids[i],&status[i]);
cprintf("main: pid %d, acc %d, time %d\n",pids[i],status[i],gettime_msec());
}
cprintf("main: wait pids over\n");
cprintf("stride sched correct result:");
for (i = 0; i < TOTAL; i ++)
{
cprintf(" %d", (status[i] * 2 / status[0] + 1) / 2);
}
cprintf("\n");
return 0;
failed:
for (i = 0; i < TOTAL; i ++) {
if (pids[i] > 0) {
kill(pids[i]);
}
}
panic("FAIL: T.T\n");
}

View File

@@ -0,0 +1,9 @@
#include <stdio.h>
#include <ulib.h>
int
main(void) {
asm volatile("int $14");
panic("FAIL: T.T\n");
}

29
labcodes/lab6/user/spin.c Normal file
View File

@@ -0,0 +1,29 @@
#include <stdio.h>
#include <ulib.h>
int
main(void) {
int pid, ret;
cprintf("I am the parent. Forking the child...\n");
if ((pid = fork()) == 0) {
cprintf("I am the child. spinning ...\n");
while (1);
}
cprintf("I am the parent. Running the child...\n");
yield();
yield();
yield();
cprintf("I am the parent. Killing the child...\n");
assert((ret = kill(pid)) == 0);
cprintf("kill returns %d\n", ret);
assert((ret = waitpid(pid, NULL)) == 0);
cprintf("wait returns %d\n", ret);
cprintf("spin may pass.\n");
return 0;
}

View File

@@ -0,0 +1,33 @@
#include <stdio.h>
#include <ulib.h>
#define ARRAYSIZE (1024*1024)
uint32_t bigarray[ARRAYSIZE];
int
main(void) {
cprintf("Making sure bss works right...\n");
int i;
for (i = 0; i < ARRAYSIZE; i ++) {
if (bigarray[i] != 0) {
panic("bigarray[%d] isn't cleared!\n", i);
}
}
for (i = 0; i < ARRAYSIZE; i ++) {
bigarray[i] = i;
}
for (i = 0; i < ARRAYSIZE; i ++) {
if (bigarray[i] != i) {
panic("bigarray[%d] didn't hold its value!\n", i);
}
}
cprintf("Yes, good. Now doing a wild write off the end...\n");
cprintf("testbss may pass.\n");
bigarray[ARRAYSIZE + 1024] = 0;
asm volatile ("int $0x14");
panic("FAIL: T.T\n");
}

View File

@@ -0,0 +1,59 @@
#include <ulib.h>
#include <stdio.h>
void
do_yield(void) {
yield();
yield();
yield();
yield();
yield();
yield();
}
int parent, pid1, pid2;
void
loop(void) {
cprintf("child 1.\n");
while (1);
}
void
work(void) {
cprintf("child 2.\n");
do_yield();
if (kill(parent) == 0) {
cprintf("kill parent ok.\n");
do_yield();
if (kill(pid1) == 0) {
cprintf("kill child1 ok.\n");
exit(0);
}
}
exit(-1);
}
int
main(void) {
parent = getpid();
if ((pid1 = fork()) == 0) {
loop();
}
assert(pid1 > 0);
if ((pid2 = fork()) == 0) {
work();
}
if (pid2 > 0) {
cprintf("wait child 1.\n");
waitpid(pid1, NULL);
panic("waitpid %d returns\n", pid1);
}
else {
kill(pid1);
}
panic("FAIL: T.T\n");
}

View File

@@ -0,0 +1,16 @@
#include <ulib.h>
#include <stdio.h>
int
main(void) {
int i;
cprintf("Hello, I am process %d.\n", getpid());
for (i = 0; i < 5; i ++) {
yield();
cprintf("Back in process %d, iteration %d.\n", getpid(), i);
}
cprintf("All done in process %d.\n", getpid());
cprintf("yield pass.\n");
return 0;
}