MentOS  0.8.0
The Mentoring Operating System
Macros | Functions | Variables
scheduler.c File Reference

Scheduler structures and functions. More...

Macros

#define __DEBUG_HEADER__   "[SCHED ]"
 Change header.
 
#define __DEBUG_LEVEL__   LOGLEVEL_NOTICE
 Set log level.
 
#define RETURN_PROCESS_ATTR_OR_EPERM(attr)
 Returns the attributes of the runnign process. More...
 
#define FAIL_ON_INV_ID(id)    if (id < 0) { return -EINVAL; }
 Checks the given ID.
 
#define FAIL_ON_INV_ID_OR_PROC(id)
 Checks the ID, and if there is a running process. More...
 
#define IF_PRIVILEGED_SET_ALL_AND_RETURN(attr)
 If the process is ROOT, set the attribute and return 0. More...
 
#define IF_RESET_SET_AND_RETURN(attr)
 Checks the attributes, resets them, and returns 0. More...
 
#define SET_IF_PRIVILEGED_OR_FAIL(attr)
 If the process is ROOT set the attribute, otherwise return failure. More...
 

Functions

void enter_userspace (uintptr_t location, uintptr_t stack)
 Assembly function setting the kernel stack to jump into location in Ring 3 mode (USER mode). More...
 
void scheduler_initialize (void)
 Initialize the scheduler.
 
uint32_t scheduler_getpid (void)
 Returns a non-decreasing unique process id. More...
 
task_structscheduler_get_current_process (void)
 Returns the pointer to the current active process. More...
 
time_t scheduler_get_maximum_vruntime (void)
 Returns the maximum vruntime of all the processes in running state. More...
 
size_t scheduler_get_active_processes (void)
 Returns the number of active processes. More...
 
task_structscheduler_get_running_process (pid_t pid)
 Returns a pointer to the process with the given pid. More...
 
void scheduler_enqueue_task (task_struct *process)
 Activate the given process. More...
 
void scheduler_dequeue_task (task_struct *process)
 Removes the given process from the queue. More...
 
void scheduler_run (pt_regs *f)
 The RR implementation of the scheduler. More...
 
void scheduler_store_context (pt_regs *f, task_struct *process)
 Values from pt_regs to task_struct process. More...
 
void scheduler_restore_context (task_struct *process, pt_regs *f)
 Values from task_struct process to pt_regs. More...
 
void scheduler_enter_user_jmp (uintptr_t location, uintptr_t stack)
 Switch CPU to user mode and start running that given process. More...
 
static int try_to_wake_up (task_struct *process, unsigned mode, int sync)
 Awakens a sleeping process. More...
 
int default_wake_function (wait_queue_entry_t *wait, unsigned mode, int sync)
 The default wake function, a wrapper for try_to_wake_up. More...
 
wait_queue_entry_tsleep_on (wait_queue_head_t *wq)
 Sets the state of the current process to TASK_UNINTERRUPTIBLE and inserts it into the specified wait queue. More...
 
int is_orphaned_pgrp (pid_t pgid)
 Returns 1 if the given group is orphaned, the session leader of the group is no longer alive. More...
 
pid_t sys_getpid (void)
 Returns the process ID (PID) of the calling process. More...
 
pid_t sys_getsid (pid_t pid)
 Return session id of the given process. If pid == 0 return the SID of the calling process If pid != 0 return the SID corresponding to the process having identifier == pid. More...
 
pid_t sys_setsid (void)
 creates a new session if the calling process is not a process group leader. The calling process is the leader of the new session (i.e., its session ID is made the same as its process ID). The calling process also becomes the process group leader of a new process group in the session (i.e., its process group ID is made the same as its process ID). More...
 
pid_t sys_getpgid (pid_t pid)
 returns the Process Group ID (PGID) of the process specified by pid. If pid is zero, the process ID of the calling process is used. More...
 
int sys_setpgid (pid_t pid, pid_t pgid)
 Sets the Process Group ID (PGID) of the process specified by pid. If pid is zero, the process ID of the calling process is used. More...
 
uid_t sys_getuid (void)
 Returns the real User ID (UID) of the calling process. More...
 
uid_t sys_geteuid (void)
 Returns the effective User ID (UID) of the calling process. More...
 
gid_t sys_getgid (void)
 returns the real group ID of the calling process. More...
 
gid_t sys_getegid (void)
 returns the effective group ID of the calling process. More...
 
int sys_setuid (uid_t uid)
 Tries to set the User ID (UID) of the calling process. More...
 
int sys_setgid (gid_t gid)
 sets the group ID of the calling process. More...
 
int sys_setreuid (uid_t ruid, uid_t euid)
 Set the real and effective User ID (UID) of the calling process. More...
 
int sys_setregid (gid_t rgid, gid_t egid)
 sets the real and effective group ID of the calling process. More...
 
pid_t sys_getppid (void)
 Returns the parent process ID (PPID) of the calling process. More...
 
int sys_nice (int increment)
 Adds the increment to the priority value of the task. More...
 
pid_t sys_waitpid (pid_t pid, int *status, int options)
 Suspends execution of the calling thread until a child specified by pid argument has changed state. More...
 
void do_exit (int exit_code)
 Exit the current process with status. More...
 
void sys_exit (int exit_code)
 
int sys_sched_setparam (pid_t pid, const sched_param_t *param)
 Set new scheduling settings for the given process. More...
 
int sys_sched_getparam (pid_t pid, sched_param_t *param)
 Gets the scheduling settings for the given process. More...
 
static int __response_time_analysis (void)
 Performs the response time analysis for the current list of periodic processes. More...
 
static double __compute_utilization_factor (void)
 Computes the total utilization factor. More...
 
int sys_waitperiod (void)
 Puts the process on wait until its next period starts. More...
 

Variables

runqueue_t runqueue
 The list of processes.
 

Detailed Description

Scheduler structures and functions.

Macro Definition Documentation

◆ FAIL_ON_INV_ID_OR_PROC

#define FAIL_ON_INV_ID_OR_PROC (   id)
Value:
if (!runqueue.curr) { return -EPERM; }
#define EPERM
Operation not permitted.
Definition: errno.h:13
#define FAIL_ON_INV_ID(id)
Checks the given ID.
Definition: scheduler.c:403
runqueue_t runqueue
The list of processes.
Definition: scheduler.c:31
task_struct * curr
The current running process.
Definition: scheduler.h:21

Checks the ID, and if there is a running process.

◆ IF_PRIVILEGED_SET_ALL_AND_RETURN

#define IF_PRIVILEGED_SET_ALL_AND_RETURN (   attr)
Value:
if (runqueue.curr->uid == 0) { \
runqueue.curr->r##attr = runqueue.curr->attr = attr; \
return 0; \
}
uid_t uid
The effective User ID (UID) of the process.
Definition: process.h:96

If the process is ROOT, set the attribute and return 0.

◆ IF_RESET_SET_AND_RETURN

#define IF_RESET_SET_AND_RETURN (   attr)
Value:
if (runqueue.curr->r##attr == attr) { \
runqueue.curr->attr = attr; \
return 0; \
}

Checks the attributes, resets them, and returns 0.

◆ RETURN_PROCESS_ATTR_OR_EPERM

#define RETURN_PROCESS_ATTR_OR_EPERM (   attr)
Value:
if (runqueue.curr) { return runqueue.curr->attr; } \
return -EPERM;

Returns the attributes of the runnign process.

◆ SET_IF_PRIVILEGED_OR_FAIL

#define SET_IF_PRIVILEGED_OR_FAIL (   attr)
Value:
if (runqueue.curr->uid == 0) { \
runqueue.curr->attr = attr; \
} else { \
return -EPERM; \
}

If the process is ROOT set the attribute, otherwise return failure.

Function Documentation

◆ __compute_utilization_factor()

static double __compute_utilization_factor ( void  )
inlinestatic

Computes the total utilization factor.

Returns
the utilization factor.

◆ __response_time_analysis()

static int __response_time_analysis ( void  )
static

Performs the response time analysis for the current list of periodic processes.

Returns
1 if scheduling periodic processes is feasible, 0 otherwise.

◆ default_wake_function()

int default_wake_function ( wait_queue_entry_t wait,
unsigned  mode,
int  sync 
)

The default wake function, a wrapper for try_to_wake_up.

Parameters
waitThe pointer to the wait queue.
modeThe type of wait (TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE).
syncSpecifies if the wakeup should be synchronous.
Returns
1 on success, 0 on failure.

◆ do_exit()

void do_exit ( int  status)

Exit the current process with status.

Parameters
statusThe exit status of the current process

◆ enter_userspace()

void enter_userspace ( uintptr_t  location,
uintptr_t  stack 
)

Assembly function setting the kernel stack to jump into location in Ring 3 mode (USER mode).

Parameters
locationThe location where to jump.
stackThe stack to use.

◆ is_orphaned_pgrp()

int is_orphaned_pgrp ( pid_t  gid)

Returns 1 if the given group is orphaned, the session leader of the group is no longer alive.

Parameters
gidID of the group
Returns
1 if the group is orphan, 0 otherwise.

◆ scheduler_dequeue_task()

void scheduler_dequeue_task ( task_struct process)

Removes the given process from the queue.

Parameters
processProcess that has to be activated.

◆ scheduler_enqueue_task()

void scheduler_enqueue_task ( task_struct process)

Activate the given process.

Parameters
processProcess that has to be activated.

◆ scheduler_enter_user_jmp()

void scheduler_enter_user_jmp ( uintptr_t  location,
uintptr_t  stack 
)

Switch CPU to user mode and start running that given process.

Parameters
locationThe instruction pointer of the process we are starting.
stackAddress of the stack of that process.

◆ scheduler_get_active_processes()

size_t scheduler_get_active_processes ( void  )

Returns the number of active processes.

Returns
Number of processes.

◆ scheduler_get_current_process()

task_struct* scheduler_get_current_process ( void  )

Returns the pointer to the current active process.

Returns
Pointer to the current process.

◆ scheduler_get_maximum_vruntime()

time_t scheduler_get_maximum_vruntime ( void  )

Returns the maximum vruntime of all the processes in running state.

Returns
A maximum vruntime value.

◆ scheduler_get_running_process()

task_struct* scheduler_get_running_process ( pid_t  pid)

Returns a pointer to the process with the given pid.

Parameters
pidThe pid of the process we are looking for.
Returns
Pointer to the process, or NULL if we cannot find it.

◆ scheduler_getpid()

uint32_t scheduler_getpid ( void  )

Returns a non-decreasing unique process id.

Returns
Process identifier (PID).

The current unused PID.

◆ scheduler_restore_context()

void scheduler_restore_context ( task_struct process,
pt_regs f 
)

Values from task_struct process to pt_regs.

Parameters
processThe process for which we are restoring the registers in CPU .
fThe set of registers we are restoring.

◆ scheduler_run()

void scheduler_run ( pt_regs f)

The RR implementation of the scheduler.

Parameters
fThe context of the process.

◆ scheduler_store_context()

void scheduler_store_context ( pt_regs f,
task_struct process 
)

Values from pt_regs to task_struct process.

Parameters
fThe set of registers we are saving.
processThe process for which we are saving the CPU registers status.

◆ sleep_on()

Sets the state of the current process to TASK_UNINTERRUPTIBLE and inserts it into the specified wait queue.

Parameters
wqWaitqueue where to sleep.
Returns
Pointer to the entry inside the wq representing the sleeping process.

◆ sys_exit()

void sys_exit ( int  exit_code)

The exit() function causes normal process termination.

Parameters
exit_codeThe exit code.

◆ sys_getegid()

gid_t sys_getegid ( void  )

returns the effective group ID of the calling process.

Returns
GID of the current process

◆ sys_geteuid()

uid_t sys_geteuid ( void  )

Returns the effective User ID (UID) of the calling process.

Returns
The User ID.

◆ sys_getgid()

gid_t sys_getgid ( void  )

returns the real group ID of the calling process.

Returns
GID of the current process

◆ sys_getpgid()

pid_t sys_getpgid ( pid_t  pid)

returns the Process Group ID (PGID) of the process specified by pid. If pid is zero, the process ID of the calling process is used.

Parameters
pidprocess of which we want to know the PGID.
Returns
the PGID of the specified process.

◆ sys_getpid()

pid_t sys_getpid ( void  )

Returns the process ID (PID) of the calling process.

Returns
The process ID.

◆ sys_getppid()

pid_t sys_getppid ( void  )

Returns the parent process ID (PPID) of the calling process.

Returns
The parent process ID.

◆ sys_getsid()

pid_t sys_getsid ( pid_t  pid)

Return session id of the given process. If pid == 0 return the SID of the calling process If pid != 0 return the SID corresponding to the process having identifier == pid.

Parameters
pidprocess identifier from wich we want the SID
Returns
On success return SID of the session Otherwise return -1 with errno set on: EPERM or ESRCH

◆ sys_getuid()

uid_t sys_getuid ( void  )

Returns the real User ID (UID) of the calling process.

Returns
The User ID.

◆ sys_nice()

int sys_nice ( int  increment)

Adds the increment to the priority value of the task.

Parameters
incrementThe modifier to apply to the nice value.
Returns
The new nice value.

◆ sys_sched_getparam()

int sys_sched_getparam ( pid_t  pid,
sched_param_t param 
)

Gets the scheduling settings for the given process.

Parameters
pidID of the process we are manipulating.
paramWhere we store the parameters.
Returns
1 on success, -1 on error.

◆ sys_sched_setparam()

int sys_sched_setparam ( pid_t  pid,
const sched_param_t param 
)

Set new scheduling settings for the given process.

Parameters
pidID of the process we are manipulating.
paramNew parameters.
Returns
1 on success, -1 on error.

◆ sys_setgid()

int sys_setgid ( gid_t  gid)

sets the group ID of the calling process.

Parameters
gidgroup id
Returns
On success, zero is returned. Otherwise returns -1 with errno set to :EINVAL or EPERM.

◆ sys_setpgid()

int sys_setpgid ( pid_t  pid,
pid_t  pgid 
)

Sets the Process Group ID (PGID) of the process specified by pid. If pid is zero, the process ID of the calling process is used.

Parameters
pidprocess of which we want to set the PGID.
pgidthe PGID we want to set.
Returns
returns zero. On error, -1 is returned, and errno is set appropriately.

◆ sys_setregid()

int sys_setregid ( gid_t  rgid,
gid_t  egid 
)

sets the real and effective group ID of the calling process.

Parameters
rgidreal group id
egideffective group id
Returns
On success, zero is returned. Otherwise returns -1 with errno set to EPERM.

◆ sys_setreuid()

int sys_setreuid ( uid_t  ruid,
uid_t  euid 
)

Set the real and effective User ID (UID) of the calling process.

Parameters
ruidthe new real User ID.
euidthe new effective User ID.
Returns
On success, zero is returned. Otherwise returns -1 with errno set to EPERM.

◆ sys_setsid()

pid_t sys_setsid ( void  )

creates a new session if the calling process is not a process group leader. The calling process is the leader of the new session (i.e., its session ID is made the same as its process ID). The calling process also becomes the process group leader of a new process group in the session (i.e., its process group ID is made the same as its process ID).

Returns
On success return SID of the session just created Otherwise return -1 with errno : EPERM

◆ sys_setuid()

int sys_setuid ( uid_t  uid)

Tries to set the User ID (UID) of the calling process.

Parameters
uidthe new User ID.
Returns
On success, zero is returned. Otherwise returns -1 with errno set to :EINVAL or EPERM.

◆ sys_waitperiod()

int sys_waitperiod ( void  )

Puts the process on wait until its next period starts.

Returns
0 on success, a negative value on failure.

◆ sys_waitpid()

pid_t sys_waitpid ( pid_t  pid,
int *  status,
int  options 
)

Suspends execution of the calling thread until a child specified by pid argument has changed state.

Parameters
pidThe pid to wait.
statusIf not NULL, store status information here.
optionsDetermines the wait behaviour.
Returns
on success, returns the process ID of the terminated child; on error, -1 is returned.

◆ try_to_wake_up()

static int try_to_wake_up ( task_struct process,
unsigned  mode,
int  sync 
)
inlinestatic

Awakens a sleeping process.

Parameters
processThe process that should be awakened
modeThe type of wait (TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE).
syncSpecifies if the wakeup should be synchronous.
Returns
1 on success, 0 on failure.