Assignment #1 Solution

$30.00

Category:

Description

Introduction:

 

The purpose of this exercise is to bring everyone to a common point with basic Linux, C programming, and the Linux-based tools for working with C programs. If you have an Ubuntu Linux on a system at home, or any other current Linux distribution, you can do all of this exercise on it and much of the exercise steps will likely be very familiar to you. If you do not have a Linux system at home, you can work through the exercise on one of the available Linux machines in the Lab.

 

  1. Information Gathering:

 

  1. Look at the man page for the “open()” system call, found in section two of the manpages.
    1. List the BUGS when using the system call.
    2. What files need to be included to use this function?
    3. List the first three related system calls to open().
    4. Choose one of the system calls from above and list its bugs (also list what system call you chose) and files needing to be included to use the system call.
  2. Use http://lxr.free-electrons.com to search for the following:
    1. Search for “usb_device”. In what file is the structure defined, and what are the first five members of the struct?
    2. In what header file is the type declared for the 5th member of the struct? (hint: don’t look in test tools or staging directories)
    3. Include the entire enumeration declaration from above.

 

 

  1. Basic Linux Use:

 

  1. Log onto an Ubuntu Linux system
  2. Under each heading in the toolbar at the top, pop up and note the menu and sub menu tools/topics. Note where you find Terminal and the gedit Text Editor.
  3. Click on the Terminal entry to bring up a command shell line. This should be familiar to you from working with Cygwin in ECE 371/372. You will use Terminal to enter Linux commands.
  4. To get an overview of Linux structure and commands, go to

http://gd.tuwien.ac.at/linuxcommand.org/learning_the_shell.php . Study the “Learning the shell” section carefully and make some notes on basic commands to make a directory, determine current directory, change to a different directory, do a long or short form listing of the files in a directory, copy a file, etc.

 

  1. The “script” command is a useful way to make a record of all the commands you enter at the command shell and the results of those commands. Script records the commands, etc. in a file called typescript in your current directory. You terminate script with CTRL-D or ‘exit’. You can then edit/print the typescript file. You will be turning in a printout of the typescript file for a shell practice session.
  2. For a start with the basic shell commands, enter script at the command prompt and then:
    1. Enter the command to determine the current directory
    2. Enter the command to command to show the files in that directory with file permissions.
    3. Enter a command to make a directory called ECE373.
    4. Change the working directory to that directory.
    5. Exit script with CTRL D.

 

  1. Find the gedit program under Application|Accessories and open a gedit window. Find the File|Open menu command and find the typescript file you created. Click on Open and you should see the typescript file in the window. It likely contains some control characters along with the commands but you can easily edit these out, if you want. Click on the Printer icon in gedit to print the typescript file.

 

  1. Basic C Programming in Linux:

 

The tools you will use for developing C programs for this class are the GNU tools, gcc, ln, and gdb. For a start with these, work through the following steps.

 

  1. Type in a simple C program such as a Fahrenheit to Celsius program or one of the examples from the C reference that takes in data and outputs a result to the display.
  2. To compile your C program, enter the command gcc -g -o filename filename.c where filename is the name you gave your C file. The –g switch tells the compiler to include debug information that will be used by the gdb debugger.

 

  1. Edit and recompile the .c file until you get no compile errors.
  2. To run the program from the command line, just enter ./filename.
  3. If the program functions correctly, rejoice, turn off script, print the typescript file, and print the C source file.
  4. If the program does not function correctly or even if it does, it is time to learn about gdb.
  5. At the command prompt, enter man gdb. This should bring up the manual pages for gdb.
  6. Read through the pages using the arrow keys to find out how to use gdb. Write some notes about the main gdb commands such as break, run, next, step, print, and quit. Hit the q key to exit the man pages.
  7. Try running your program under gdb with a gdb filename Enter a breakpoint at main and then run the program. Execution then stops at main(). You can step through and execute one line of the program with step or next (note the difference). You can determine the values of variables at any point with the print command.

 

  1. Experiment with gdb until you are comfortable working through a program with it.

 

 

 

  1. Hello, Kernel:

 

Now that you have the basics, we get to the real point of the whole assignment: load a simple “hello” module into the kernel.

 

  1. Following the notes from the lecture, create a simple hello.c file with the basic __init and __exit functions, where the init function prints “Hello, Kernel” and the exit function prints “Goodbye, Kernel”.

 

  1. Create a simple makefile for compiling the hello.c file into a kernel module

 

  1. Compile the hello.c file into hello.ko, re-editing as necessary to clean up any and all compiler warnings and errors.

 

  1. Load the module into the kernel with the insmod utility and look for the “Hello, Kernel” message in the log file /var/log/messages
  2. Remove the module from the kernel with the rmmod utility and look for the “Goodbye, Kernel” message in the log file.

 

 

 

 

What to turn in:

 

In a text file, or Word document, or Open Office document, include:

 

  • For the manpage exercise, all of the information from #1 in “The Work” above.

 

  • For the LXR exercise, copy and paste the lines of code the LXR hits on PLUS the kernel you searched against. For example, searching for sys_gettimeofday() on kernel 3.14, then the line of code would be lines 203 and 204, and the code would be:

 

asmlinkage long sys_gettimeofday(struct timeval __user *tv, struct timezone __user *tz);

 

Also include the file names requested in each of the parts of the exercise above.

 

  • Print of the example C program from section C.

 

  • Print of the typescript showing execution of program from section C.

 

  • Print of the hello.c module code from section D.

 

  • Print of the ‘hello’ and ‘goodbye’ logfile lines from section D.

 

 

 


error: Content is protected !!