-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpos-app.c
86 lines (70 loc) · 1.95 KB
/
mpos-app.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "mpos-app.h"
#include "lib.h"
/*****************************************************************************
* mpos-app
*
* This application simply starts a child process and then waits for it
* to exit. Both processes print messages to the screen.
*
*****************************************************************************/
#define EXTRA6
void run_child(void);
void newThread(void);//XIA: new thread
void
start(void)
{
volatile int checker = 0; /* This variable checks that you correctly
gave the child process a new stack. */
pid_t p;
int status;
app_printf("About to start a new process...\n");
p = sys_fork();
if (p == 0)
run_child();
else if (p > 0) {
app_printf("Main process %d!\n", sys_getpid());
do {
status = sys_wait(p);
app_printf("W");
} while (status == WAIT_TRYAGAIN);
//XIA: new thread
#ifdef EXTRA6
//app_printf("new thread\n");
pid_t new;
//app_printf("newThread address: %d",&newThread);
new = sys_newthread(&newThread);
#endif
//status = sys_wait(p);
app_printf("Child %d exited with status %d!\n", p, status);
// Check whether the child process corrupted our stack.
// (This check doesn't find all errors, but it helps.)
if (checker != 0) {
app_printf("Error: stack collision!\n");
sys_exit(1);
} else
sys_exit(0);
} else {
app_printf("Error!\n");
sys_exit(1);
}
}
void
run_child(void)
{
int i;
volatile int checker = 1; /* This variable checks that you correctly
gave this process a new stack.
If the parent's 'checker' changed value
after the child ran, there's a problem! */
app_printf("Child process %d!\n", sys_getpid());
// Yield a couple times to help people test Exercise 3
for (i = 0; i < 20; i++)
sys_yield();
sys_exit(1000);
}
// XIA: new thread
void newThread()
{
app_printf("newThread pid = %d\n",sys_getpid());
sys_yield();
}