Implementation Alternatives. Lecture 2: Implementation Alternatives & Concurrent Programming. Current Implementation Alternatives

Similar documents
Concurrent Programming. Implementation Alternatives. Content. Real-Time Systems, Lecture 2. Historical Implementation Alternatives.

Concurrent Programming

Interrupts and Time. Real-Time Systems, Lecture 5. Martina Maggio 28 January Lund University, Department of Automatic Control

Interrupts and Time. Interrupts. Content. Real-Time Systems, Lecture 5. External Communication. Interrupts. Interrupts

CS370 Operating Systems

OPERATING SYSTEMS CS3502 Spring Processor Scheduling. Chapter 5

CS370 Operating Systems

Chapter 6: CPU Scheduling

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

CPU Scheduling: Objectives

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

Scheduling in the Supermarket

CS370 Operating Systems

SMD149 - Operating Systems

Lecture Topics. Announcements. Today: Uniprocessor Scheduling (Stallings, chapter ) Next: Advanced Scheduling (Stallings, chapter

Implementing Scheduling Algorithms. Real-Time and Embedded Systems (M) Lecture 9

Class average is Undergraduates are performing better. Working with low-level microcontroller timers

Chapter 5 CPU scheduling

CPU Scheduling. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

Scheduling - Overview

CSCI-GA Operating Systems Lecture 3: Processes and Threads -Part 2 Scheduling Hubertus Franke

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

CHAPTER 2: PROCESS MANAGEMENT

PROCESS SCHEDULING II. CS124 Operating Systems Fall , Lecture 13

INF1060: Introduction to Operating Systems and Data Communication. Pål Halvorsen. Wednesday, September 29, 2010

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

What s An OS? Cyclic Executive. Interrupts. Advantages Simple implementation Low overhead Very predictable

NuttX Realtime Programming

Embedded Systems: OS. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Last Class: CPU Scheduling. Pre-emptive versus non-preemptive schedulers Goals for Scheduling: CS377: Operating Systems.

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

Chapter 5: CPU Scheduling

Chapter 5: CPU Scheduling. Operating System Concepts Essentials 8 th Edition

Processes and Non-Preemptive Scheduling. Otto J. Anshus

CIS233J Java Programming II. Threads

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

Embedded Systems: OS

CPU Scheduling. Daniel Mosse. (Most slides are from Sherif Khattab and Silberschatz, Galvin and Gagne 2013)

Subject Name: OPERATING SYSTEMS. Subject Code: 10EC65. Prepared By: Kala H S and Remya R. Department: ECE. Date:

Chapter 5: CPU Scheduling

238P: Operating Systems. Lecture 14: Process scheduling

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

Chapter 19: Real-Time Systems. Operating System Concepts 8 th Edition,

Concurrent Programming

Multiprocessor and Real-Time Scheduling. Chapter 10

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Virtual Machine Design

Uniprocessor Scheduling. Basic Concepts Scheduling Criteria Scheduling Algorithms. Three level scheduling

CS370 Operating Systems

Introduction. CS3026 Operating Systems Lecture 01

Comparison of soft real-time CPU scheduling in Linux kernel 2.6 series with Solaris 10

Lecture: Embedded Software Architectures

Java Threads. COMP 585 Noteset #2 1

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CSE 120 Principles of Operating Systems

LECTURE 3:CPU SCHEDULING


CS 326: Operating Systems. CPU Scheduling. Lecture 6

THREADS ADMINISTRIVIA RECAP ALTERNATIVE 2 EXERCISES PAPER READING MICHAEL ROITZSCH 2

EECS 750: Advanced Operating Systems. 01/29 /2014 Heechul Yun

Lecture 7: Process & Thread Introduction

TDIU25: Operating Systems II. Processes, Threads and Scheduling

Process management. Scheduling

Object Oriented Programming. Week 10 Part 1 Threads

Processes and Threads. Processes: Review

Recap. Run to completion in order of arrival Pros: simple, low overhead, good for batch jobs Cons: short jobs can stuck behind the long ones

Lecture Topics. Announcements. Today: Advanced Scheduling (Stallings, chapter ) Next: Deadlock (Stallings, chapter

Process Description and Control

Lecture 2 Process Management

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

Announcements/Reminders

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

Chapter 3 Process Description and Control

Threads. Computer Systems. 5/12/2009 cse threads Perkins, DW Johnson and University of Washington 1

ò Paper reading assigned for next Tuesday ò Understand low-level building blocks of a scheduler User Kernel ò Understand competing policy goals

Threads. Raju Pandey Department of Computer Sciences University of California, Davis Spring 2011

Multiprocessor and Real- Time Scheduling. Chapter 10

Main Points of the Computer Organization and System Software Module

CISC 7310X. C05: CPU Scheduling. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 3/1/2018 CUNY Brooklyn College

Threads and Concurrency

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

Process- Concept &Process Scheduling OPERATING SYSTEMS

Threads and Concurrency

Threads SPL/2010 SPL/20 1

Lesson 6: Process Synchronization

Chapter 4: Threads. Chapter 4: Threads

Operating Systems CS 323 Ms. Ines Abbes

Chapter 5: CPU Scheduling

CPU Scheduling. The scheduling problem: When do we make decision? - Have K jobs ready to run - Have N 1 CPUs - Which jobs to assign to which CPU(s)

Operating Systems. Lecture Course in Autumn Term 2015 University of Birmingham. Eike Ritter. September 22, 2015

High level scheduling: Medium level scheduling: Low level scheduling. Scheduling 0 : Levels

CSE 120 Principles of Operating Systems

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Scheduling Mar. 19, 2018

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

Programming Languages for Real-Time Systems. LS 12, TU Dortmund

CS370 Operating Systems

Transcription:

Lecture 2: Implementation Alternatives & Concurrent ming Implementation Alternatives Controllers can be implemented using a number of techniques [RTCS Ch 3] Implementation Alternatives Processes and threads es Internal representation Several of these are rarely used any longer in industrial practice, eg mechanical techniques discrete analog electronics discrete digital electronics 1 2 Current Implementation Alternatives General purpose processors Performance, Power efficiency Special purpose processors Micro controllers Digital Signal Processors (DSP) mable Hardware Field mable Gate Arrays (FPGA) Flexibility Application Specific Integrated Circuits (ASIC) System on a Chip (SoC) 3 4 Small Microcontrollers Large Microcontrollers Limited program memory (eg 8-16 kbyte) Limited data memory (eg 1-4 kbyte) Processor: Main program + interrupt handlers (Periodic) timers Periodic controllers implemented in interrupt handlers associated with periodic timers Only a limited number of timers Large amount of memory on chip External memory Processor: Real-time kernel supporting multiple threads, ie, concurrent programming 5 6

Multicores Several processors (cores) on the same chip Multicore typically 2-16 cores Manycore > 16 cores Shared main memory Shared or separate caches Concurrent ming 1 Multiprogramming: the processes multiplex their execution on a single CPU 2 Multiprocessing: the processes multiplex their execution on a multiprocessor system with tightly coupled processors, eg, a multi-core platform 3 Distributed processing: the processes multiplex their execution on several CPUS connected through a network True parallelism: (2) and (3) Logical parallelism (pseudo-parallelism): (1) the main topic 7 8 A B C A B C Process Process Logical concurrency Context Switch Time Approaches Real-Time Operating System (RTOS) sequential language (C) with real-time primitives real-time kernel for process handling Real-time programming language (eg Ada) the language itself or the run-time system provides the functionality of a real-time kernel Time sharing concurrency Time 9 10 Processes may have shared address space Memory models thread (sometimes also called light-weight process) shared variables and code process = procedure (Modula 2) or run method (Java) used in the course separate address space independent of each other cp Windows message-passing 11 Processes vs threads A thread (light-weight process) resides within an ordinary process and shares it address space The scheduler operates on threads Windows, Solaris Threads Process 1 r Process 2 Process 3 In the course we will only deal with threads However, for historical reasons we, incorrectly, call the threads for processes (not unusual) 12

Processes and Threads in Linux Linux does not differentiate between processes and threads All threads are implemented as processes A thread is simply a process that shares certain resources, eg, memory, with other processes Coroutines A mechanism for expressing concurrent execution A procedure (subroutine) that, instead of returning, resumes another coroutine The context (local data) of the coroutines remains in between invocations Coroutine A; Coroutine B; begin 1 Resume(B); 5 Resume(B); 9 end A; begin 2 3 4 Resume(A); 6 7 8 Resume(A); end B; 13 14 Internal Process States Context Switches Ready waiting event Blocked Ready The process is ready to execute The process is currently executing Blocked The process is waiting for an event A context switch takes place when the system changes the running process (thread) The context of the previously running process is stored and the context of the process to run next is restored The context consists of the that is used the contents of the programmable registers the contents of the status registers 15 16 Priorities Each process is assigned a number that reflects the importance of its time demands Many RTOS (incl Stork): Low priority number = high priority Priority range: 1 - max integer Java: High priority number = high priority Priority range: 1-10 Using priority-based scheduling, processes that are Ready to execute are stored in according to priority Process Process Process Process Higher Priority Multicore Case Two alternatives: scheduling and Partitioned scheduling Scheduling a single ready queue 1 2 Process Process Process Process Process Priority Partitioned scheduling one ordinary ready queue per core often combined with the possibility to move (migrate) processes among cores 17 18

A change (insertion, removal) in may lead to a context switch If the change results in a new process being the first in, a context switch takes place preemptive scheduling the context switch takes place immediately (the running process is preempted) Most RTOS and real-time languages (eg and Java) nonpreemptive scheduling the running process continues until it voluntarily releases the CPU, then the context switch takes place scheduling based on preemption points the running process continues until it reaches an preemption point, then the context switch takes place early versions of Linux (no preemptions in kernel mode) Non-trivial knowledge Two ways: ad hoc rules Assigning priorities important time requirements high priority time consuming computations low priority conflicts, no guarantees scheduling theory Often easier to assign deadline than to assign priority 19 20 Dynamic priorities Process Representation Conceptually a process/thread consists of: With priority-based scheduling the priority of a process is fixed Fixed priority scheduling In the real-time system community it is increasingly common to instead study systems where it is the closeness to the deadline of the processes that decides which process that should execute Earliest Deadline First (EDF) scheduling The deadline can be viewed as a dynamic priority that changes as time proceeds Still not usual in commercial systems the code to be executed Java: run method a local variables of the process arguments and local variables of procedures called by the process when suspended: storage place for the values of programmable registers and program a process record (process (task) control block) administrative information priority, a to the code of the process, a to the of the process, etc 21 22 Memory Organization: Sequential MODULE Main; Growing direction VAR x, z: INTEGER; y: POINTER TO INTEGER; PROCEDURE Demo(a : INTEGER, b : INTEGER): INTEGER; VAR Result: INTEGER; Result := a * b - b*b; RETURN Result; END Demo; end x := 2; NEW(y); y^ := 7; z := Demo(x,y^); Write(z); DISPOSE(y); End Main 7 x -- 2 y z free-list New(y); y^ := 7; start 23 24

contents when Demo is called A frame is created Parameters Local variables b a Return value Return address Result Result Growing direction Pointer Memory Organization: Concurrency MODULE Main; (* Process *) PROCEDURE (); (* Local variable declarations *) LOOP ; END ; (* Process *) PROCEDURE (); (* Local variable declarations *) LOOP ; END ; 25 (* variable declarations *) (* creating process A and B *) Wait statement; END Main 26 Main Main end Process Record end TYPE PROCESS = ADDRESS; ProcessRef = POINTER TO ; Queue = POINTER TO QueueRec; () start QueueRec = RECORD, : ProcessRef; priority : CARDINAL; nexttime : Time; 27 = RECORD head : QueueRec; procv : PROCESS; timer : CARDINAL: : ADDRESS; Size : CARDINAL; 28 Real-Time Kernel Process Record QueueRec head: procv timer Size Modula-2 Process Record for for head head procv head head procv QueueRec (List Head) Head: procv timer Size QueueRec Head: procv timer Size QueueRec () 29 30

for head head procv for head head procv for head head procv for head head procv () () 31 32 for head head procv for head head procv for head head procv for head head procv () () 33 34 for for head head procv head head procv Initiated by a call to Context Switching directly by the running procedure from within an interrupt handler () Happens when: when the running process voluntarily releases the CPU when the running process performs an operation that may cause a blocked process to become ready 35 when an interrupt has occurred which may have caused a blocked process to become ready 36

PROCEDURE ; VAR old : ProcessRef; olddisable : InterruptMask; TRANSFER is entered in the context of one process and left in the context of another process Process A Process B olddisable := Disable(); (* Disable interrupts *) IF ^ <> THEN old := ; := ^; TRANSFER(old^procv,^procv); Reenable(oldDisable); interrupts disabled the actual context switch takes place in the Modula-2 primitive TRANSFER Disable enter TRANSFER; Disable; enter TRANSFER; the Modula-2 process record as argument 37 38 Process A Process B Process C Disable enter TRANSFER; The actual context switch Inside TRANSFER Saving: the state of the processor immediately before the switch is saved on the of the suspended process Disable; enter TRANSFER; Disable; enter TRANSFER; Switching: the context switch the value of the is stored in the process record of the suspended process the is set to the value that is stored in the process record of the new process Restoring: the state of the resumed process is restored (popped from its ) 39 40 Hyperthreading Many new architectures support hyperthreading The processor registers are duplicated Saving Switching Restoring Turn off all interrupts Push the program on the ; Push the programmable registers on the ; Push the status registers on the ; Save the value of the in the process record; Set the value of the to the value stored in the new process; Pop the status registers from the ; Pop the programmable registers from the ; Pop the program from the ; Turn on all interrupts; Old context New Context One set of registers is used for the thread currently being executed The other set (in the case of two hyperthreads) is used for the thread that is next to be executed, eg, the thread with the next-highest priority When context-switching between these two threads no context need to be saved and restored The processor only needs to change which set of register that it operates upon, which takes substantially less time 41 42

Java in Real-Time Three possibilities: Java Execution Models 1 Ahead-of-time (AOT) Compilation Java or Java bytecode to native code Java or Java bytecode to intermediate language (C) 2 Java Virtual Machine (JVM) as a process in an existing OS green thread model the threads are handled internally by the JVM native thread model the Java threads are mapped onto the threads of the OS executing on an empty machine green thread model 3 direct hardware support, eg, through micro-code 43 Compiled Java: works essentially in the same way as presented for JVM with native-thread model: each Java thread is executed by a native thread similar to what has been presented before 44 Java Execution Models JVM with green-thread model: threads are abstractions inside the JVM the JVM holds within the thread objects all information related to the threads the thread s the current instruction of the thread bookkeeping information the JVM performs context switching between threads by saving and restoring thread contexts the JVM program points at the current instruction of the executing thread the global program points at the current instruction of the JVM 45 Two ways: Thread creation By defining a class that extends (inherits from) the Thread class By defining a class that implements the runnable interface (defines a run method) The run method contains the code that the thread executes The thread is started by a call to the start method A Java thread active object, as opposed to a passive object 46 Thread creation: Extending Thread public class MyThread extends Thread { Start of the thread: MyThread m = new MyThread(); mstart(); 47 Thread creation: Implementing Runnable This way is used when the active object needs to extend some other class than Thread public class MyClass implements Runnable { Since an instance of MyClass is no longer a Thread object, the starting of the thread is slightly more complicated MyClass m = new MyClass(); Thread t = new Thread(m); tstart(); Drawback: non-static Thread methods are not directly accessible inside the run method (example later on) 48

Thread creation: Thread as a variable Thread creation: Thread as an inner class public class MyThread extends Thread { MyClass owner; public class MyClass extends MySuperClass { MyThread t; public MyThread(MyClass m) { owner = m; public class MyClass extends MySuperClass { MyThread t; public MyClass() { t = new MyThread(this); public void start() { tstart(); Makes it possible for an active object to contain multiple execution threads 49 class MyThread extends Thread { public MyClass() { t = new MyThread(); public void start() { tstart(); No explicit owner reference needed MyThread has direct access to variables and methods of MyClass The inner class need not have a name an anonymous class (Exercise 1) 50 Thread Priorities Thread termination A newly created thread inherits the priority of the creating thread The default priority is NORM_PRIORITY which is 5 Thread priority is changed by calling the nonstatic Thread method setpriority A thread terminates when its run method terminates To stop a thread from some other thread the recommended solution is to use a flag public class MyClass implements Runnable { boolean doit = true; public class MyClass implements Runnable { Thread t = ThreadcurrentThread(); tsetpriority(10); The static currentthread method is used to get a reference to thread of MyClass This reference is then used to call the nonstatic setpriority method 51 while (doit) { periodically The thread is stopped by setting the doit flag to false, either directly or by using some access method provided by MyClass Sleeping threads will not terminate immediately 52 Scheduling in Linux Scheduling in Linux, cont Four different scheduling classes (schedulers) SCHED_NORMAL the default scheduler from 2623 based on the Completely Fair r (CFS) not a real-time scheduler round robin-based fairness and efficiency major design design goals tradeoff between low latency (eg, IO-bound processes) and high throughput (eg, for compute-bound processes) SCHED_FIFO and SCHED_RR two quite similar real-time scheduling policies always have priority over SCHED_NORMAL tasks behaviour similar to, eg 53 SCHED_DEADLINE Earliest-Deadline-First r (more in later lectures) Support for CPU reservations Has priority over all the other scheduling policies Added in Linux 314, March 2014 A result from the EU project ACTORS led by Ericsson in Lund and with our department as partner 54