Code for managing processes. More...
Files | |
| file | fork.c |
| Implementation of fork-related functions. | |
| file | process.c |
| Implementation of functions for creating and killing processes. | |
| file | scheduler.c |
| Implementation of scheduler functions. | |
| file | scheduler.h |
| Definition of armv6 scheduler helper functions. | |
| file | scheduler.h |
| Definition of armv8-a scheduler helper functions. | |
| file | fork.h |
| Definition of fork-related functions. | |
| file | process.h |
| Definition of functions for creating and killing processes. | |
| file | scheduler.h |
| Definition of scheduler structs and functions. | |
Data Structures | |
| struct | cpu_context |
| struct | task_struct |
Macros | |
| #define | THREAD_CPU_CONTEXT 0 |
| #define | THREAD_CPU_CONTEXT 0 |
| #define | TASK_RUNNING 0 |
| #define | NR_TASKS 64 |
| #define | FIRST_TASK task[0] |
| #define | LAST_TASK task[NR_TASKS-1] |
| #define | THREAD_SIZE 4096 |
| #define | INIT_TASK { /* cpu_context */ {0,0,0,0,0,0,0,0,0,0,0,0,0}, /* state etc */ 0,0,1,0 } |
Functions | |
| int | copy_process (uint64_t fn, uint64_t arg) |
| void | create_processes (size_t proc_num) |
| void | kill_processes () |
| void | ret_from_fork () |
| void | cpu_switch_to (task_struct *prev, task_struct *next) |
| void | preempt_disable () |
| void | preempt_enable () |
| void | schedule_tail () |
| void | switch_to (task_struct *next) |
| void | schedule () |
| void | timer_tick () |
Variables | |
| task_struct * | task [NR_TASKS] |
| task_struct * | current |
| int | nr_tasks |
Code for managing processes.
| #define INIT_TASK { /* cpu_context */ {0,0,0,0,0,0,0,0,0,0,0,0,0}, /* state etc */ 0,0,1,0 } |
Defines the init task's task_struct ( kernel_main()) that is run on kernel startup.
Fields:
| #define NR_TASKS 64 |
Maximum number of concurrent tasks
| #define TASK_RUNNING 0 |
Task States
| #define THREAD_CPU_CONTEXT 0 |
Offset of cpu_context in task_struct.
| #define THREAD_CPU_CONTEXT 0 |
Offset of cpu_context in task_struct.
| #define THREAD_SIZE 4096 |
Defines size of each process.
Equal to the Page size ( PAGE_SIZE).
| int copy_process | ( | uint64_t | fn, |
| uint64_t | arg | ||
| ) |
Function for creating a new process.
| fn | A function to execute in a new thread. |
| arg | An argument that need to be passed to this function. |
Allocates a new task_struct and makes it available for the scheduler.
| void cpu_switch_to | ( | task_struct * | prev, |
| task_struct * | next | ||
| ) |
Performs the context switch, by changing the registers.
| prev | Pointer to the task_struct of the task we are switching (Starting address of memory page of the new task). |
| next | Pointer to the next task_struct. |
| void create_processes | ( | size_t | proc_num | ) |
Creates processes using copy_process().
| proc_num | Number of processes to fork. |
Maximum number of processes that can be created is NR_TASKS.
| void kill_processes | ( | ) |
Kills all running processes (except init_task).
Do for each process:
init_task).task[pid] = 0).| void preempt_disable | ( | ) |
Disables preemption for current running process.
| void preempt_enable | ( | ) |
Enables preemption for current running process.
| void ret_from_fork | ( | ) |
Starting point for each new task.
PC register (cpu_context.pc) of each new task points here.
| void schedule | ( | ) |
Main scheduler function.
Wrapper for core scheduler function _schedule(). Checks whether there is a new task, that needs to preempt the current one.
Gets called:
| void schedule_tail | ( | ) |
Tail function that enables preemption for current process.
It is the last function a new task calls, after its initialization, in order for it to be rescheduled.
| void switch_to | ( | task_struct * | next | ) |
Function that switches from current to the next task.
| next | A task_struct pointer, that points to the next task. |
Calls cpu_next_to, in order to perform the necessary context switch.
| void timer_tick | ( | ) |
Calls _schedule() after a time interval.
Gets called from System Timer interrupt handler.
| task_struct* current |
Pointer that points to the currently executing task.
Holds a pointer to the init_task.
| int nr_tasks |
Number of currently running tasks.
Number of currently running tasks = 1 (init_task)
| task_struct* task[NR_TASKS] |
Array that holds all the tasks.
Appends a pointer to the init_task.