Introduction to Embedded Systems

Similar documents
Introduction to Embedded Systems

Making Concurrency Mainstream

Concurrency Demands New Foundations for Computing

Microkernel/OS and Real-Time Scheduling

Concurrent Semantics without the Notions of State or State Transitions

Disciplined Concurrent Models of Computation for Parallel Software

fakultät für informatik informatik 12 technische universität dortmund Specifications Peter Marwedel TU Dortmund, Informatik /11/15

Specifications and Modeling

Introducing Embedded Systems: A Cyber- Physical Systems Approach

CSEN 602-Operating Systems, Spring 2018 Practice Assignment 2 Solutions Discussion:

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

Concurrency, Thread. Dongkun Shin, SKKU

Understandable Concurrency

PThreads in a Nutshell

Multiprocessors 2007/2008

The Problem with Treads

Software Engineering CSE 335, Spring OO design: Concurrency and Distribution

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS510 Operating System Foundations. Jonathan Walpole

CMSC421: Principles of Operating Systems

The Problem with Threads

Threads Tuesday, September 28, :37 AM

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Multithreading Programming II

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

CSE 306/506 Operating Systems Threads. YoungMin Kwon

CS 3305 Intro to Threads. Lecture 6

Introduction to PThreads and Basic Synchronization

Lesson 5: Software for embedding in System- Part 2

Programming with Shared Memory. Nguyễn Quang Hùng

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

EECS 571 Principles of Real-Time Embedded Systems. Lecture Note #10: More on Scheduling and Introduction of Real-Time OS

THREADS & CONCURRENCY

Concurrent Models of Computation

Programming Languages

Programming in Parallel COMP755

CS 261 Fall Mike Lam, Professor. Threads

Models of computation

Lecture 19: Shared Memory & Synchronization

Thread and Synchronization

The Problem With Threads

This exam paper contains 8 questions (12 pages) Total 100 points. Please put your official name and NOT your assumed name. First Name: Last Name:

Introduction to Embedded Systems

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

CS333 Intro to Operating Systems. Jonathan Walpole

Parallel Programming Languages COMP360

THREADS AND CONCURRENCY

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

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Threads. studykorner.org

CSE 4/521 Introduction to Operating Systems

CS533 Concepts of Operating Systems. Jonathan Walpole

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 4 Concurrent Programming

Lesson 6: Process Synchronization

Threads. lightweight processes

20-EECE-4029 Operating Systems Spring, 2015 John Franco

Introduction to pthreads

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

So far, we've seen situations in which locking can improve reliability of access to critical sections.

Chapter 6 Process Synchronization

Operating systems and concurrency (B10)

Notes. CS 537 Lecture 5 Threads and Cooperation. Questions answered in this lecture: What s in a process?

Synchronization I q Race condition, critical regions q Locks and concurrent data structures q Next time: Condition variables, semaphores and monitors

ECE 462 Object-Oriented Programming using C++ and Java. Scheduling and Critical Section

Synchronization I. To do

Synchronization I. Today. Next time. Race condition, critical regions and locks. Condition variables, semaphores and Monitors

EECS 482 Introduction to Operating Systems

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

Threaded Programming. Lecture 9: Alternatives to OpenMP

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization

TCSS 422: OPERATING SYSTEMS

Operating systems. Lecture 12

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

CS420: Operating Systems. Process Synchronization

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Processes and Threads

Embedded System Design and Modeling EE382V, Fall 2008

Process! Process Creation / Termination! Process Transitions in" the Two-State Process Model! A Two-State Process Model!

20-EECE-4029 Operating Systems Fall, 2015 John Franco

CSE 333 Final Exam June 8, 2016 Sample Solution

Synchronization I. Today. Next time. Monitors. ! Race condition, critical regions and locks. ! Condition variables, semaphores and

EC-821 Advanced Embedded Systems (3+0)

Multithreading and Interactive Programs

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

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

e-ale-rt-apps Building Real-Time Applications for Linux Version c CC-BY SA4

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

Concurrent Server Design Multiple- vs. Single-Thread

Shared Memory Programming. Parallel Programming Overview

Introduction to Embedded Systems

Overview. Thread Packages. Threads The Thread Model (1) The Thread Model (2) The Thread Model (3) Thread Usage (1)

EE382N.23: Embedded System Design and Modeling

What's wrong with Semaphores?

Concurrent Models of Computation

Operating System Labs. Yuanbin Wu

Dealing with Issues for Interprocess Communication

Transcription:

Introduction to Embedded Systems Edward A. Lee & Sanjit Seshia UC Berkeley EECS 124 Spring 2008 Copyright 2008, Edward A. Lee & Sanjit Seshia, All rights reserved Lecture 17: Concurrency 2: Threads Definition and Uses Threads are sequential procedures that share memory. Uses: Reacting to external events (interrupts) Exception handling (software interrupts) Creating the illusion of simultaneously running different programs (multitasking) Exploiting parallelism in the hardware (e.g. multicore machines). Dealing with real-time constraints. EECS 124, UC Berkeley: 2 1

Thread Scheduling Thread scheduling is an iffy proposition. Without an OS, multithreading is achieved with interrupts. Timing is determined by external events. Generic OSs (Linux, Windows, OSX, ) provide thread libraries (like pthreads ) and provide no fixed guarantees about when threads will execute. Real-time operating systems (RTOSs), like QNX, VxWorks, RTLinux, Windows CE, support a variety of ways of controlling when threads execute (priorities, preemption policies, deadlines, ). Processes are collections of threads with their own memory, not visible to other processes. Segmentation faults are attempts to access memory not allocated to the process. Communication between processes must occur via OS facilities (like pipes or files). EECS 124, UC Berkeley: 3 Posix Threads (PThreads) PThreads is an API (Application Program Interface) implemented by many operating systems, both real-time and not. It is a library of C procedures. Standardized by the IEEE in 1988 to unify variants of Unix. Subsequently implemented in most other operating systems. An alternative is Java, which typically uses PThreads under the hood, but provides thread constructs as part of the programming language. EECS 124, UC Berkeley: 4 2

Creating and Destroying Threads #include <pthread.h> Can pass in pointers to shared variables. void* threadfunction(void* arg) { return pointertosomething or NULL; Can return pointer to something. Do not return a pointer to an automatic variable! int main(void) { pthread_t threadid; void* exitstatus; Create a thread (may or may not start running!) int value = something; pthread_create(&threadid, NULL, threadfunction, &value); Becomes arg parameter to pthread_join(threadid, &exitstatus); threadfunction. Why is it OK that this an return 0; automatic variable? Return only after all threads have terminated. EECS 124, UC Berkeley: 5 Notes Threads may or may not beginning running when created. A thread may be suspended between any two atomic instructions (typically, assembly instructions, not C statements!) to execute another thread and/or interrupt service routine. Threads can often be given priorities, and these may or may not be respected by the thread scheduler. Threads may block on semaphores and mutexes (we do this next). EECS 124, UC Berkeley: 6 3

Modeling Threads States or transitions represent atomic instructions Interleaving semantics: Choose one machine at random. Advance to a next state if guards are satisfied. Repeat. For the machines at the left, what are the reachable states? EECS 124, UC Berkeley: 7 Typical thread programming problem The Observer pattern defines a one-to-many dependency between a subject object and any number of observer objects so that when the subject object changes state, all its observer objects are notified and updated automatically. Design Patterns, Eric Gamma, Richard Helm, Ralph Johnson, John Vlissides (Addison-Wesley Publishing Co., 1995. ISBN: 0201633612): EECS 124, UC Berkeley: 8 4

Observer Pattern in C // Value that when updated triggers notification // of registered listeners. int value; // List of listeners. A linked list containing // pointers to notify procedures. typedef void* notifyprocedure(int); struct element { typedef struct element elementtype; elementtype* head = 0; elementtype* tail = 0; // Procedure to add a listener to the list. void* addlistener(notifyprocedure listener) { // Procedure to update the value void* update(int newvalue) { // Procedure to call when notifying void print(int newvalue) { EECS 124, UC Berkeley: 9 Observer Pattern in C // Value that when updated triggers notification of registered listeners. int value; // List of listeners. A linked list containing // pointers to notify procedures. typedef void* notifyprocedure(int); struct element { typedef struct element elementtype; ; elementtype* head = 0; elementtype* tail = 0; // Procedure to add a listener to the list. void* addlistener(notifyprocedure listener) { // Procedure to update the value void* update(int newvalue) { // Procedure to call when notifying void print(int newvalue) { typedef void* notifyprocedure(int); struct element { notifyprocedure* listener; struct element* next; typedef struct element elementtype; elementtype* head = 0; elementtype* tail = 0; EECS 124, UC Berkeley: 10 5

Observer Pattern in C // Value that when updated triggers notification of registered listeners. int value; // Procedure to add a listener to the list. void* addlistener(notifyprocedure listener) { if (head == 0) { head = malloc(sizeof(elementtype)); head->listener = listener; // List of listeners. A linked list containing // pointers to notify procedures. typedef void* notifyprocedure(int); struct element { head->next = 0; typedef struct element tail elementtype; = head; elementtype* head = else 0; { elementtype* tail = 0; // Procedure to add a listener to the list. void* addlistener(notifyprocedure listener) { // Procedure to update the value void* update(int newvalue) { // Procedure to call when notifying void print(int newvalue) { tail->next = malloc(sizeof(elementtype)); tail = tail->next; tail->listener = listener; tail->next = 0; EECS 124, UC Berkeley: 11 Observer Pattern in C // Value that when updated triggers notification of registered listeners. int value; // List of listeners. A linked list containing // pointers to notify procedures. typedef void* notifyprocedure(int); struct element { typedef struct element elementtype; elementtype* head = 0; elementtype* tail = 0; // Procedure to update the value void* update(int newvalue) { value = newvalue; // Procedure to add // a Notify listener listeners. to the list. void* addlistener(notifyprocedure listener) { // Procedure to update the value void* update(int newvalue) { elementtype* element = head; while (element!= 0) { (*(element->listener))(newvalue); element = element->next; // Procedure to call when notifying void print(int newvalue) { EECS 124, UC Berkeley: 12 6

Observer Pattern in C // Value that when updated triggers notification of registered listeners. int value; // List of listeners. A linked list containing // pointers to notify procedures. typedef void* notifyprocedure(int); struct element { typedef struct element elementtype; elementtype* head = 0; elementtype* tail = 0; // Procedure to add a listener to the list. void* addlistener(notifyprocedure listener) { // Procedure to update the value void* update(int newvalue) { // Procedure to call when notifying void print(int newvalue) { Will this work in a multithreaded context? EECS 124, UC Berkeley: 13 #include <pthread.h> pthread_mutex_t lock; Using Posix mutexes on the observer pattern in C void* addlistener(notify listener) { pthread_mutex_lock(&lock); pthread_mutex_unlock(&lock); void* update(int newvalue) { pthread_mutex_lock(&lock); value = newvalue; elementtype* element = head; while (element!= 0) { (*(element->listener))(newvalue); element = element->next; pthread_mutex_unlock(&lock); int main(void) { pthread_mutex_init(&lock, NULL); However, this carries a significant deadlock risk. The update procedure holds the lock while it calls the notify procedures. If any of those stalls trying to acquire another lock, and the thread holding that lock tries to acquire this lock, deadlock results. EECS 124, UC Berkeley: 14 7

After years of use without problems, a Ptolemy Project code review found code that was not thread safe. It was fixed in this way. Three days later, a user in Germany reported a deadlock that had not shown up in the test suite. EECS 124, UC Berkeley: 15 #include <pthread.h> pthread_mutex_t lock; void* addlistener(notify listener) { pthread_mutex_lock(&lock); pthread_mutex_unlock(&lock); void* update(int newvalue) { pthread_mutex_lock(&lock); value = newvalue; copy the list of listeners pthread_mutex_unlock(&lock); elementtype* element = headcopy; while (element!= 0) { (*(element->listener))(newvalue); element = element->next; int main(void) { pthread_mutex_init(&lock, NULL); One possible fix What is wrong with this? Notice that if multiple threads call update(), the updates will occur in some order. But there is no assurance that the listeners will be notified in the same order. Listeners may be mislead about the final value. EECS 124, UC Berkeley: 16 8

This is a very simple, commonly used design pattern. Perhaps Concurrency is Just Hard Sutter and Larus observe: humans are quickly overwhelmed by concurrency and find it much more difficult to reason about concurrent than sequential code. Even careful people miss possible interleavings among even simple collections of partially ordered operations. H. Sutter and J. Larus. Software and the concurrency revolution. ACM Queue, 3(7), 2005. EECS 124, UC Berkeley: 17 If concurrency were intrinsically hard, we would not function well in the physical world It is not concurrency that is hard EECS 124, UC Berkeley: 18 9

It is Threads that are Hard! Threads are sequential processes that share memory. From the perspective of any thread, the entire state of the universe can change between any two atomic actions (itself an ill-defined concept). Imagine if the physical world did that EECS 124, UC Berkeley: 19 Problems with the Foundations A model of computation: Bits: B = {0, 1 Set of finite sequences of bits: B Computation: f : B B Composition of computations: f f ' Programs specify compositions of computations Threads augment this model to admit concurrency. But this model does not admit concurrency gracefully. EECS 124, UC Berkeley: 20 10

Basic Sequential Computation initial state: b 0 B sequential composition b n = f n ( b n-1 ) final state: b N Formally, composition of computations is function composition. EECS 124, UC Berkeley: 21 When There are Threads, Everything Changes A program no longer computes a function. suspend resume b n = f n ( b n-1 ) another thread can change the state b' n = f n ( b' n-1 ) Apparently, programmers find this model appealing because nothing has changed in the syntax. EECS 124, UC Berkeley: 22 11

Succinct Problem Statement Threads are wildly nondeterministic. The programmer s job is to prune away the nondeterminism by imposing constraints on execution order (e.g., mutexes) and limiting shared data accesses (e.g., OO design). EECS 124, UC Berkeley: 23 Incremental Improvements to Threads Object Oriented programming Coding rules (Acquire locks in the same order ) Libraries (Stapl, Java 5.0, ) Transactions (Databases, ) Patterns (MapReduce, ) Formal verification (Blast, thread checkers, ) Enhanced languages (Split-C, Cilk, Guava, ) Enhanced mechanisms (Promises, futures, ) EECS 124, UC Berkeley: 24 12