/
schedos-1.c
54 lines (44 loc) · 1.26 KB
/
schedos-1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "schedos-app.h"
#include "x86sync.h"
/*****************************************************************************
* schedos-1
*
* This tiny application prints red "1"s to the console.
* It yields the CPU to the kernel after each "1" using the sys_yield()
* system call. This lets the kernel (schedos-kern.c) pick another
* application to run, if it wants.
*
* The other schedos-* processes simply #include this file after defining
* PRINTCHAR appropriately.
*
*****************************************************************************/
#ifndef PRINTCHAR
#define PRINTCHAR ('1' | 0x0C00)
#endif
// UNCOMMENT THE NEXT LINE TO USE EXERCISE 8 CODE INSTEAD OF EXERCISE 6
// #define __EXERCISE_8__
void
start(void)
{
int i;
for (i = 0; i < RUNCOUNT; i++) {
#ifndef __EXERCISE_8__
/* EXERCISE 6: use a system call to print characters *****************/
sys_print(PRINTCHAR);
#endif
#ifdef __EXERCISE_8__
/* EXERCISE 8: use a lock to prevent race conditions *****************/
// spinlock until we obtain write lock
while (atomic_swap(&spinlock, 1) != 0)
continue;
// write
*cursorpos++ = PRINTCHAR;
// release write lock
atomic_swap(&spinlock, 0);
#endif
sys_yield();
}
// Yield forever.
while (1)
sys_exit(0);
}