You are on page 1of 7

Operating Systems Course

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)

command_line " readline(#FOS$ #)%


//parse and e&ecute the command
i' (command_line (" )*++)
i' (e&ecute_command(command_line) , -)
.reak%
/
...
/
Figure 1 : The portion of code in run_command_prompt(! that
invo"es the command e#ecution se$uence
6s $ou see in the figure, when the user inputs the desired command .must not be a
blank string0 and presses 8enter9, the run_command_prompt() starts to e!ecute the
command b$ calling the e&ecute_command(char 0) function" 2he kernel is now going
to e!ecute the command"
2. Executing the user command:
2he function e&ecute_command(char 0) works as following,
%. Split the command string into whitespace4separated arguments,
int e&ecute_command(char 0command_strin1)

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<= "

#help#2#>isplay this list o'


commands#2command_help/2
#kernel_in'o#2#>isplay in'ormation a.out the
kernel#2 command_kernel_in'o/
/%
Figure (: The commands[] arra)
#or e!ample, as $ou see in the figure, the command help uses the
command_help(int num.er_o'_ar1uments2 char 00ar1uments) function, defined in
the command_prompt.c file, to e!ecute" See figure <"
int command_help(int num.er_o'_ar1uments2 char
00ar1uments)

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@@)

i' (strcmp(ar1uments<-=2 commands<i=.name) ""


-)

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

//i' not 'ound2 then itDs unkno!n command


cprint'(#*nkno!n command DAsDBn#2
ar1uments<-=)%
return -%
/
...
/
Second: %dding Simp&e .ommands to the .ommand /rompt
2o add a new command, we have to,
34 add a new EcommandC ob7ect in the EcommandsC arra$
)4 add the function that is responsible for e!ecuting the command,
a" add the function declaration in ECommand_prompt.hC
b" add the function definition in ECommand_prompt.cC
-#amp&e 1:
6dd the following command to the #OS command prompt,
Name, version
6rguments, none
'escription, prints the #OS kernel version
So&ution:
34 add a new EcommandC ob7ect in the EcommandsC arra$, figure +
struct Command commands<= "

#help#2#>isplay this list o' commands#2command_help/2


#kernel_in'o#2#>isplay in'ormation a.out the kernel#2
command_kernel_in'o/
{"version","Display FOS kernel version",
command_kernel_version}
/%
Figure 0: adding ne1 command o23ect for version! command
2- add the function declaration of Ecommand_kernel_version()C that is responsible for
e!ecuting the command, see figure @
//Command_prompt.h
...
int command_kernel_version(int number_of_arguments, char
**arguments);
...
Figure 4: add function dec&aration of version! command to ECommand_prompt.hC fi&e
3- add the function definition of Ecommand_kernel_version()C, see figure 3*
Figure 15: add function definition of version! command to ECommand_prompt.cC fi&e
-#amp&e 2:
int command_kernel_version(int number_of_arguments, char **arguments)
{
cprintf(!"# kernel version $.%&n);
return $;
'
6dd the following command to the #OS command prompt,
Name, add
6rguments,
6rgument 3, integer .represents first operand0
6rgument ), integer .represents second operand0
'escription, 'ispla$s on screen the value .6rgument 3 A 6rgument )0
So&ution:
34 add a new EcommandC ob7ect in the EcommandsC arra$, figure 33
struct Command commands<= "

#help#2#>isplay this list o' commands#2command_help/2


#kernel_in'o#2#>isplay in'ormation a.out the kernel#2
command_kernel_in'o/
{"version","Display FOS kernel version",
command_kernel_version}
{"add","Takes 2 integers n1, n2 and displays n1 + n2",
command_add}
/%
Figure 11: adding ne1 command o23ect for add! command
(- add the function declaration of Ecommand_add()C that is responsible for e!ecuting the
command, see figure 3)
//Command_prompt.h
...
int command_kernel_version(int number_of_arguments, char
**arguments);
int command_add(int number_of_arguments, char **arguments);
...
Figure 12: add function dec&aration of add! command to ECommand_prompt.hC fi&e
*- add the function definition of Ecommand_add()C, see figure 3:
Figure 13: add function definition of add! command to ECommand_prompt.cC fi&e
int command_add(int number_of_arguments, char **arguments)
{
int n%, n(, res;
n% ) strtol(arguments*%+, ,-.., %$);
n( ) strtol(arguments*(+, ,-.., %$);
res ) n%/n(;
cprintf(0d / 0d ) 0d&n, n%, n(, res);
return $;
'
Assignment I:
30 &odif$ the 1ernel code .command(prompt"c0, to print a BInvalid number of argumentsB
message when the user t$pes an$ command with invalid number of argumentsC
>!",
#OS9 help 333
Invalid number of arguments
#OS9(
)0 6dd the following commands to the 1ernel,
a4 rep 8string9 8N9
2his command should echo the given string on the screen for N times
>!",
#OS9 rep OS =
OS
OS
OS
OS
OS
OS
#OS9(
b4 halt
2his command should halt the #OS 1ernel .i"e" break the bus$4while loops0
:0 6dd a Command Concatenation feature to the command prompt, which allow us to
e!ecute set of commands in seDuence separated 6 character
>!",
#OS9 help 333 & rep OS = & rep #inished 3
Invalid number of arguments
OS
OS
OS
OS
OS
OS
#inished
#OS9(
References
&I2 OCE OS Course lab =+)+ , F2e!as OS Course lab CS:?)G .lab3 & lab)0"

You might also like