Lecture 20 Threads Richard Gesick. Threads Makes use of multiple processors/cores if available Share memory within the program (as opposed to processes.

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
Topic : Process Management Lecture By: Rupinder Kaur Lecturer IT, SRS Govt. Polytechnic College for Girls,Ludhiana.
The Process Control Block From: A Process Control Block (PCB, also called Task Control Block or Task Struct) is.
Processes CSCI 444/544 Operating Systems Fall 2008.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
Operating Systems Lecture # 3. Recap Hardware Operating System Application System Call Trap Hardware Trap Processor.
Real-Time Kernels and Operating Systems. Operating System: Software that coordinates multiple tasks in processor, including peripheral interfacing Types.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Process Description and Control A process is sometimes called a task, it is a program in execution.
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.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Intro to OS CUCS Mossé Processes and Threads What is a process? What is a thread? What types? A program has one or more locus of execution. Each execution.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Lecture 2 Foundations and Definitions Processes/Threads.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
CE Operating Systems Lecture 11 Windows – Object manager and process management.
E X C E E D I N G E X P E C T A T I O N S OP SYS Linux System Administration Dr. Hoganson Kennesaw State University Operating Systems Functions of an operating.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Multiprogramming. Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
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.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Department of Computer Science and Software Engineering
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
COT 4600 Operating Systems Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 3:00-4:00 PM.
Multithreading The objectives of this chapter are: To understand the purpose of multithreading To describe Java's multithreading mechanism.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Processes 2 Introduction to Operating Systems: Module 4.
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
Demonstrating Multithreading Concepts. Overvie w Multithreading or free-threading is the ability of an operating system to concurrently run programs that.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
Multiprogramming. Readings r Chapter 2.1 of the textbook.
Threads & Multithreading
Processes and threads.
Process Tables; Threads
Process concept.
Process Management Process Concept Why only the global variables?
Chapter 3: Process Concept
PROCESS MANAGEMENT IN MACH
Multi Threading.
Java Multithreading.
OPERATING SYSTEMS CS3502 Fall 2017
Lecture 7 Processes and Threads.
CPU Scheduling.
Process management Information maintained by OS for process management
Lecture 2: Processes Part 1
More examples How many processes does this piece of code create?
Process Tables; Threads
Processes and Threads.
Threads and Concurrency
Threads and Concurrency
Chapter 3: Processes.
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Multithreading in java.
CS510 Operating System Foundations
Foundations and Definitions
Chapter 6: Scheduling Algorithms Dr. Amjad Ali
Lecture 19 Threads CSE /6/2019.
CMSC 202 Threads.
CS Introduction to Operating Systems
Presentation transcript:

Lecture 20 Threads Richard Gesick

Threads Makes use of multiple processors/cores if available Share memory within the program (as opposed to processes which are separate) Must coordinate access to shared memory 14-2

THREADS IN THE OS Operating systems use processes to separate the different applications that they are executing. Threads are the basic unit to which an operating system allocates processor time, and more than one thread can be executing code inside that process. Each thread maintains exception handlers, a scheduling priority, and a set of structures the system uses to save the thread context until it is scheduled. The thread context includes all the information the thread needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process. 1-3

Threads in the OS An operating system that supports preemptive multitasking creates the effect of simultaneous execution of multiple threads from multiple processes. It does this by dividing the available processor time among the threads that need it, allocating a processor time slice to each thread one after another. The currently executing thread is suspended when its time slice elapses, and another thread resumes running. When the system switches from one thread to another, it saves the thread context of the preempted thread and reloads the saved thread context of the next thread in the thread queue. The length of the time slice depends on the operating system and the processor. Because each time slice is small, multiple threads appear to be executing at the same time, even if there is only one processor. This is actually the case on multiprocessor systems, where the executable threads are distributed among the available processors. 1-4

Threads Easy to make a separate worker (thread) within a larger program: Define worker function/method Create thread and tell it what method to run when it's started Run/start thread 14-5

Threads There is a problem with this, though, in that the method that the thread runs does not take in any parameters. To solve this, use a class: Define a class that takes in any needed parameters in its constructor Store the values into object-level attributes The last thing the constructor does is create a thread and run it (invoking an internal method of the class) This worker thread then has access to all the attributes 14-6

Example class Program { static void Main(string[] args) { // Bring to life 10 threaded workers and let the run! for (int i = 0; i < 10; i++) { ThreadedWorker tr = new ThreadedWorker(i); } 14-7

The worker public class ThreadedWorker { // A unique ID to identify this thread (for printing) int ID; // The thread Thread t; // Constructor public ThreadedWorker(int ID) { this.ID = ID; // Bring the thread to life t = new Thread(new ThreadStart(doWork)); t.Start(); } 14-8

do work // The method that we're going to run for the thread void doWork() { for (int i = 0; i < 10; i++) { Console.WriteLine("Thread " + ID + " is running"); } Console.WriteLine("Thread "+ID+" is finished"); } 1-9

Simple "Cornflower Blue" XNA Game 14-10

After adding worker threads to the game 14-11