250 likes | 458 Views
CSE451 Section 3. Reminders. Start project 2! It’s long Read the assignment carefully Read it again Project 2 will be done in groups of 3
E N D
Reminders • Start project 2! • It’s long • Read the assignment carefully • Read it again • Project 2 will be done in groups of 3 • Form project groups, who you’ll be with for the rest of the projects. Email all three of us (Kurtis, Lillie, and Ed) your chosen groups by the end of the day on Friday– after that we’ll pair up everyone who’s unpaired • Project 2 intro • CVS
Project 2 • You have to: • Implement a user thread library • Implement synchronization primitives • Solve a synchronization problem • Implement a multithreaded web server • Add Preemption • Get some results and write a (small) report • First three parts due: in two weeks • Rest of it due: 11 days after that Parts 1,2,3 Parts 4,5,6
Simplethreads • We give you: • Skeleton functions for thread interface • Machine-specific code • Support for creating new stacks • Support for saving regs/switching stacks • A generic queue • When do you need one? • Very simple test programs • You should write more • Singlethreaded web server
Simplethreads Code Structure test/*.c Web server (web/sioux.c) Other apps include/sthread.h You write this lib/sthread_user.c lib/sthread_user.h lib/sthread_queue.h lib/sthread_ctx.h lib/sthread_preempt.h lib/sthread_queue.c lib/sthread_ctx.c lib/sthread_preempt.c lib/sthread_switch.S sthread_switch_i386.h sthread_switch_powerpc.h
Thread Operations • void sthread_init() • Initialize the whole system • sthread_t sthread_create(func start_func, void *arg) • Create a new thread and make it runnable • void sthread_yield() • Give up the CPU • void sthread_exit(void *ret) • Exit current thread • What about the TCB (Thread control block)? struct _thread { sthread_ctx_t *saved_ctx; ……… } • Others?
Sample multithreaded program int main(int argc, char **argv) { int i; sthread_init(); if (sthread_create(thread_start, (void*)i) == NULL) { printf("sthread_create failed\n"); exit(1); } sthread_yield(); //yield main thread to our new thread printf("back in main\n"); return 0; } void *thread_start(void *arg) { printf("In thread_start, arg = %d\n", (int)arg); return 0; } • Output? (assume no preemption)
Managing Contexts (given) • Thread context = thread stack + stack pointer • sthread_new_ctx(func_to_run) • gives a new thread context that can be switched to • sthread_free_ctx(some_old_ctx) • Deletes the supplied context • sthread_switch(oldctx, newctx) • Puts current context into oldctx • Takes newctx and makes it current
How sthread_switch works Thread 1 TCB … SP Thread 2 TCB … SP Xsthread_switch: pusha movl %esp,(%eax) movl %edx,%esp popa ret Thread 2 registers CPU ESP Thread 1 running Thread 2 ready Thread 1 regs Want to switch to thread 2…
Push old context Thread 1 TCB … SP Thread 2 TCB … SP Xsthread_switch: pusha movl %esp,(%eax) movl %edx,%esp popa ret Thread 1 registers Thread 2 registers CPU ESP Thread 1 running Thread 2 ready Thread 1 regs
Save old stack pointer Thread 1 TCB … SP Thread 2 TCB … SP Xsthread_switch: pusha movl %esp,(%eax) movl %edx,%esp popa ret Thread 1 registers Thread 2 registers CPU ESP Thread 1 running Thread 2 ready Thread 1 regs
Change stack pointers Thread 1 TCB … SP Thread 2 TCB … SP Xsthread_switch: pusha movl %esp,(%eax) movl %edx,%esp popa ret Thread 1 registers Thread 2 registers CPU ESP Thread 1 ready Thread 2 running Thread 1 regs
Pop off new context Thread 1 TCB … SP Thread 2 TCB … SP Xsthread_switch: pusha movl %esp,(%eax) movl %edx,%esp popa ret Thread 1 registers CPU ESP Thread 1 ready Thread 2 running Thread 2 regs
Done; return Thread 1 TCB … SP Thread 2 TCB … SP Xsthread_switch: pusha movl %esp,(%eax) movl %edx,%esp popa ret Thread 1 registers • What got switched? • SP • PC (how?) • Other registers CPU ESP Thread 1 ready Thread 2 running Thread 2 regs
Adjusting the PC Thread 1 TCB … SP Thread 2 TCB … SP • ret pops off the new return address! Thread 1 registers ra=0x400 ra=0x800 CPU ESP Thread 1 (stopped): switch(t1,t2); 0x400: printf(“test 1”); Thread 2 running: switch(t2,...); 0x800: printf(“test 2”); PC
Things to think about • Who will call sthread_switch? • Where does sthread_switch return? • How do we delete a thread? • Can a thread free its stack itself? • Starting up a thread • When you create a new stack with sthread_new_ctx(), you initialize it to run some function foo • sthread_new_ctx doesn’t pass parameters to foo • But in sthread_create, you give a function and an arg! • Bottom line: how do you pass arguments to a function with no arguments?
Preemption • Initially, you will build a non-preemptive thread library. • In part 4, you add preemption • Much more realistic model • We give you: • Timer interrupts • Primitives to enable/disable interrupts • atomic_test_and_set/atomic_clear for synchronization • You must: • Do something on each timer interrupt. (What?) • Synchronize all of your code using 2 and 3 above. • More on this later…
Last note • You can compare your implementation against pthreads (which is preemptive kernel-threads). • ./configure --with-pthreads
What is CVS • Version control system for source files • Multiple users can work on the same file simultaneously
Why use CVS • The other way: • Keep every version of code, all with different names: • Project2good • Project2_10_13_04 • Project2working • Project2_Feb_2_alex • Project2_old • Send emails back and forth with new changes • Merge different versions by hand • The CVS way: • One version, saved in the CVS repository • Multiple people can work on the same file concurrently • CVS merges the edited versions automatically as you put them back in the repository
Setting up CVS • Set up CVS root environment var • Tells CVS where to find your repository • setenv CVSROOT /cse451/yourgroup/cvs • (bash)export CVSROOT=/cse451/yourgroup/cvs • Initialize a repository (only one person per group) • Create a dir in your group’s dir to hold repository (master copy of code) • cd /cse451/yourgroup • mkdir cvs • Initialize repository • cvs init • You now have an empty repository
Setting up CVS (2) • Add/Import the simplethreads distribution to your repository • tar xvfz simplethreads-1.20.tar.gz • cd simplethreads-1.20 • cvs import –m “initial code” simplethreads SIMPLETHREADS SIMPLETHREADS_1_20 • cd .. • rm –fr simplethreads-1.20
CVS Commands • Check out a project (sandbox, or local copy) to your home directory to work on: • CVS creates a dir simplethreads/ and puts copy of all source files in repository into that dir • Also adds CVS dir where it stores data about what you check out • cd ~ • cvs checkout simplethreads • cd simplethreads • Merge in new changes from repository (update): • cvs update [files…] • Save your edited files into the repository so others can use them: • cvs commit –m “fixed annoying bugs” [files…]
CVS Commands 2 • Add a new file (source files .c or .h) to the repository • cvs add [files…] • Check status of a file • cvs status file.c • Check differences between your file and one in the repository: • cvs diff file.c • cvs diff –r 1.1 file.c (specifies version) • View log of changes for a file • cvs log file.c • Use emacs for cvs commit log editing (default is vi): • setenv VISUAL emacs More info: • http://www.cvshome.org • cvs --help-commands
CVS Remote Access • Access CVS from another machine besides forkbomb?: • setenv CVSROOT coredump.cs.washington.edu:/cse451/yourgroup/cvs • setenv CVS_RSH ssh (for CVS to know how to access repository – use ssh)(add to ~/.login (csh) or ~/.profile (bash) to avoid typing…) Warning: be wary of CVS on windows, OS X is ok