Operating Systems Lab 2

10
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 Linux. Steps 1- First run your Linux image using VMware player 2- Double click the FOS_2008 shortcut on your desktop 3- Open directory “part1/kern” This directory contains number of files; we are interested only in the following: Entry.s: contains an assembly code responsible for system initialization (initializing segment registers, etc…) before calling the main Kernel function “FOS_initialize()init.c: contains the C code for “FOS_initialize()” function, this function is responsible for running the command prompt, by calling the “run_command_prompt()” function Command_prompt.c: contains the “run_command_prompt()” function, this function is responsible for displaying the command prompt to the user, accepting the command and executing it. Command_prompt.h: the header file for command_prompt.c (contains the functions declaration) The section contains the following: 1- Navigating the Command Prompt code. 2- Adding simple commands to the Command Prompt.

description

os

Transcript of Operating Systems Lab 2

Page 1: Operating Systems Lab 2

Operating Systems Course

Lab 2: Kernel I“Navigating the kernel source code & adding simple commands to

the shell”

IntroductionIn 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 Linux.

Steps1- First run your Linux image using VMware player2- Double click the FOS_2008 shortcut on your desktop3- Open directory “part1/kern”

This directory contains number of files; we are interested only in the following:

Entry.s: contains an assembly code responsible for system initialization (initializing segment registers, etc…) before calling the main Kernel function “FOS_initialize()”

init.c: contains the C code for “FOS_initialize()” function, this function is responsible for running the command prompt, by calling the “run_command_prompt()” function

Command_prompt.c: contains the “run_command_prompt()” function, this function is responsible for displaying the command prompt to the user, accepting the command and executing it.

Command_prompt.h: the header file for command_prompt.c (contains the functions declaration)

The section contains the following:

1- Navigating the Command Prompt code.2- Adding simple commands to the Command Prompt.

Page 2: Operating Systems Lab 2

First: Navigating the Command Prompt Command Prompt s responsible for the following jobs:

Displaying the command prompt to the FOS user, accepting the user command,

executing the user command

1. Displaying the command prompt and accepting the user input:

This is done using the “run_command_prompt()” function, as in figure 1.

void run_command_prompt(){

...

while (1){

command_line = readline("FOS> ");//parse and execute the commandif (command_line != NULL)

if (execute_command(command_line) < 0)break;

}

...}

Figure 1 : The portion of code in “run_command_prompt()” thatinvokes the command execution sequence

As you see in the figure, when the user inputs the desired command (must not be a blank string) and presses <enter>, the “run_command_prompt()” starts to execute the command by calling the “execute_command(char *)” function. The kernel is now going to execute the command.

2. Executing the user command:

The function “execute_command(char *)” works as following:A. Split the command string into whitespace-separated arguments:

int execute_command(char *command_string){

int number_of_arguments;char **arguments;strsplit(command_string, WHITESPACE, &arguments, &number_of_arguments) ;if (number_of_arguments == 0)

return 0;...

Page 3: Operating Systems Lab 2

Figure 2: Splitting the command string

B. Lookup in the “commands” array for the provided command name:

The commands are stored in an array of “struct Command” objects.

struct Command {

char *name;char *description;// return -1 to force command prompt to exitint (*function_to_execute)(int number_of_arguments, char** arguments);

};

Figure 3: The command structure

As you see in figure 3, each command object holds the following: name, description: text shown to user, function_to_execute: pointer to a function in “command_prompt.c” file

that contains the command code.

Initially there are only two commands that are stored in the “commands[]” array, as shown in figure 4:

1. help2. kernel_infostruct Command commands[] ={

{"help","Display this list of commands",command_help},{"kernel_info","Display information about the kernel", command_kernel_info}

};

Figure 4: The “commands[]” array

For example, as you see in the figure, the command “help” uses the “command_help(int number_of_arguments, char **arguments)” function, defined in the “command_prompt.c” file, to execute. See figure 5.

Page 4: Operating Systems Lab 2

int command_help(int number_of_arguments, char **arguments){

int i;

for (i = 0; i < NUM_OF_COMMANDS; i++)cprintf("%s - %s\n", commands[i].name, commands[i].description);

return 0;}

Figure 5: The “help” command execution functionSo, when a new command is entered by a user, the “execute_command” function will search for this command in the “commands” array, as shown in figure 6.

int execute_command(char *command_string){

...

int command_found = 0;int i ;for (i = 0; i < NUM_OF_COMMANDS; i++){

if (strcmp(arguments[0], commands[i].name) == 0)

{command_found = 1;break;

}}

...}

Figure 6: Searching the command in commands array

If the command name is found, then execute the command along with its arguments. Else, displays an error message. As shown in figure 7.

Figure 7: Executing the commandint execute_command(char *command_string){

...

if(command_found){

int return_value;return_value = commands[i].function_to_execute( number_of_arguments,arguments);return return_value;

}else{

//if not found, then it's unknown commandcprintf("Unknown command '%s'\n",

arguments[0]);return 0;

}

...}

Page 5: Operating Systems Lab 2

Second: Adding Simple Commands to the Command Prompt

To add a new command, we have to:

1- add a new “command” object in the “commands” array2- add the function that is responsible for executing the command:

a. add the function declaration in “Command_prompt.h”b. add the function definition in “Command_prompt.c”

Example 1:

Add the following command to the FOS command prompt:

Name: versionArguments: noneDescription: prints the FOS kernel version

Solution:1- add a new “command” object in the “commands” array, figure 8

struct Command commands[] ={

{"help","Display this list of commands",command_help},{"kernel_info","Display information about the kernel", command_kernel_info}{"version","Display FOS kernel version", command_kernel_version}

};

Figure 8: adding new “command” object for “version” command

2- add the function declaration of “command_kernel_version()” that is responsible for executing the command, see figure 9

//Command_prompt.h...int command_kernel_version(int number_of_arguments, char **arguments);...

Figure 9: add function declaration of “version” command to “Command_prompt.h” file

3- add the function definition of “command_kernel_version()”, see figure 10

Figure 10: add function definition of “version” command to “Command_prompt.c” file

Example 2:

int command_kernel_version(int number_of_arguments, char **arguments){

cprintf("FOS kernel version 0.1\n");return 0;

}

Page 6: Operating Systems Lab 2

Add the following command to the FOS command prompt:

Name: addArguments:

Argument 1: integer (represents first operand)Argument 2: integer (represents second operand)

Description: Displays on screen the value (Argument 1 + Argument 2)

Solution:1- add a new “command” object in the “commands” array, figure 11

struct Command commands[] ={

{"help","Display this list of commands",command_help},{"kernel_info","Display information about the kernel", command_kernel_info}{"version","Display FOS kernel version", command_kernel_version}{"add","Takes 2 integers n1, n2 and displays n1 + n2", command_add}

};

Figure 11: adding new “command” object for “add” command

4- add the function declaration of “command_add()” that is responsible for executing the command, see figure 12

//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 declaration of “add” command to “Command_prompt.h” file

5- add the function definition of “command_add()”, see figure 13

Figure 13: add function definition of “add” command to “Command_prompt.c” fileint command_add(int number_of_arguments, char **arguments){

int n1, n2, res;n1 = strtol(arguments[1], NULL, 10);n2 = strtol(arguments[2], NULL, 10);

res = n1+n2;

cprintf("%d + %d = %d\n", n1, n2, res);return 0;

}

Page 7: Operating Systems Lab 2

Assignment I:1) Modify the Kernel code (command_prompt.c), to print a "Invalid number of arguments" message when the user types any command with invalid number of arguments?Ex.:

FOS> help 111Invalid number of argumentsFOS>_

2) Add the following commands to the Kernel:

a- rep <string> <N>This command should echo the given string on the screen for N times

Ex.:FOS> rep OS 6OSOSOSOSOSOSFOS>_

b- haltThis command should halt the FOS Kernel (i.e. break the busy-while loops)

3) Add a “Command Concatenation” feature to the command prompt, which allow us to execute set of commands in sequence separated “&” character

Ex.:FOS> help 111 & rep OS 6 & rep Finished 1Invalid number of argumentsOSOSOSOSOSOSFinishedFOS>_

ReferencesMIT OCW OS Course lab 6828 , UTexas OS Course lab CS372H (lab1 & lab2).