CSE 451: Operating Systems Section 4 Project 2 Intro; Threads.

Slides:



Advertisements
Similar presentations
Threads. Readings r Silberschatz et al : Chapter 4.
Advertisements

Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
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 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.
Threads CSCI 444/544 Operating Systems Fall 2008.
Jonathan Walpole Computer Science Portland State University
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
Process Concept An operating system executes a variety of programs
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Scheduler Activations Jeff Chase. Threads in a Process Threads are useful at user-level – Parallelism, hide I/O latency, interactivity Option A (early.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
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.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Chapter 1 Process and Thread. 1.2 process The address space of a program – Text – Code – Stack – Heap A set of registers – PC – SP Other resources – Files.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
CS333 Intro to Operating Systems Jonathan Walpole.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
Department of Computer Science and Software Engineering
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
1 OS Review Processes and Threads Chi Zhang
CSE 451: Operating Systems Section 4 Scheduling, Project 2 Intro, Threads.
Threads A thread is an alternative model of program execution
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,
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Process Tables; Threads
CS 6560: Operating Systems Design
Realizing Concurrency using the thread model
Threads in C Caryl Rahn.
CS399 New Beginnings Jonathan Walpole.
Threading and Project 2 (Some slides taken from Sec. 3 Winter 2006)
Threads & multithreading
Reminders Form groups of 3 by tomorrow Start project 2! Today:
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Realizing Concurrency using the thread model
Process Tables; Threads
CS510 Operating System Foundations
Jonathan Walpole Computer Science Portland State University
Unix System Calls and Posix Threads
Operating System Concepts
CSE 451 Autumn 2003 Section 3 October 16.
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Jonathan Walpole Computer Science Portland State University
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
CS333 Intro to Operating Systems
Welcome to the world of concurrency!
Presentation transcript:

CSE 451: Operating Systems Section 4 Project 2 Intro; Threads

Project 1  Congratulations, you’re all kernel hackers now!  We’re going to give you a break and have you do some userspace work 4/19/122

Project 2: user-level threads  Part A: due Wednesday, May 2 at 11:59pm  Implement part of a user thread library  Add synchronization primitives  Solve a synchronization problem  Part B: due Friday, May 18 at 11:59pm  Implement a multithreaded web server  Add preemption  Get some results and write a (small) report 34/19/12

Project 2 notes  Start EARLY!  It’s loooooooong  Read the assignment carefully  Read it again  Understand the skeleton code  Use the same groups as for project 1 44/19/12

Project 2 tips  Understand what the provided code does for you  Division of work  Part 3 can be completed without parts 1 and 2  More tools  ddd  (Or just gdb if you’re not a fan of GUIs) 54/19/12

6 Simplethreads  We give you:  Skeleton functions for thread interface  Machine-specific code (x86, i386, PowerPC)  Support for creating new stacks  Support for saving regs/switching stacks  A queue data structure (why?)  Very simple test programs  You should write more, and include them in the turnin  A single-threaded web server 4/19/12

7 Simplethreads code structure include/sthread.h Other appsWeb server (web/sioux.c) test/*.c lib/sthread_user.h lib/sthread_user.c lib/sthread_ctx.c lib/sthread_ctx.h You write this sthread_switch_i386.h sthread_switch_powerpc.h lib/sthread_switch.S lib/sthread_queue.c lib/sthread_queue.h lib/sthread_preempt.c lib/sthread_preempt.h 4/19/12

8 Pthreads  Pthreads (POSIX threads) is a preemptive, kernel-level thread library  Simplethreads is similar to Pthreads  Project 2: compare your implementation against Pthreads ./configure --with-pthreads 4/19/12

9 Thread operations  What functions do we need for a userspace thread library? 4/19/12

10 Simplethreads API 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 void* sthread_join(sthread_t t)  Wait for specified thread to exit 4/19/12

11 Simplethreads internals  Structure of the TCB: struct _sthread { sthread_ctx_t *saved_ctx; /** * Add your fields to the thread * data structure here. */ }; 4/19/12

12 Sample multithreaded program  (this slide and next – see test-create.c) void *thread_start(void *arg) { if (arg) { printf(“in thread_start, arg = %p\n”, arg); } return 0; }... 4/19/12

13 Sample multithreaded program int main(int argc, char *argv[]) { sthread_init(); for(i = 0; i < 3; i++) { if (sthread_create(thread_start, (void *)&i) == NULL) { printf("sthread_create failed\n"); exit(1); } // needs to be called multiple times sthread_yield(); printf("back in main\n"); return 0; } 4/19/12

14 Managing contexts  (Provided for you in project 2)  Thread context = thread stack + stack pointer sthread_new_ctx(func_to_run)  creates 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 4/19/12

15 How sthread_switch works Xsthread_switch: (push all regs) movq %rsp,(%rax) movq %rdx,%rsp (pop all regs) ret Thread 1 TCB … SP Thread 2 TCB … SP RSP CPU Thread 1 runningThread 2 ready Want to switch to thread 2… Thread 2 registers Thread 1 regs 4/19/12

16 Push old context Xsthread_switch: (push all regs) movq %rsp,(%rax) movq %rdx,%rsp (pop all regs) ret Thread 1 TCB … SP Thread 2 TCB … SP RSP CPU Thread 1 runningThread 2 ready Thread 2 registers Thread 1 registers Thread 1 regs 4/19/12

17 Save old stack pointer Xsthread_switch: (push all regs) movq %rsp,(%rax) movq %rdx,%rsp (pop all regs) ret Thread 1 TCB … SP Thread 2 TCB … SP RSP CPU Thread 1 runningThread 2 ready Thread 2 registers Thread 1 registers Thread 1 regs 4/19/12

18 Change stack pointers Xsthread_switch: (push all regs) movq %rsp,(%rax) movq %rdx,%rsp (pop all regs) ret Thread 1 TCB … SP Thread 2 TCB … SP RSP CPU Thread 1 readyThread 2 running Thread 2 registers Thread 1 registers Thread 1 regs 4/19/12

19 Pop off new context Xsthread_switch: (push all regs) movq %rsp,(%rax) movq %rdx,%rsp (pop all regs) ret Thread 1 TCB … SP Thread 2 TCB … SP RSP CPU Thread 1 readyThread 2 running Thread 1 registers Thread 2 regs 4/19/12

20 Done; return Xsthread_switch: (push all regs) movq %rsp,(%rax) movq %rdx,%rsp (pop all regs) ret Thread 1 TCB … SP Thread 2 TCB … SP RSP CPU Thread 1 readyThread 2 running Thread 1 registers What got switched? RSP PC (how?) Other registers Thread 2 regs 4/19/12

Adjusting the PC Thread 1 TCB … SP Thread 2 TCB … SP RSP CPU Thread 2 (running): sthread_switch(t2,...); 0x800: printf(“test 2”); Thread 1 registers ret pops off the new return address! ra=0x800 PC Thread 1 (stopped): sthread_switch(t1,t2); 0x400: printf(“test 1”); ra=0x400 21

22 Thread joining  With Pthreads (and Sthreads):  Master thread calls join on worker thread  Join blocks until worker thread exits.  Join returns the return value of the worker thread. 4/19/12

23 The need for synchronization  Thread safety:  An application's ability to execute multiple threads simultaneously without "clobbering" shared data or creating "race" conditions 4/19/12

24 Synchronization primitives: mutexes sthread_mutex_t sthread_mutex_init() void sthread_mutex_free(sthread_mutex_t lock) void sthread_mutex_lock(sthread_mutex_t lock)  When returns, thread is guaranteed to acquire lock void sthread_mutex_unlock( sthread_mutex_t lock) 4/19/12

25 Synchronization primitives: condition variables sthread_cond_t sthread_cond_init() void sthread_cond_free(sthread_cond_t cond) void sthread_cond_signal(sthread_cond_t cond)  Wake-up one waiting thread, if any void sthread_cond_broadcast( sthread_cond_t cond)  Wake-up all waiting threads, if any 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 4/19/12

26 Things to think about  How do you create a thread?  How do you pass arguments to the thread’s start function?  Function pointer passed to sthread_new_ctx() doesn’t take any arguments  How do you deal with the initial (main) thread?  How do you block a thread? 4/19/12

27 Things to think about  When and how do you reclaim resources for a terminated thread?  Can a thread free its stack itself?  Where does sthread_switch return?  Who and when should call sthread_switch?  What should be in struct _sthread_mutex, struct _sthread_cond? 4/19/12

28 Things to think about  Working with synchronization: When does it make sense to disable interrupts?  Which actions are atomic at the application level versus at the thread level?  When using forkbomb, run “ulimit -Su 64” to limit the number of processes/threads  Allows you to log in from another session even if you hit the above limit  Add it to your.bash_profile so it happens automatically 4/19/12

294/19/12