Professional Documents
Culture Documents
Lab 2: Kernel I
Navigating the kernel source code & adding simple commands to the shell
Introduction
In this section we will see the kernel code, and edit it a little so we become more familiar
with OS development using C language under inu!"
Steps
1- #irst run $our inu! image using %&ware pla$er
2- 'ouble click the #OS()**+ shortcut on $our desktop
3- Open director$ part1/kern
This directory contains number of files; we are interested only in the following:
Entry.s, contains an assembl$ code responsible for s$stem initiali-ation .initiali-ing
segment registers, etc/0 before calling the main 1ernel function FOS_initialize()
init.c, contains the C code for FOS_initialize() function, this function is responsible
for running the command prompt, b$ calling the run_command_prompt() function
Command_prompt.c, contains the run_command_prompt() function, this function is
responsible for displa$ing the command prompt to the user, accepting the command and
e!ecuting it"
Command_prompt.h, the header file for command_prompt.c .contains the functions
declaration0
2he section contains the following,
34 Navigating the Command 5rompt code"
)4 6dding simple commands to the Command 5rompt"
First: Navigating the Command Prompt
Command 5rompt s responsible for the following 7obs,
'ispla$ing the command prompt to the #OS user,
accepting the user command,
e!ecuting the user command
1. Displaying the command prompt and accepting the user input:
2his is done using the run_command_prompt() function, as in figure 3"
void run_command_prompt()
...
!hile (1)
int num.er_o'_ar1uments%
char 00ar1uments%
strsplit(command_strin12 3456ES78CE2 9ar1uments2
9num.er_o'_ar1uments) %
i' (num.er_o'_ar1uments "" -)
return -%
...
Figure 2: Sp&itting the command string
'. ookup in the commands arra$ for the provided command name,
2he commands are stored in an arra$ of struct Command ob7ects"
struct Command
char 0name%
char 0description%
// return :1 to 'orce command prompt to e&it
int (0'unction_to_e&ecute)(int
num.er_o'_ar1uments2 char00 ar1uments)%
/%
Figure 3: The command structure
6s $ou see in figure :, each command ob7ect holds the following,
name,
description; te!t shown to user,
'unction_to_e&ecute; pointer to a function in command_prompt.c file that
contains the command code"
Initiall$ there are onl$ two commands that are stored in the commands<= arra$, as
shown in figure ;,
3" he&p
)" "erne&_info
struct Command commands<= "
int i%
'or (i " -% i , )*?_OF_CO??8)>S% i@@)
cprint'(#As : AsBn#2 commands<i=.name2
commands<i=.description)%
return -%
/
Figure *: The he&p! command e#ecution function
So, when a new command is entered b$ a user, the e&ecute_commandC function will
search for this command in the commands arra$, as shown in figure ="
int e&ecute_command(char 0command_strin1)
...
int command_'ound " -%
int i %
'or (i " -% i , )*?_OF_CO??8)>S% i@@)
command_'ound " 1%
.reak%
/
/
...
/
Figure +: Searching the command in commands arra)
If the command name is found, then e!ecute the command along with its
arguments" >lse, displa$s an error message" 6s shown in figure ?"
Figure ,: -#ecuting the command
int e&ecute_command(char 0command_strin1)
...
i'(command_'ound)
int return_value%
return_value " commands<i=.'unction_to_e&ecute(
num.er_o'_ar1uments2ar1uments)%
return return_value%
/
else