SSC - Concurrency and Multi-threading Basic concepts

Similar documents
SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

Chapter 2: OS Overview

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Page 1 of 5. IS 335: Information Technology in Business Lecture Outline Operating Systems

Multiprocessor Scheduling and Scheduling in Linux Kernel 2.6

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

W4118 Operating Systems. Instructor: Junfeng Yang

Module 8. Industrial Embedded and Communication Systems. Version 2 EE IIT, Kharagpur 1

Linux Process Scheduling Policy

First-class User Level Threads

Operating System: Scheduling

Why Threads Are A Bad Idea (for most purposes)

Thomas Fahrig Senior Developer Hypervisor Team. Hypervisor Architecture Terminology Goals Basics Details

Multi-core Programming System Overview

OPERATING SYSTEMS SCHEDULING

REAL TIME OPERATING SYSTEMS. Lesson-10:

Deciding which process to run. (Deciding which thread to run) Deciding how long the chosen process can run

Operating Systems 4 th Class

Processes and Non-Preemptive Scheduling. Otto J. Anshus

ELEC 377. Operating Systems. Week 1 Class 3

System Structures. Services Interface Structure

Operating Systems Concepts: Chapter 7: Scheduling Strategies

Operating Systems. Lecture 03. February 11, 2013

Objectives. Chapter 5: CPU Scheduling. CPU Scheduler. Non-preemptive and preemptive. Dispatcher. Alternating Sequence of CPU And I/O Bursts

Chapter 6, The Operating System Machine Level

Technical Properties. Mobile Operating Systems. Overview Concepts of Mobile. Functions Processes. Lecture 11. Memory Management.

Real-Time Systems Prof. Dr. Rajib Mall Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CHAPTER 1 INTRODUCTION

CS161: Operating Systems

Operating System Tutorial

Chapter 3 Operating-System Structures

CPU Scheduling. Basic Concepts. Basic Concepts (2) Basic Concepts Scheduling Criteria Scheduling Algorithms Batch systems Interactive systems

CS414 SP 2007 Assignment 1

Overview of Presentation. (Greek to English dictionary) Different systems have different goals. What should CPU scheduling optimize?

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

Processor Scheduling. Queues Recall OS maintains various queues

Zing Vision. Answering your toughest production Java performance questions

Operating Systems. 05. Threads. Paul Krzyzanowski. Rutgers University. Spring 2015

Lesson Objectives. To provide a grand tour of the major operating systems components To provide coverage of basic computer system organization

Comp 204: Computer Systems and Their Implementation. Lecture 12: Scheduling Algorithms cont d

ò Paper reading assigned for next Thursday ò Lab 2 due next Friday ò What is cooperative multitasking? ò What is preemptive multitasking?

Real Time Programming: Concepts

Chapter 1 Computer System Overview

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

Operating Systems for Parallel Processing Assistent Lecturer Alecu Felician Economic Informatics Department Academy of Economic Studies Bucharest

Lecture 6: Interrupts. CSC 469H1F Fall 2006 Angela Demke Brown

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

Control 2004, University of Bath, UK, September 2004

Outline: Operating Systems

OPERATING SYSTEM SERVICES

This tutorial will take you through step by step approach while learning Operating System concepts.

CSE 120 Principles of Operating Systems. Modules, Interfaces, Structure

1 Organization of Operating Systems

Embedded Systems. 6. Real-Time Operating Systems

Review from last time. CS 537 Lecture 3 OS Structure. OS structure. What you should learn from this lecture

CPU Scheduling Outline

Process Scheduling CS 241. February 24, Copyright University of Illinois CS 241 Staff

1. Computer System Structure and Components

Operating Systems. III. Scheduling.

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Kernel Types System Calls. Operating Systems. Autumn 2013 CS4023

Chapter 3: Operating-System Structures. System Components Operating System Services System Calls System Programs System Structure Virtual Machines

Performance Comparison of RTOS

find model parameters, to validate models, and to develop inputs for models. c 1994 Raj Jain 7.1

Road Map. Scheduling. Types of Scheduling. Scheduling. CPU Scheduling. Job Scheduling. Dickinson College Computer Science 354 Spring 2010.

Thread Synchronization and the Java Monitor

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

Operating System Structures

Introduction. Scheduling. Types of scheduling. The basics

COS 318: Operating Systems

Chapter 13 Embedded Operating Systems

Kernel comparison of OpenSolaris, Windows Vista and. Linux 2.6

How do Users and Processes interact with the Operating System? Services for Processes. OS Structure with Services. Services for the OS Itself

Chapter 2: Processes, Threads, and Agents

Process Description and Control william stallings, maurizio pizzonia - sistemi operativi

Example of Standard API

Chapter 12. Development Tools for Microcontroller Applications

Real-Time Scheduling 1 / 39

Operatin g Systems: Internals and Design Principle s. Chapter 10 Multiprocessor and Real-Time Scheduling Seventh Edition By William Stallings

Lecture Outline Overview of real-time scheduling algorithms Outline relative strengths, weaknesses

CHAPTER 15: Operating Systems: An Overview

CSC 2405: Computer Systems II

PART IV Performance oriented design, Performance testing, Performance tuning & Performance solutions. Outline. Performance oriented design

ICS Principles of Operating Systems

2. is the number of processes that are completed per time unit. A) CPU utilization B) Response time C) Turnaround time D) Throughput

Windows Server Performance Monitoring

Real Time Operating System for Embedded DSP Applications

Agenda. Enterprise Application Performance Factors. Current form of Enterprise Applications. Factors to Application Performance.

Operating Systems Lecture #6: Process Management

CS420: Operating Systems OS Services & System Calls

Chapter 2 System Structures

Lecture 25 Symbian OS

Topics. Producing Production Quality Software. Concurrent Environments. Why Use Concurrency? Models of concurrency Concurrency in Java

Facing the Challenges for Real-Time Software Development on Multi-Cores

Red Hat Linux Internals

Mutual Exclusion using Monitors

OPERATING SYSTEM - VIRTUAL MEMORY

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2007 Lecture 5 - DBMS Architecture

Readings for this topic: Silberschatz/Galvin/Gagne Chapter 5

Using Protothreads for Sensor Node Programming

Transcription:

SSC - Concurrency and Multi-threading Basic concepts Shan He School for Computational Science University of Birmingham Module 06-19321: SSC

Outline Outline of Topics

Concurrency: history In the beginning, a computer was only capable of executing a single program at a time. Multitasking: Execute multiple programs at the same time. Modern operating systems are all multitasking Essentially shares the computing resources, such as CPU(s), main memory, and I/O channels. Single-CPU machine: only one task can be executed at one time through time-slicing of the CPU. Multi-CPU machine: tasks can be executed simultaneously, distributed among or time-slicing the CPUs.

Multitasking: Cooperative vs Preemptive Cooperative multitasking: processes control the CPU Used in early multitasking operating systems, e.g., Win 3.1 Transferring control explicitly from one process to another (co-routines) according to a cooperative model Runtime support is simpler to implement Programmers have to handle cooperation: bugs in processes may lock the systems Pre-emptive multi-tasking: Kernel schedules CPU to each task Used in modern multitasking operating systems Operating system s kernel manages (schedules) process access to CPU Pre-emption: an action performed by kernel: it forces a process to abandon its running state even if it could safely execute Used time slicing mechanism: a process is suspend when a specified amount of time has expired

Multitasking: Cooperative vs Preemptive Cooperative multitasking Pre-emptive multitasking 1 3 5 Switch() Switch() P1 2 4 6 Switch() P2 Switch() Interrupt Interrupt 1 3 5 P1 2 4 6 Interrupt P2 Interrupt

Preemptive Multitasking Process creation ready Selection Preemption running Process termination Synchronisation statement executed by other processes waiting Synchronisation statement executed by the process

Concurrent programming: Two basic units Process: a program has a self-contained execution environment has a complete, private set of basic run-time resources has its own memory space totally controlled by the operating system Thread: also called lightweight process, which is a dispatchable unit of work in a process. has a definite beginning and an end run inside a single process share the same address space, the resources allocated and the environment of that process

More about threads Both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process. A process can be divided into multiple independent threads A process can be a single thread, which is called main thread. A standalone Java application starts with main thread ( main() ) This main thread can start new independent threads. Multithreading: executing multiple threads inside the same process (program).

Concurrency: Two basic units Single threaded process Multithreaded process Thread execution

Main thread and threads Main thread Thread 2 Thread 3 Time Begin End Suspended/Resumed

Difference between threads and processes Processes are typically independent and might consist of multiple threads Processes have separate address spaces for code, data and other resources, whereas threads share the address space of the process that created it Threads are easier to create than processes Multithreading requires careful programming A thread has its own stacks and registers Stack: a reserved region of memory data that is operated in a last-in-first-out manner Register: a small amount of storage available as part of CPU Processes use inter-process communication mechanisms provided by the OS to communicate with other processes, while threads can directly communicate with other threads in the same process

Difference between threads and processes Single thread process Code Memory segments Other Data resources Multithreaded process Code Data Other resources Register Stack Register Register Register Stack Stack Stack

Context switching Context switching: a procedure of multi-threading for the system to switch between threads running on the available CPUs A context is the minimal set of data used by this task that must be saved to allow a task interruption at any point int time Data to be saved include: Registers: one of a small set of data holding places in CPU, which may hold a computer instruction, a storage address, or any kind of data Program counter: known as an instruction address register, which is a small amount of fast memory that holds the address of the instruction to be executed immediately after the current one other necessary operating system specific data

Context switching: how it works A CPU timer: determines the end of the timeslice for each thread, and signals at the end of the timeslice Steps for context switching: Step 1: interrupt current thread by the CPU timer Step 2: The CPU saves all information required, e.g., register and programme counter for the current thread onto a stack Step 3: Move this information from the stack into a data structure called context structure. Step 4: A scheduler algorithm decides which thread to run next Step 5: Run the new thread until a interrupt-return is called to switch back to a previously executing thread Step 6: Transfers all the information from the context structure associated with the previously executing thread to the stack Step 7: resume previous executing thread

Advantages of multi-threading Advantages compared with multi-processing Improves the performance of the program by better usage of system resources: Share the same address space, less overhead for operating system Context-switching between threads is normally inexpensive Better usage of CPU time, e.g., while one thread is blocked (e.g., waiting for completion of an I/O operation), another thread can use the CPU time to perform computations Simpler program design: Control and communication between threads is easy and inexpensive. More responsive programs

Disadvantages of multi-threading Disadvantages/costs compared with single-threading Context switching overhead: even lighter than multi-processing, CPU still needs to save the register, program counter etc. of the current thread, and load the same of the next thread to execute. More complex design: data shared and accessed by multiple threads needs special attention Increased resource consumption: CPU time, memory to keep its local stack, and operating system resources to manage the thread

Concurrent programming - three important concepts Atomicity : An operation is said atomic when it cannot be interrupted. Visibility: If an action in one thread is visible to another thread, then the result of that action can be observed by the second thread. Ordering: Since in multi-threading program, the order of execution is not guaranteed, we need ordering constraints to define what order should the actions in threads executed. We shall see these concepts in Java examples in the next few lectures.