CS415 Minithreads Project 2 Context Switch and Stack Initialization Ken Hopkinson

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

Chapter 3 Process Description and Control
Computer Architecture CSCE 350
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
CS414 Minithreads project overview
Processes CSCI 444/544 Operating Systems Fall 2008.
Threads - Definition - Advantages using Threads - User and Kernel Threads - Multithreading Models - Java and Solaris Threads - Examples - Definition -
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
CS533 Concepts of Operating Systems Class 3 Integrated Task and Stack Management.
CS414 Minithreads Project 1 Overview Adrian Bozdog (Adi)
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
Process Concept An operating system executes a variety of programs
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
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.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
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.
CS 153 Design of Operating Systems Spring 2015
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
CS1550 Assignment 5 Multiprogramming Implementation notes Matt Craven.
Threads, Thread management & Resource Management.
Scheduler Activations: Effective Kernel Support for the User- Level Management of Parallelism. Thomas E. Anderson, Brian N. Bershad, Edward D. Lazowska,
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Operating Systems CMPSC 473 Lecture 8: Threads September Instructor: Bhuvan Urgaonkar.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
ITFN 3601 Introduction to Operating Systems Lecture 3 Processes, Threads & Scheduling Intro.
CSE 451: Operating Systems Winter 2015 Module 5 1 / 2 User-Level Threads & Scheduler Activations Mark Zbikowski 476 Allen Center.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Lecture 5 Page 1 CS 111 Online Processes CS 111 On-Line MS Program Operating Systems Peter Reiher.
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.
Processes and Virtual Memory
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Processes & Threads Introduction to Operating Systems: Module 5.
Cs431-cotter1 Processes and Threads Tanenbaum 2.1, 2.2 Crowley Chapters 3, 5 Stallings Chapter 3, 4 Silberschaz & Galvin 3, 4.
What is a Process ? A program in execution.
Unit 4: Processes, Threads & Deadlocks June 2012 Kaplan University 1.
CS415 Project 1: Understanding Setjmp/Longjmp Manpreet Singh
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
Embedded Real-Time Systems
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Scheduler activations Landon Cox March 23, What is a process? Informal A program in execution Running code + things it can read/write Process ≠
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Threads & Multithreading
Processes and threads.
CS 6560: Operating Systems Design
Scheduler activations
Threads & multithreading
Operating System Concepts
More examples How many processes does this piece of code create?
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Lecture Topics: 11/1 General Operating System Concepts Processes
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
CS510 Operating System Foundations
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
CS703 – Advanced Operating Systems
Threads CSE 2431: Introduction to Operating Systems
CS Introduction to Operating Systems
Presentation transcript:

CS415 Minithreads Project 2 Context Switch and Stack Initialization Ken Hopkinson

CS415 Project 2: Context Switching and Stack Initialization 2 What You Have to Do  Lay the foundation for Minithreads, a user-level threads package, on Windows NT  In this assignment we will be creating code for context switching and stack initialization

CS415 Project 2: Context Switching and Stack Initialization 3 What we’ll cover  What order to do things in  How context switching works  How yielding between threads works  Minithread implementation hints

CS415 Project 2: Context Switching and Stack Initialization 4 Thread Review  A process is a task plus at least one thread  A task consists of a code section, data section, and a set of operating system resources  A thread is basic unit of CPU utilization consisting of a program counter, a register set, and a stack space  These definitions can be found in sections 4 and 5 of your book where they are explained in more detail

CS415 Project 2: Context Switching and Stack Initialization 5 Threads Continued  Threads can be based either at the user- or kernel- level  Kernel-level threads are scheduled by the Kernel (advantage), but require protected operating system actions to switch contexts (disadvantage)  User-level threads are managed by the threads’ parent process. Context switches are very fast and do not involve the operating system (advantage). The operating system only sees processes, not threads, so scheduling can be unfair and system calls from one thread will block all threads associated with its parent process as well. (disadvantage)

CS415 Project 2: Context Switching and Stack Initialization 6 Minithreads Project 2 Part A  Windows NT lacks support for user-level threads  We will create our own user-level thread package, called Minithreads, in this course  In Project 1, you dissected setjmp/longjmp to learn how to change a program’s program counter, register set, and stack position (the components of a thread)  In Project 2 we will use this knowledge to implement context switching. We will also create multiple stacks and learn how to switch between them.

CS415 Project 2: Context Switching and Stack Initialization 7 Project 2, step-by-step  Implement stack initialization  Implement context switching  Use test code to be sure you have done your work correctly  Note: Your responsibilities are limited to the minithread.h and minithread.c files. Everything else is there so that you can test your code.

CS415 Project 2: Context Switching and Stack Initialization 8 Threads and their stacks  NT gives you an initial stack  Subsequent stacks must be allocated on the process’s heap using malloc code stack heap 50000

CS415 Project 2: Context Switching and Stack Initialization 9 Stack Space Alignment  Stack space needs to be aligned on 32-bit word boundaries  malloc allocates space properly aligned at its starting address  The ending allocated address is not necessarily aligned properly  Be sure to set the top of the stack to an aligned location at or below the end of the dynamically allocated stack space  Note: CS, DS, and SS are all initialized to the same value in VC++ so you do not need to worry about them even though malloc works from DS while stacks are in SS’s domain.

CS415 Project 2: Context Switching and Stack Initialization 10 Stack Routines  allocate_stack(stack_pointer_t *stackbase, stack_pointer_t *stacktop): Allocate a new stack space of size STACK_SIZE.  stackbase points to the low end of the stack address range.  stacktop is the highest aligned address in the stack address range.  minithread_free_stack(stack_pointer_t *stackbase): Free an allocated stack  minithread_initialize_stack has been implemented for you

CS415 Project 2: Context Switching and Stack Initialization 11 An initialized stack base_arg1 final_arg1 final_arg2 base_arg2 final_proc addr stack_top stack_base (lowest memory address) Stack initialized to look as if minithread_switch had been called final_proc addr original stack_top (highest memory address)

CS415 Project 2: Context Switching and Stack Initialization 12 Stack Initialization Limitations  Our context switching operates similarly to a setjmp followed by a longjmp  When a return occurs, the final_proc address is popped off of the stack but there is no restoration to the next frame  To do so, we would need a third procedure (or a modified final_proc) to restore stack space appropriately  It is not important to do so in Project2 since the final procedure there uses no arguments

CS415 Project 2: Context Switching and Stack Initialization 13 Threads  minithread_type: Includes a stack base, stack top, and process context (to be filled in by you)  minithread_t *allocate_minithread(): Allocate a new minithread and return it  minithread_initialize_thread(minithread_t *thread, proc_t *base_proc): Initialize a new thread using the current register set for the register values. Exceptions: eip = the base_proc address. esp, ebp need to be set according to the new thread’s initialized stack

CS415 Project 2: Context Switching and Stack Initialization 14 Context switching  minithread_switch(old_thread, new_thread)  Swaps execution contexts with a thread from the run queue  registers  program counter  stack pointer

CS415 Project 2: Context Switching and Stack Initialization 15 Context switching old_thread_sp_ptrnew_thread_sp_ptr ESP ? old thread TCBnew thread TCB

CS415 Project 2: Context Switching and Stack Initialization 16 Save old context old_thread_sp_ptrnew_thread_sp_ptr ESP ? old thread TCBnew thread TCB

CS415 Project 2: Context Switching and Stack Initialization 17 Change stack pointers old_thread_sp_ptrnew_thread_sp_ptr ESP old thread TCBnew thread TCB

CS415 Project 2: Context Switching and Stack Initialization 18 Restore new context old_thread_sp_ptrnew_thread_sp_ptr ESP old thread TCBnew thread TCB

CS415 Project 2: Context Switching and Stack Initialization 19 Project 2 Part B  We briefly discussed Purify in the last lecture  It is a great tool for memory debugging effectively adding many of the valuable memory safety and management routines that Java provides  In Part B of Project 2, you will get some hands-on experience using Purify

CS415 Project 2: Context Switching and Stack Initialization 20 Building the Purify.exe File  Get the purify.zip archive from the course web page  Place the buggy.c and purify.mak files in the same directory  Open buggy.c up and look at what it does  Type nmake /f purify.mak in that directory to build the purify.exe executable

CS415 Project 2: Context Switching and Stack Initialization 21 Purifying Buggy.C  The buggy.c file has a number of memory problems  To find them, open Purify from the Rational Development Studio 1.5 section of the Programs menu on any of the Windows NT Machines in the Undergraduate Lab  Choose Run from the Purify menu setting the working directory and program executable appropriately when prompted

CS415 Project 2: Context Switching and Stack Initialization 22 Purify’s Opening Screen

CS415 Project 2: Context Switching and Stack Initialization 23 Sample Purify Output

CS415 Project 2: Context Switching and Stack Initialization 24 Part B Continued  Purify will begin by instrumenting the purify.exe executable  Purify will continue by running purify.exe noting any memory errors and warnings that it might find  In Part B, you are asked to answer questions based on the Purify output

CS415 Project 2: Context Switching and Stack Initialization 25 Summary  Implement context switching  Implement stack initialization code  Create code to allocate and initialize three stacks, start threads of execution, and swap between them  Explore the Purify environment to help make life easier in future projects in Project 2 Part B  In Project 3, you will be building on Project 2 to create a simple (non-preemptive) user-level threads package