annotate kernel/src/process.c3 @ 393:6ae782a085e0

Added init program
author Windel Bouwman
date Sat, 17 May 2014 21:17:40 +0200
parents 2ec730e45ea1
children
rev   line source
283
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
1 module process;
300
Windel Bouwman
parents: 296
diff changeset
2
283
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
3 import memory;
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
4 import kernel;
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
5
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
6 // process type definition:
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 295
diff changeset
7 type struct {
283
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
8 int id;
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 283
diff changeset
9 int status;
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
10 process_t* next; // For linked list..
283
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
11 } process_t;
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
12
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 283
diff changeset
13 // Or, use this list structure:
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 295
diff changeset
14 // List<process_t> procs;
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 283
diff changeset
15
283
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
16 // init is the root of all processes:
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
17 var process_t* root_process;
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
18
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
19 var int next_pid;
283
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
20
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 295
diff changeset
21 function void init()
283
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
22 {
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
23 next_pid = 0;
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
24 root_process = cast<process_t*>(0);
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
25 // init_pid = Create();
283
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
26 }
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
27
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
28 /*
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
29 Create a new process.
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
30 */
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 295
diff changeset
31 function process_t* Create()
283
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
32 {
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
33 var process_t* p;
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
34
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
35 p = cast<process_t*>(memory.alloc(sizeof(process_t)));
283
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
36 p->id = next_pid;
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
37 p->status = 0; // Ready!
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
38 p->next = cast<process_t*>(0);
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
39
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
40 // Increment PID:
300
Windel Bouwman
parents: 296
diff changeset
41 next_pid = next_pid + 1;
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
42
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
43 // Store it in the list:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
44 if (root_process == cast<process_t*>(0))
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
45 {
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
46 root_process = p;
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
47 }
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
48 else
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
49 {
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
50 var process_t* parent;
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
51 parent = root_process;
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
52 while (parent->next != cast<process_t*>(0))
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
53 {
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
54 parent = parent->next;
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
55 }
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
56
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
57 parent->next = p;
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
58 }
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 359
diff changeset
59
283
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
60 return p;
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
61 }
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
62
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
63
300
Windel Bouwman
parents: 296
diff changeset
64 function void Kill(process_t* p)
283
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
65 {
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
66 // clean memory
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
67 }
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
68
300
Windel Bouwman
parents: 296
diff changeset
69 function process_t* byId(int id)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 283
diff changeset
70 {
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 283
diff changeset
71 // Perform lookup
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 283
diff changeset
72 return 0;
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 283
diff changeset
73 }
283
c9781c73e7e2 Added first kernel files
Windel Bouwman
parents:
diff changeset
74