Presentation is loading. Please wait.

Presentation is loading. Please wait.

Welcome to the world of concurrency!

Similar presentations


Presentation on theme: "Welcome to the world of concurrency!"— Presentation transcript:

1 Welcome to the world of concurrency!
CSE451 Section 4: Spring 2006 Welcome to the world of concurrency! AA: Kurtis AB: YongChul

2 Threads & synchronization
A rite of passage All undergrad OS courses do The most fun part Will feel like being a GURU  The craziest part Will be sick of synchronization  Start early & read thoroughly Grab a big picture first! Discuss your design with Fellow students TAs

3 User-level threads: to do
First submission(Apr 28 11:00pm) user-level thread manager common synchronization primitives simple synchronization problem Second submission(May 10 11:00am) Preemption Multithreaded web server to test your thread package Analyze your design and report test results

4 Dependency You can work concurrently!  Part 1: Thread manager Part 2:
Synchronization primitives Part 4: Preemption Part 3: Synchronization problem Part 6: Report Part 5: Multithreaded web server pthread compatible You can work concurrently! 

5 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 From Section 3 – Winter 2006

6 Sthread: thread interface
void sthread_init(void) Initialize thread system(or manager) sthread_t sthread_create(sthread_start_func_t start_routine,void *arg,int joinable) Spawn a new thread The new thread runs start_routine with given arg void sthread_exit(void *ret) Exit caller thread with ret as exit value ret is actually return value of start_routine void sthread_yield(void) Yield execution of caller thread void *sthread_join(sthread_t t) Join thread t which is created as joinable Return value is the return value of start_routine of t Refer sthread.h

7 Sthread: mutex interface
sthread_mutex_t sthread_mutex_init() Create a new-unlocked mutex void sthread_mutex_free(sthread_mutex_t lock) Release resources held by given mutex void sthread_mutex_lock(sthread_mutex_t lock) Returned thread is guaranteed to acquire lock void sthread_mutex_unlock(sthread_mutex_t lock) Release lock Refer sthread.h

8 Sthread: condition variable
sthread_cond_t sthread_cond_init() Create a new condition variable void sthread_cond_free(sthread_cond_t cond) Release resources held by given condition variable void sthread_cond_signal(sthread_cond_t cond) Wake-up one waiting thread if there is any void sthread_cond_broadcast(sthread_cond_t cond) Wake-up all waiting threads void sthread_cond_wait(sthread_cond_t cond, sthread_mutex_t lock) Wait for given condition variable Returning thread is guaranteed to hold the lock Refer sthread.h

9 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); Output? (assume no preemption) From Section 3 – Winter 2006

10 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 From Section 3 – Winter 2006

11 How sthread_switch works
From Section 3 – Winter 2006 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…

12 Push old context CPU From Section 3 – Winter 2006 Thread 1 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

13 Save old stack pointer CPU From Section 3 – Winter 2006 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

14 Change stack pointers CPU From Section 3 – Winter 2006 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

15 Pop off new context CPU From Section 3 – Winter 2006 Thread 1 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

16 Done; return CPU What got switched? From Section 3 – Winter 2006
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

17 Adjusting the PC CPU From Section 3 – Winter 2006 Thread 1 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

18 Join: new from this quarter!
Wait until another thread terminates and get its return value Convenient way to synchronize threads Assumption A joinable thread is guaranteed to be joined by another thread (not threads!) Join can be called only one time at any time in the life-cycle of a joinable thread Behavior of multiple calls of join on a single joinable thread is undefined

19 Think about How do you start a new thread?
How do you pass an argument to the start function? How do you deal the main(or initial) thread? When & how do you reclaim resources of a terminated thread? Where does sthread_switch() return? Who should call sthread_switch() and when? What should be in struct _sthread(TCB)? How do you identify current thread? How do you block a thread? What should be in struct _sthread_mutex? What should be in struct _sthread_cond?

20 Hints Read project descriptions & given codes Design first Program
There are many hints already  pthread manual pages may be helpful Design first Answer questions in previous slides Write your algorithms and verify them Program Don’t forget to comment your code! Test, test, test, … Write your own test programs

21 CVS Make your project concurrent just like your programs 
Refer any CSE303 course material We have created UNIX groups for each team

22 Scenario Your team has two members UNIX group of your team is cse451x
dumb dumber UNIX group of your team is cse451x dumber is going to maintain the repository The name of repository is cse451cvs

23 How to set up? Log in as dumber Create a repository directory
% mkdir cse451cvs Adjust group % chgrp cse451x cse451cvs Adjust permission % chmod 2770 cse451cvs Initialize repository % cvs –d $HOME/cse451cvs init

24 Import source code Copy simplethreads-1.30.tar.gz Type following
% tar zxf simplethreads-1.30.tar.gz % cd simplethreads-1.30 % cvs –d $HOME/cse451cvs import –m “initial skeleton code” simplethreads SIMPLETHREADS SIMPLETHREADS_1_30 % cd .. % rm –rf simplethreads-1.30 Now you can check out using simplethreads as a module name

25 Set CVSROOT & CVS_RSH Now set CVSROOT environment variable
Bash export CVSROOT=/homes/iws/dumber/cse451cvs Csh/tcsh setenv CVSROOT /homes/iws/dumber/cse451cvs If you want to access the repository remotely, export CVS_RSH=ssh export setenv CVS_RSH ssh setenv CVSROOT To check your shell, % echo $SHELL


Download ppt "Welcome to the world of concurrency!"

Similar presentations


Ads by Google