- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask.c
More file actions
Latest commit
60 lines (50 loc) · 1.56 KB
/
Copy pathtask.c
File metadata and controls
60 lines (50 loc) · 1.56 KB
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
#include"types.h"
externvoid*kmalloc(size_tsize);
typedefstructTask {
uint32_tid;
uint32_tesp; // Stack pointer register
uint32_tebp; // Base pointer register
uint32_teip; // Instruction pointer register
structTask*next; // Pointer to next task in linked list
} Task;
staticTask*current_task=NULL;
staticTask*task_list_head=NULL;
staticuint32_tnext_pid=1;
/**
* @brief Creates a new concurrent process with its own dedicated stack space
*/
Task*create_task(void (*entry_point)(void)) {
Task*new_task= (Task*)kmalloc(sizeof(Task));
new_task->id=next_pid++;
// Allocate 16 KB stack for the new task
uint32_tstack= (uint32_t)kmalloc(16384) +16384;
// Set up initial execution registers
new_task->esp=stack;
new_task->ebp=stack;
new_task->eip= (uint32_t)entry_point;
new_task->next=NULL;
// Add to circular linked list
if (!task_list_head) {
task_list_head=new_task;
new_task->next=new_task; // Circular reference
} else {
Task*temp=task_list_head;
while (temp->next!=task_list_head) {
temp=temp->next;
}
temp->next=new_task;
new_task->next=task_list_head;
}
returnnew_task;
}
/**
* @brief Switches execution to the next process in the queue (Round-Robin)
*/
voidschedule(void) {
if (!current_task) {
current_task=task_list_head;
return;
}
// Switch to next task pointer
current_task=current_task->next;
}