-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
97 lines (71 loc) · 2.19 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include "threadlib.h"
#define TESTING /* once you understand the code undefine this (just comment)*/
#ifdef TESTING
/* few threads to play around with*/
void thread1(void);
void thread2(void);
void thread1(void) {
printf("Thread 1\n");
int i = 1;
for(;i<100;) {
printf("%s: count = %d\n",__func__,i++);
//yield();
}
//delete_thread();
//assert(0);
}
void thread2(void) {
printf("Thread 2\n");
int i = -100;
for(;i<0;) {
printf("%s: count = %d\n",__func__,i++);
//yield();
}
//delete_thread();
//assert(0);
}
int main(void) {
printf("\n");
printf("\nCreating thread 1\n");
printf("%s : thread1 = %p\n", __func__, thread1);
assert(!create_thread(thread1));
printf("\nCreating thread 2\n");
printf("%s : thread2 = %p\n", __func__, thread1);
assert(!create_thread(thread2));
//stop_main(); /* give up the CPU */
//assert(0); /* you should not come back */
return 0;
}
#else /* real code this is what we'll test/mark */
int count = 1;
#define MAX 10 /* how may threads to create */
#define COUNT(x) x*100000
void simple_thread(void); /* want to create many instance of this */
void simple_thread(void) {
int i, myID, sleep;
myID = count;
count ++; /* for the next thread
* It is shared, do I need to lock??? A lock is not necessary. Yielding is collaborative. So cant be pre-emptied at the middle */
for(i = myID; i < MAX; i++) {
/* print something, sleep for a while and yield */
printf("I'm %s %d: Count=%d\n",__func__,myID,i);
for(sleep = -COUNT(myID); sleep < COUNT(myID); sleep++);
yield();
}
delete_thread(); /* die */
assert(0); /* I should not be running */
}
#define NO_THREADS 10 /* FixMe: this should work for any number of threads */
int main(void) {
int i;
for(i = 0; i < NO_THREADS; i++) {
//assert(!create_thread(simple_thread));
}
stop_main(); /* give up the CPU */
//assert(0); /* you should not come back */
return 0;
}
#endif