-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test01.cpp
56 lines (48 loc) · 1.17 KB
/
Test01.cpp
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
/*#include "Thread.h"
#include "Semaphor.h"
#include <STDLIB.H>
#include <IOSTREAM.H>
#include "system.h"
#include "Event.h"
int syncPrintf(const char *format, ...);
void dumbSleep(int delay);
#define true 1
#define false 0
class ForkThread : public Thread {
public:
ForkThread() : Thread(1, 1) {}
virtual void run();
virtual Thread* clone() const {
return new ForkThread();
}
~ForkThread() {
waitToComplete();
}
static volatile int failedFork;
};
volatile int ForkThread::failedFork = false;
void ForkThread::run() {
while (!failedFork) {
ID forked = fork();
if (forked < 0) {
syncPrintf("Failed to fork in thread %d!\n", getRunningId());
failedFork = true;
break;
} else if (forked == 0) {
syncPrintf("We are in child %d\n", getRunningId());
} else {
syncPrintf("Cloned thread: %d\n", forked);
dumbSleep(10000);
}
}
waitForForkChildren();
}
void tick() {}
int userMain(int argc, char* argv[]) {
(void) argc;
(void) argv;
ForkThread t;
t.start();
return 0;
}
*/