comparison kernel/process.c3 @ 407:9eb1fc6aad6c

Minor improvements
author Windel Bouwman
date Fri, 20 Feb 2015 15:47:54 +0100
parents 0fb6633c42f6
children ad6be5454067
comparison
equal deleted inserted replaced
406:b1daa462ee17 407:9eb1fc6aad6c
5 5
6 // process type definition: 6 // process type definition:
7 type struct { 7 type struct {
8 int id; 8 int id;
9 int status; 9 int status;
10 process_t* next; // For linked list.. 10 process_t* next;
11 process_t* prev;
11 } process_t; 12 } process_t;
12 13
13 // Or, use this list structure: 14 var process_t* first_process;
14 // List<process_t> procs; 15 var process_t* last_process;
15
16 // init is the root of all processes:
17 var process_t* root_process;
18
19 var int next_pid; 16 var int next_pid;
20 17
21 function void init() 18 function void init()
22 { 19 {
23 next_pid = 0; 20 next_pid = 0;
24 root_process = create(); 21 first_process = 0;
22 last_process = 0;
25 } 23 }
26 24
27 /* 25 /*
28 Create a new process. 26 Create a new process.
29 */ 27 */
30 function process_t* create() 28 function process_t* create()
31 { 29 {
32 var process_t* p; 30 var process_t* p;
33 31
34 p = cast<process_t*>(memory.alloc(sizeof(process_t))); 32 p = memory.alloc(sizeof(process_t));
35 p->id = next_pid; 33 p->id = next_pid;
36 p->status = 0; // Ready! 34 p->status = 0; // Ready!
37 p->next = cast<process_t*>(0); 35 p->next = 0;
36 p->prev = 0;
38 37
39 // Increment PID: 38 // Increment PID:
40 next_pid = next_pid + 1; 39 next_pid = next_pid + 1;
41 40
41 return p;
42 }
43
44 function void enqueue(process_t* p)
45 {
42 // Store it in the list: 46 // Store it in the list:
43 if (root_process == cast<process_t*>(0)) 47 if (first_process == cast<process_t*>(0))
44 { 48 {
45 root_process = p; 49 first_process = p;
50 last_process = p;
46 } 51 }
47 else 52 else
48 { 53 {
49 var process_t* parent; 54 // Update pointers:
50 parent = root_process; 55 last_process->next = p;
51 while (parent->next != cast<process_t*>(0)) 56 p->prev = last_process;
52 { 57 last_process = p;
53 parent = parent->next;
54 }
55
56 parent->next = p;
57 } 58 }
58
59 return p;
60 } 59 }
61 60
62 61 function void kill(process_t* p)
63 function void Kill(process_t* p)
64 { 62 {
65 // clean memory 63 // clean memory
66 } 64 }
67 65
68 function process_t* byId(int id) 66 function process_t* byId(int id)