Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 451 Section 2 - Spring 2005.

Similar presentations


Presentation on theme: "CSE 451 Section 2 - Spring 2005."— Presentation transcript:

1 CSE 451 Section 2 - Spring 2005

2 Reminders Homework 2 due tomorrow Project 1 is out (as of yesterday)
Start thinking about project groups (3 people) for the rest of the quarter You’ll be using those for the rest of the projects! Today: project 1 overview

3 First Project Introduces C and Unix skills you’ll need
Teaches how to build and run Linux in VMWare Two main parts: Write a simple shell in C Add a simple system call to Linux kernel Due: Thurs, April 14, before section (12:00pm) Electronic turnin: code + writeup Dust off C skills Become familiar with the environment we use

4 What does a shell do? Print out prompt Accept input Parse input
If built-in command do it directly Else create new process Launch specified program there Wait for it to finish Repeat CSE451Shell% /bin/date Fri Jan 16 00:05:39 PST 2004 CSE451Shell% pwd /root CSE451Shell% cd / / CSE451Shell% exit Actually, also special built-in commands which do not fork off a process. E.g.: cd, exit, new syscall execcounts.

5 Writing a new shell Just a C program that executes a big while loop
Has 2 internal commands - cd, exit All other commands are invokable executables (given with full path names like /bin/ls) Use fork to create a child process that executes the executable Child should use ‘execv’ or one of its related versions (execvp, etc). See man pages for help. Parent (shell) waits for child to finish Use ‘wait’ function (man wait for help) Useful version is ‘waitpid’ Last step: add your execcounts syscall as a 3rd internal command

6 The shell + using C strings
Your shell will need to manipulate/parse C strings. You only need to use: strncmp(src,dest,n) – compare first n chars of strings, 0 if equal, not 0 o.w. Do not do str1 == str2!!! strtok: parse string into tokens 1st use: tok = strtok(buf, “delimiters”); Subsequent uses: tok = strtok(NULL, “delimiters”); fgets(buf, n, stdin) – read a line (up to n=256 chars) from stdin (or getline) (maybe) strncpy(dest, src, n) – copy up to n=256 chars from src to dest. (maybe) Allocate memory with malloc, free with free Fine to assume: A maximum length for a shell command (say, n=256) Maximum number of arguments (say, 256 again)

7 System Calls What’s a system call? Examples? …?
Examples used in your shell: Use fork to create a child process Use execvp to execute a specified program Use wait to wait until child process terminates a system call is the mechanism used by an application program to request service from the operating system. System calls often use a special machine code instruction which causes the processor to change mode (e.g. to "supervisor mode" or "protected mode"). This allows the OS to perform restricted actions such as accessing hardware devices or the memory management unit.

8 Part 2: Adding a System Call
Add execcounts system call to Linux: Purpose: collect statistics Count number of times you call fork, vfork, clone, and exec system calls. Steps: Modify kernel to keep track of this information We give you the kernel code Add execcounts wrapper to return the counts to the user Use execcounts in your shell to get this data from kernel and print it out.

9 Example of execcounts CSE451Shell% execcounts clear CSE451Shell% cd /
CSE451Shell% pwd / CSE451Shell% date Wed Sep 29 16:52:41 PDT 2004 CSE451Shell% time Usage: time [-apvV] [-f format] [-o file] [--append] [--verbose] [--portability] [--format=format] [--output=file] [--version] [--help] command [arg...] CSE451Shell% execcounts Statistics: Fork: % Clone: % VFork: % Exec: % CSE451Shell% exit

10 Linux syscall implementation
Called indirectly via interrupt, looked up in system call table New syscall => new entry in table How do they work in Linux? Consider a syscall: foo foo is a wrapper function that calls syscall() with foo’s system call number System call numbers defined in <asm/unistd.h> Look for #define __NR_foo num syscall() loads the number and parameters into registers, saves process state, raises interrupt (0x80 on x86 architecture) Control flow jumps to kernel at location ‘system_call’ Looks up number in system call table to find kernel function (in entry.S assembly file) Kernel function ‘sys_foo()’ executes Control/process state restored back to user This is a 50,000 foot aerial view of linux sys calls You’ll need to dive in for a ground-level view Suggestion: google. There are lots of tutorials out there for adding sys calls to linux.

11 Howto: add linux syscall
What you’ll need to do: Add new system call to the sys_call_table Define unique sys call number for execcounts (the “stub”) Write the actual function: sys_execcounts Modify any kernel code necessary to track info needed by sys_execcounts Add appropriate header files Probably need a structure to hold execcounts info! Finally, integrate as internal command ‘execcounts’ for your new shell

12 Programming in kernel mode
Your shell will operate in user mode Your system call code will be in the Linux kernel, which operates in kernel mode Be careful - different programming rules, conventions, etc.

13 Programming in kernel mode
Can’t use application libraries (e.g. libc) E.g. can’t use printf Use only functions defined by the kernel E.g. use printk instead Include files are different in the kernel Don’t forget you’re in kernel space E.g. unsafe to access a pointer from user space directly, use fn’s that perform checks Example: Copy_to/from_user functions (don’t use user pointers explicitly! -- unsafe) Best way to learn – look at existing code Look how the other syscalls are implemented As a programmer, you know that an application can call functions it doesn't define: the linking stage resolves external references using the appropriate library of functions. printf is one of those callable functions and is defined in libc. In the kernel, the only functions you can call are the ones exported by the kernel; there are no libraries to link to. The printk function, for example, is the version of printf defined within the kernel and exported to modules. It behaves similarly to the original function, with a few minor differences, the main one being lack of floating-point support.[6]

14 Computing Resources Develop your code on dedicated 451 Linux hosts:
spinlock, coredump Each of you has a scratch space at: /cse451/<username> Test your code on VMWare PCs in 006 Do not use attu

15 VMWare Software simulation of x86 architecture Run an OS in a sandbox
Easily reset to known good state

16 Using VMWare All disks are nonpersistent Network adapter is host-only
Power on/off, reset VMWare config Don’t change! All disks are nonpersistent Powering off loses your changes! Use “shutdown –r now” instead Network adapter is host-only

17 Linux && VMWare There is only one user: root
The password is rootpassword You will need to: Build a kernel image on spinlock/coredump Transfer it to Linux running inside VMWare Boot your new Linux kernel in VMWare Use ftp to get your files into VMWare FTP to from the host running VMWare. E.g. using IE, go to

18 UNIX & C help Unix & C tutorial links on 451 projects page
What if my shell crashes? Use gdb to debug gdb tutorials linked on web site What do I use to compile my shell? gcc. For example, gcc –o shell shell.c –Wall -g What do I use to type up my code? I recommend Emacs (look for Emacs tutorials) VS.NET works too

19 UNIX & C help - 2 How do I find stuff in the kernel source?
Use grep –r search_string * Use LXR (Linux Cross Reference): Which library functions does C have to simplify my shell code? man strncmp, gets, fgets, strtok, strchr, perror

20 C Debugging hint #define MYDEBUG // comment out to disable debugging
#ifdef MYDEBUG #define DEBUG(x) x #else #define DEBUG(x) #endif int main() { printf(“normal output”); DEBUG(printf(“debug output“)); }

21 More debugging Just for printing:
#ifdef MYDEBUG # ifdef __KERNEL__ /* This one if debugging is on, and kernel space */ # define PDEBUG(fmt, args...) printk(“myprg: " fmt, ## args) # else /* This one for user space */ # define PDEBUG(fmt, args...) fprintf(stderr, fmt, ## args) # endif #else # define PDEBUG(fmt, args...) /* not debugging: nothing */ #endif works for both for kernel and userspace To use: PDEBUG(“Testing two numbers: %d and %d\n”, num, num2);


Download ppt "CSE 451 Section 2 - Spring 2005."

Similar presentations


Ads by Google