Lab guidance

Hardness of assignments

Each assignment indicates how difficult it is:

These times are rough estimates of our expectations. For some of the optional assignments we don't have a solution and the hardness is a wild guess. If you find yourself spending more time on an assignment than we expect, please reach out on piazza or come to office hours.

The exercises in general require not many lines of code (tens to a few hundred lines), but the code is conceptually complicated and often details matter a lot. So, make sure you do the assigned reading for the labs, read the relevant files through, consult the documentation (the RISC-V manuals etc. are on the reference page) before you write any code. Only when you have a firm grasp of the assignment and solution, then start coding. When you start coding, implement your solution in small steps (the assignments often suggest how to break the problem down in smaller steps) and test whether each steps works before proceeding to the next one.

Warning: don't start a lab the night before a lab is due; it much more time efficient to do the labs in several sessions spread over multiple days. The manifestation of a bug in operating system kernel can be bewildering and may require much thought and careful debugging to understand and fix.

Vim tips

Working on our labs requires editing code on Linux using the vim. There are some alternatives such as emacs and nano. However, I think using vim is your best alternative. My first code editor was a punched card. I wrote code using punched cards from 1974 until 1981. After punched cards, I used a keyboard with scrolling paper to edit code. You could list lines, add lines, and edit lines. In 1983, I wrote code on a VT100 connected to a VAX, using the EDT editor. Sometime in the late 1980's or early 1990's, I began using vim. I initially selected emacs, but it was not available on all computers so I selected vim. Once you get used to vim, you can edit source code efficiently.

.vimrc

.vimrc is read by vim to apply your specific features. The following is my .vimrc thoughts, followed by the contents of my .vimrc file.

My .vimrc file.

" custom vim settings go into this file
set number
set tabstop=4
set shiftwidth=4
set expandtab
set incsearch
set hlsearch
syntax on
set mouse=a
let g:netrw_banner = 0
let g:netrw_liststyle = 0
" Values for netrw_browse_split
" 1 open file in new horizontal split
" 2 open file in new vertical split
" 3 open file in new tab
" 4 open in previous window
let g:netrw_browse_split = 3
let g:netrw_altv = 1
let g:netrw_winsize = 15
let g:netrw_list_hide = '^\.\=/\=$,.DS_Store,.git,*\.o,\.*\.swp,.h'
let g:netrw_hide = 1
" Uncomment the following commands to have Vexp called on Vim startup
"" augroup ProjectDrawer
""   autocmd!
""   autocmd VimEnter * :Vexplore
"" augroup END
map <c-l> :Vex<CR>

ctags

When editing source code for a program that is in multiple .c files, you want to bounce from one .c file to another discovering the definitions of functions and data. The ctags program creates a tags file, which allows you to begin editing in one file and easily jump to another file that contains the definitions of functions and data. There are various ways to create a tags file, but the simplist is to place source code in a directoy and run the ctags program. The Xv6 program has two primary directories of source code. The kernel directory contains to OS kernel source code. The user directory contains the utility program source code. You can create a tags file in both directories. For example, to create a tags file in the kernel directory.

$ cd kernel
$ tags *.c *.h

When you use vim to edit a file in the kernel directory, vim reads the tags file. Now you can use Ctl-] and Ctl-t to bounce from the current file being shown to another file containing the definition of the function or data. Suppose I am editing file.c, and my cursor is on a line calling the function acquire as mimiced below.

struct file*
filedup(struct file *f)
{
acquire(&ftable.lock);

Pressing Ctl-] opens the file spinlock.c and positions the cursor on the function acquire. When you are finished examining the function acquire, you return to file.c by pressing Ctl-t. Both file.c and spinlock.c remain a buffers in your vim editing session.

The tags file many have multiple entries for a tag, and Ctl-] jumps to the first match. You can view additional matches by pressing g] or g Ctl-], which show all matches and allow you to select a destination. Other options are for you to enter :tselect or :tjump, which also show all matches and allow you to select a destination.

Shell tips

When using Linux, it is beneficial to use a nice shell. For example, bash has tab completion. The shell /bin/sh does not provide tab completion.

Debugging Tips

Here are some tips for debugging your solutions: It is well worth the time learning the above-mentioned tools.