CSE 451 Section 2 - Spring 2005.

Slides:



Advertisements
Similar presentations
CSCC69: Operating Systems
Advertisements

Chapter 6 Limited Direct Execution
CSE 451: Operating Systems Section 2 Shells and System Calls.
1 Processes Professor Jennifer Rexford
1 CSE 451 Section Autumn 2004 Alex Moshchuk Office hours: Tue 1-2, Thu 4:30-5:30 Allen 218 (or lab)
CSE 451 Section Autumn 2005 Richard Dunn Office hours: WTh 3:30-4:20 Allen 216 (or lab)
1 Reminders Project 1 due tomorrow by 6:00 Office hours in 006 today at 4:30 Start thinking about project groups (3 people) for the rest of the quarter.
OS Spring’03 Introduction Operating Systems Spring 2003.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
CSE 451 Section 4 Project 2 Design Considerations.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
CSE 451: Operating Systems Section 2 Interrupts, Syscalls, Virtual Machines, and Project 1.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
System Calls 1.
Project 1, Command Shell CS-502 (EMC) Fall Programming Project #1 Command Shell CS-502, Operating Systems EMC, Fall 2009 (Slides include materials.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
CSE 451 – Operating Systems Section, Winter 2003 Scapegoats: Evan Welbourne Albert J. Wong
Creating and Executing Processes
CSE 451 – Operating Systems Section, Autumn 2003 TAs: Mike Swift Adrienne Noble
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1.
More on UART Interrupts; System Calls Reference on Interrupt Identification Register(IIR) slide 17 of
Project 2: Initial Implementation Notes Tao Yang.
A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.
CNIT 127: Exploit Development Ch 3: Shellcode. Topics Protection rings Syscalls Shellcode nasm Assembler ld GNU Linker objdump to see contents of object.
CSE 451: Operating Systems Section 3: Project 0 recap, Project 1.
Operating Systems Process Creation
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Genesis: From Raw Hardware to Processes Andy Wang Operating Systems COP 4610 / CGS 5765.
Project 2: User Programs Abdelmounaam Rezgui Acknowledgment: The content of some slides is partly taken from Josh Wiseman’s presentation.
CSE451 Section 2: Spring 2006 Kurtis Heimerl, YongChul Kwon
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Introduction Contain two or more CPU share common memory and peripherals. Provide greater system throughput. Multiple processor executing simultaneous.
1 CSE 451 Section 2: Processes, the shell, and system calls.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
CSE 451 Section Autumn 2004 Alex Moshchuk Office hours: Tue 2-3, Thu 4:30-5:30 Allen 216 (or lab)
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
CSE 451 Section #3 Project 0 Recap Project 1 Overview
Exceptional Control Flow
YongChul Kwon CSE451 Section 1: Spring 2006 YongChul Kwon
Introduction to Operating Systems
CSE 451 Section 2: Processes, the shell, and system calls
CS 3305 System Calls Lecture 7.
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
Introduction to Operating Systems
SAPC Hardware Pentium CPU (or 486) 4M usable memory
Genesis: From Raw Hardware to Processes
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Unix System Calls and Posix Threads
Process Programming Interface
Programming Project #1 Command Shell
CSE 451 Section 2 – Winter 2006.
System Calls David Ferry CSCI 3500 – Operating Systems
Tutorial: The Programming Interface
Programming Project #1 Fork and Command Shell
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
Lecture 6: Multiprogramming and Context Switching
Processes in Unix, Linux, and Windows
January 8, 2004 Adrienne Noble
Reminders Project 1 due tomorrow at 5:00 pm
Processes in Unix and Windows
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Intro to the Shell with Fork, Exec, Wait
Presentation transcript:

CSE 451 Section 2 - Spring 2005

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

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

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.

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

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)

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.

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.

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: 3 27% Clone: 0 0% VFork: 0 0% Exec: 8 72% CSE451Shell% exit

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.

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

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.

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]

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

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

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

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 192.168.93.2 from the host running VMWare. E.g. using IE, go to ftp://root:rootpassword@192.168.93.2

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

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

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“)); }

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