CS414 Minithreads project overview

Slides:



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

CSE 451: Operating Systems
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Processes CSCI 444/544 Operating Systems Fall 2008.
Scheduler Activations Effective Kernel Support for the User-Level Management of Parallelism.
Threads vs. Processes April 7, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Operating Systems Lecture # 3. Recap Hardware Operating System Application System Call Trap Hardware Trap Processor.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
CS415 Minithreads Project 2 Context Switch and Stack Initialization Ken Hopkinson
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
CS414 Minithreads Project 1 Overview Adrian Bozdog (Adi)
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Operating Systems (CSCI2413) Lecture 3 Processes phones off (please)
Project Part I Background Tom Roeder CS sp.
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Operating Systems CSE 411 CPU Management Sept Lecture 11 Instructor: Bhuvan Urgaonkar.
Threads & Scheduling What are threads?
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Operating Systems Chapter 5 Threads. Benefits Responsiveness Resource Sharing Economy Utilization of MP Architectures.
Introduction to Processes CS Intoduction to Operating Systems.
Multiprogramming CSE451 Andrew Whitaker. Overview Multiprogramming: Running multiple programs “at the same time”  Requires multiplexing (sharing) the.
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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
Operating Systems CMPSC 473 Lecture 8: Threads September Instructor: Bhuvan Urgaonkar.
CS333 Intro to Operating Systems Jonathan Walpole.
CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.
CSE 451: Operating Systems Winter 2015 Module 5 1 / 2 User-Level Threads & Scheduler Activations Mark Zbikowski 476 Allen Center.
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.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Department of Computer Science and Software Engineering
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Processes and Virtual Memory
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
1 OS Review Processes and Threads Chi Zhang
1 Lecture 6 “Nachos” n nachos overview n directory structure n nachos emulated machine n nachos OS n nachos scheduler n nachos 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.
Threads & Multithreading
Process Tables; Threads
PROCESS MANAGEMENT IN MACH
Realizing Concurrency using the thread model
CS399 New Beginnings Jonathan Walpole.
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Process Tables; Threads
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Realizing Concurrency using the thread model
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Threads vs. Processes Hank Levy 1.
CS703 – Advanced Operating Systems
CS Introduction to Operating Systems
Presentation transcript:

CS414 Minithreads project overview Alin Dobra dobra@cs.cornell.edu

What you have to do Implement Minithreads, a user-level threads package on Windows NT Includes semaphores and queues Non-preemptive The hardest parts (context switching, stack initialisation) are done for you For the adventurous: add preemption

What we’ll cover What order to do things in How context switching works How yielding between threads works Minithread implementation hints

Minithreads structure interrupts.h machineprimitives_x86.c interrupts.c machineprimitives.h synch.h queue.h machineprimitives.c synch.c queue.c minithread.h minithread.c

Minithreads, step-by-step Implement queues Define struct minithread Implement minithreads operations fork and yield system initialisation termination and cleanup start, stop Implement semaphores

Queue alternatives Implement by using enclosing structs: head tail Or implement by using pointers in element types: head tail

Defining a minithread What’s in a struct minithread (thread control block)? stack top pointer stack base pointer numerical identifier (int) thread status anything else you think necessary

Minithread operations minithread_t minithread_fork(proc, arg) create thread and make it runnable minithread_t minithread_create(proc, arg) create a thread but don’t make it runnable void minithread_yield() stop this thread and run a new one from the run queue (make the scheduling decisions here) void minithread_start(minithread_t t) void minithread_stop() start another thread, stop yourself

Threads and their stacks NT gives you an initial stack Subsequent minithread stacks are allocated on the process’s heap using malloc code 2000 heap stack 50000

Context switching minithread_switch(old_thread_sp_ptr, new_thread_sp_ptr) is provided Swap execution contexts with a thread from the run queue registers program counter stack pointer

Context switching old thread TCB new thread TCB ? ESP old_thread_sp_ptr ? new_thread_sp_ptr new thread’s registers ESP

Push on old context old thread TCB new thread TCB ? ESP old_thread_sp_ptr ? new_thread_sp_ptr old thread’s registers new thread’s registers ESP

Change stack pointers old thread TCB new thread TCB ESP old_thread_sp_ptr new_thread_sp_ptr old thread’s registers new thread’s registers ESP

Pop off new context old thread TCB new thread TCB ESP old_thread_sp_ptr new_thread_sp_ptr old thread’s registers ESP

Thread yield Use minithread_switch to implement minithread_yield What does a yield do? Where does a yielding thread return to when it’s rescheduled?

Thread yield ESP RUNNING STOPPED thread 1 thread 2 registers void thread1_proc() { printf("Running thread 1\n"); minithread_yield(); 0x40164 printf("Running thread 1\n"); ... } 0x85522 thread 1 thread 2 RUNNING STOPPED

Push return address and call yield ESP registers void thread1_proc() { printf("Running thread 1\n"); minithread_yield(); 0x40164 printf("Running thread 1\n"); ... } 0x85522 0x40164 thread 1 thread 2 RUNNING STOPPED

Switch to new thread ESP STOPPED RUNNING thread 1 thread 2 void thread2_proc() { int x; for (;;) { minithread_yield(); 0x85522 printf("x is now %d.\n", x++); } 0x85522 registers 0x40164 thread 1 thread 2 STOPPED RUNNING

Return from yield into new context ESP void thread2_proc() { int x; for (;;) { minithread_yield(); 0x85522 printf("x is now %d.\n", x++); } registers 0x40164 thread 1 thread 2 STOPPED RUNNING

Implementation details How do we switch to a newly-created thread? Where do the stacks come from? How do we create a thread? How do we initialise the system?

Minithread creation Two methods to choose from proc is a proc_t minithread_create(proc, arg) minithread_fork(proc, arg) proc is a proc_t typedef int (*proc_t)(arg_t) e.g. int run_this_proc(int* x) could cast any pointer to (int *)

Minithread creation Allocate a struct minithread (TCB) Allocate and initialise a new stack minithread_allocate_stack(stackbase, stacktop) minithread_initialize_stack(stacktop, body_proc, body_arg, final_proc, final_arg) Set the initial thread status Whatever else is appropriate

An initialised stack Stack must look as though minithread_switch has been called root_proc addr body_arg stack_top body_proc addr final_arg stack_base final_proc addr

How a new thread starts root_proc is popped off the stack after “return” from minithread_switch It runs body_proc(body_arg) final_proc(final_arg) To execute the user-provided function and allow thread cleanup root_proc doesn’t return

When your program starts NT has kernel threads When your program starts: one kernel thread of control NT-provided execution stack code 2000 heap stack 50000

Code example int proc(int* arg) { printf("Hello, world!\n"); return 0; } main() { minithread_system_initialize(proc, NULL);

Initialising the system minithreads_system_initialize (proc_t mainproc,arg_t mainarg) Starts up the system First user thread runs mainproc(mainarg) Should the initial minithread be the same as the kernel thread?

Initialising the system If main() returns, it will terminate the entire process! Make minithread_system_init() not return It should create the first user thread, which runs mainproc(mainarg) Your kernel thread needn’t terminate

Cleaning up threads A minithread terminates when it reaches the end of its body_proc A good solution will clean up its stack: minithread_free_stack(stackbase) But a thread can’t destroy its stack itself: minithread_switch won’t work!

Minithread destruction A terminating thread T runs its final_proc “notifies the system that it wants to finish” relinquishes the processor Some other thread sees T needs to be cleaned up frees T ’s stack, etc

A word of warning The NT kernel thread has a stack You can make a minithread out of it But you can’t free the NT stack!

Summary Implement queues Fork, initialisation, yield for 1 thread Yielding between multiple threads Thread cleanup Semaphores Implement “food services” problem concurrently if you like, or later