Section notes for CS162 Section #2, February 5 2002 Barbara Hohlt - Welcome to section - Personal introduction, undergrad experience, grad school, research interests, etc. - Logistics: - My class web page: http://www.cs.berkeley.edu/~hohltb/ta/cs162 - Office hours: 11-noon Thurs, 10-11 Fri, 443 Soda - See if office hours times work for people - e-mail: cs162-tb@cory.eecs.berkeley.edu - Please use newsgroup for most things: ucb.class.cs162 - Class info and registration - There will be a form on my webpage to fill out TBD (so I can make sure I have all of your details correct - important!) - Important that I get to know everyone in section - Get to know each other: Go around and say name - Administrivia: - Nachos download - CVS Help Session Thursday February 7 6:30pm 306 Soda - Set up .forward in cs162-xx account - Project meetings: - Will be done in week after you turn in reports - All members of group must be at meeting - You are graded on the report and the meeting - Project info - We will be using Java! - Mainly need to understand basics of language, classes, etc. (not all of the crazy APIs) - You will not be using Java threads (directly) - so don't really need to understand them either - Java/Nachos download is on the course web page - First project design due on Feb 20th - Example design document on web page - GET STARTED NOW! Don't wait until last minute - Please use CVS: - Will really help your project - CVS Help Session Thursday February 7 6:30pm 306 Soda - PROCESSES and THREADS - Difference between PROCESS and THREAD - A "process" generally refers to a single program that you run, with its own code, address space, etc. - The main thing that the operating system deals with - A "thread" is an entity corresponding to a single flow of control through that program, the unit of scheduling - One process can have many threads - those threads all share the same address space, but have their own stack, CPU registers, etc. A process with many threads is called a task. - A Thread Consists of - Program Counter - Register Set - Stack space - it shares with peer threads its code section, data section, and OS resources (open files, signals) - Program text and data - Heap - Other memory (* shared with other threads in process) - I/O state (file descriptors, sockets, etc.) - no protection between threads in a task, no problem because should be cooperating - Thread States - thread states are ready, blocked, running, or terminated - Thread Switch - Save state of current thread -> TCB - Restore state to new thread <- TCB - Jump to new returnPC Hint: It's good to always have one idle thread so you always have a thread to switch to/from. - Kernel Level Threads - uses system calls (interrupt to kernel) - Mach and OS/2 - schedules each thread - User Level Threads - no system calls, faster to switch - Project Andrew (CMU) - kernel schedules at task level - (disadv) if the kernel is single-threaded, than any user-level threads executing a system call will casue the entire task to block until the system call returns - (disadv) scheduling can be unfair - Example of User Level Threads - process a (1) and process b (100), the thread in a runs 100 times faster than a thread in b - How does multiprogramming work? - Remember that only one program is really running at any time - Preemption. OS is interrupted by hardware timer Whenever timer goes off, OS gets control back from program Can decide to CONTEXT SWITCH to another program - OS gets control whenever you do a system call, so you can make scheduling decisions then - OS gets control any time a hardware interrupt occurs - EIT - Exceptions = Interrupts + Traps - Traps are synchronous events in CPU - eg page fault, divide by zero, memory error, illegal instr, illegal address, hardware error, supervisor call (SVC) - Interrupts are asynchronouse events outside of the cpu - eg I/O timer - Hardware support - Memory Management Unit (MMU) provides address translation - Page Table for virtual->physical address mapping - Page Table Base Register (PTBR) points to page table - Checking protection bits in page table entries - Hardware timers - to allow preemption - Protection levels, diallow user code from doing certain things (i.e. mucking with page tables, etc.) - Quick CVS Primer - Walkthrough on the web under 'Projects' - Basic model: Single REPOSITORY which stores the "master" version of the code, as well as all previous versions - Users do NOT edit anything in the repository - rather, they "check out" a copy of the files, and edit that copy - After making changes, you "commit" the changes back to the repository - Other users only see those changes if they do an "update" - Basic CVS usage - Repository will be created for each project group - IMPORTANT CAVEATS: Note that CVS does not allow you to rename files - you need to "remove" the file and then "add" the file under the new name. In general it's best not to rename files, since this loses all of the editing history for that file. Also note that CVS does not allow you to remove or rename directories once they have been added to the repository. THIS IS VERY IMPORTANT!! Do not add directories to the repository temporarily - you can never get rid of them. This is a real drawback to CVS, but it's something you just have to live with. - NACHOS WALKTHROUGH - Homework #1 posted: Need to understand this to do project - Overall: For this project, really need to understand nachos.threads.KThread nachos.threads.ThreadedKernel nachos.machine.TCB (not the internals, just the interface) - Only modify nachos.threads package - nothing else You cannot use Java threads or 'synchronized' keyword - Main structure: nachos.machine.* -- the internals of the implementation Not really important to understand how this works, but need to know what the public methods are Machine.interrupt().disable() Disable interrupts, return flag of previous interrupt state Machine.interrupt().enable() Enable interrupts Machine.interrupt().restore() Restore to previously saved flag TCB.contextSwitch() Context switch to this TCB. Used internally by KThread, you don't need to call this yourself but important to see where it's used TCB.start() Used to bootstrap a new TCB - used internally by KThread nachos.threads.* -- what you will be modifying KThread(Runnable target) Create a new kernel thread and associate with it the code in 'target.run()'. KThread.setName(String name) Associate a new, can be retrieved with getName KThread.fork() Fork the given thread - that is, start it running KThread.yield() Cause the current thread to yield the CPU KThread.sleep() Cause the current thread to block - will be woken up later KThread.ready() Move this thread to the ready queue, i.e. wake it up Lock.acquire() Sleep until this lock can be acquired private KThread lockHolder; intStatus = Machine.interrupt().disable(); if (lockHolder == null) { waitQueue.acquire(KThread.currentThread()); lockHolder = KThread.currentThread(); } else { waitQueue.waitForAccess(KThread.currentThread()); KThread.sleep(); } Machine.interrupt.restore(intStatus); Lock.release() Release the lock (Ask class how this would be implemented) int status = Machine.interrupt().disable(); lockHolder = waitQueue.nextThread(); lockHolder.ready(); // Wake it up Machine.interrupt().restore(intStatus);