axle OS
x86_32 UNIX-like hobby OS
task.h
1 #ifndef TASK_H
2 #define TASK_H
3 
4 #include <std/std.h>
5 #include <kernel/util/paging/paging.h>
6 #include <std/array_l.h>
7 #include <kernel/util/multitasking/fd_entry.h>
8 //#include <kernel/util/multitasking/std_stream.h>
9 
10 #define KERNEL_STACK_SIZE 2048 //use 2kb kernel stack
11 #define FD_MAX 64
12 
13 typedef enum task_state {
14  RUNNABLE = 0,
15  ZOMBIE, //intermediate state after task finishes executing before being flushed from system
16  KB_WAIT,
17  PIT_WAIT,
18  MOUSE_WAIT,
19  CHILD_WAIT,
20  PIPE_FULL,
21  PIPE_EMPTY,
22 } task_state;
23 
24 typedef enum mlfq_option {
25  LOW_LATENCY = 0, //minimize latency between tasks running, tasks share a single queue
26  PRIORITIZE_INTERACTIVE, //use more queues, allowing interactive tasks to dominate
27 } mlfq_option;
28 
29 struct fd_entry;
30 typedef struct task {
31  char* name; //user-printable process name
32  int id; //PID
33  int queue; //scheduler ring this task is slotted in
34 
35  task_state state; //current process state
36  uint32_t wake_timestamp; //used if process is in PIT_WAIT state
37 
38  uint32_t begin_date;
39  uint32_t end_date;
40 
41  uint32_t relinquish_date;
42  uint32_t lifespan;
43  struct task* next;
44 
45  uint32_t esp; //stack pointer
46  uint32_t ebp; //base pointer
47  uint32_t eip; //instruction pointer
48 
49  page_directory_t* page_dir; //paging directory for this process
50 
51 
52  /*
53  * the below only exist for non-kernel tasks
54  * (such as loaded ELFs)
55  */
56 
57  //end of .bss section of current task
58  uint32_t prog_break;
59  //virtual address of .bss segment
60  uint32_t bss_loc;
61 
62  /* array of child tasks this process has spawned
63  * each time a process fork()'s,
64  * the new child is added to this array
65  * when wait() is used, it uses the tasks in this array
66  */
67  array_m* child_tasks;
68  //parent process that spawned this one
69  struct task* parent;
70 
71  //exit status of zombie task
72  //this field is undefined until task finishes executing
73  int exit_code;
74 
75  //TODO move this near task_state and make clean
76  //optional context provided with blocking reason
77  //up to user what this means
78  void* block_context;
79 
80  //file descriptor table
81  //this stores all types of file descriptors,
82  //including stdin/out/err, open files, and pipes
83  fd_entry fd_table[FD_MAX];
84 
85  //pseudo-terminal stream
86  //this field provides implementation for
87  //stdin/stdout/stderr
88  //(all of these map to the same backing stream)
89  struct std_stream* std_stream;
90 } task_t;
91 
92 //initializes tasking system
93 void tasking_install();
94 bool tasking_installed();
95 
96 void block_task(task_t* task, task_state reason);
97 void block_task_context(task_t* task, task_state reason, void* context);
98 
99 //initialize a new process structure
100 //does not add returned process to running queue
101 task_t* create_process(char* name, uint32_t eip, bool wants_stack);
102 
103 //adds task to running queue
104 void add_process(task_t* task);
105 
106 //changes running process
107 uint32_t task_switch();
108 
109 //forks current process
110 //spawns new process with different memory space
111 int fork();
112 
113 //stop executing the current process and remove it from active processes
114 void _kill();
115 
116 //kill task associated with task struct
117 void kill_task(task_t* task);
118 
119 //used whenever a system event occurs
120 //looks at blocked tasks and unblocks as necessary
121 void update_blocked_tasks();
122 
123 //returns pid of current process
124 int getpid();
125 
126 //print all active processes
127 void proc();
128 
129 //used to immediately invoke iosentinel process
130 //to wake any processes that were waiting on an i/o event that
131 //has now been recieved
132 void force_enumerate_blocked();
133 
134 //internal function to query current task holding first responder status
135 //the first responder of axle receives all keyboard, mouse events out of
136 //tasks waiting for keystrokes
137 task_t* first_responder();
138 
139 //appends current task to stack of responders,
140 //and marks current task as designated recipient of keyboard events
141 void become_first_responder();
142 
143 //relinquish first responder status
144 //process which first responder status was taken from becomes first responder
145 void resign_first_responder();
146 
147 //find task_t associated with PID 'pid'
148 //returns NULL if no task_t with given PID exists
149 task_t* task_with_pid(int pid);
150 
151 //suspend execution until child process terminates
152 int waitpid(int pid, int* status, int options);
153 int wait(int* status);
154 
155 #endif
Definition: paging.h:22
Definition: std_stream.h:6
Definition: array_m.h:14
Definition: task.h:30
Definition: fd_entry.h:10