Welcome to the world of concurrency!

Slides:



Advertisements
Similar presentations
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Advertisements

CSE 451: Operating Systems Section 6 Project 2b; Midterm Review.
CSE 451: Operating Systems
1 CSE451 Section 3. 2 Reminders Start project 2! It’s long Read the assignment carefully Read it again Project 2 will be done in groups of 3 Today: Form.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
CSE451 Section 3: Winter 2k7 Welcome to the world of concurrency! Kurtis Aaron Kimball
1 Reminders Start project 2! It’s long Read the assignment carefully Read it again Project 2 will be done in groups of 3 groups to me Part 1 due.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
1 Reminders Start project 2! It’s long Read the assignment carefully Read it again Make sure your groups are correct Today: Project 2 intro CVS.
Jonathan Walpole Computer Science Portland State University
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS333 Intro to Operating Systems Jonathan Walpole.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
CS140 Project 1: Threads Slides by Kiyoshi Shikuma.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.
CSE 451: Operating Systems Section 4 Scheduling, Project 2 Intro, Threads.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
CSE 451: Operating Systems Section 6 Project 2b. Midterm  Scores will be on Catalyst and midterms were handed back on Friday(?) in class  Talk to Ed,
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
CSE 451: Operating Systems Section 4 Project 2 Intro; Threads.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Realizing Concurrency using the thread model
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Process Tables; Threads
PROCESS MANAGEMENT IN MACH
CSE 451 Section 4 Project 1 Notes Looking ahead: Project 2.
Realizing Concurrency using the thread model
Threads in C Caryl Rahn.
Threads Threads.
CS399 New Beginnings Jonathan Walpole.
Multithreading Tutorial
Threading and Project 2 (Some slides taken from Sec. 3 Winter 2006)
Nachos Threads and Concurrency
Reminders Form groups of 3 by tomorrow Start project 2! Today:
Processes in Unix, Linux, and Windows
Realizing Concurrency using Posix Threads (pthreads)
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
Processes in Unix, Linux, and Windows
Process Tables; Threads
PTHREADS AND SEMAPHORES
Multithreading Tutorial
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
CS510 Operating System Foundations
Jonathan Walpole Computer Science Portland State University
Unix System Calls and Posix Threads
CSE451 – Section 6.
CSE 451 Autumn 2003 Section 3 October 16.
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Multithreading Tutorial
Programming with Shared Memory
Jonathan Walpole Computer Science Portland State University
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
Realizing Concurrency using the thread model
Programming with Shared Memory
Realizing Concurrency using Posix Threads (pthreads)
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Processes in Unix, Linux, and Windows
CS333 Intro to Operating Systems
CS703 – Advanced Operating Systems
Presentation transcript:

Welcome to the world of concurrency! CSE451 Section 4: Spring 2006 Welcome to the world of concurrency! AA: Kurtis Heimerl(kheimerl@cs) AB: YongChul Kwon(yongchul@cs)

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

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

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! 

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

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

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

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

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

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

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…

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

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

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

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

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

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

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

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?

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

CVS Make your project concurrent just like your programs  Refer any CSE303 course material http://www.cs.washington.edu/education/courses/cse303/05au/syllabus/11-14-files/ http://www.cs.washington.edu/education/courses/cse303/06wi/lec19.pdf We have created UNIX groups for each team http://cubist.cs.washington.edu/wiki/index.php/CSE_451%2C_Spring_2006_Project_2.0_Groups

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

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

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

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 CVSROOT=$USER@forkbomb.cs.washington.edu:/homes/iws/dumber/cse451cvs setenv CVS_RSH ssh setenv CVSROOT $USER@forkbomb.cs.washington.edu:/homes/iws/dumber/cse451cvs To check your shell, % echo $SHELL