Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 10 SHARED MEMORY.

Slides:



Advertisements
Similar presentations
Computer Programming Lecture 14 C/C++ Pointers
Advertisements

Chapter 11 Introduction to Programming in C
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
CSCI 4717/5717 Computer Architecture
The University of Adelaide, School of Computer Science
Exceptions. Exception Types Exception Handling Vectoring Interrupts Interrupt Handlers Interrupt Priorities Interrupt applications 6-2.
Programming Languages and Paradigms The C Programming Language.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 9 MEMORY MANAGEMENT.
Computer Architecture CSCE 350
. Smart Pointers. Memory Management u One of the major issues in writing C/C++ code is managing dynamically allocated memory u Biggest question is how.
User-Level Memory Management in Linux Programming
Kernighan/Ritchie: Kelley/Pohl:
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Review of C++ Programming Part II Sheng-Fang Huang.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Runtime Environments Compiler Construction Chapter 7.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Copyright © 1997 – 2014 Curt Hill Concurrent Execution of Programs An Overview.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Dynamic memory allocation and Pointers Lecture 4.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Module R3 Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Copyright Curt Hill Variables What are they? Why do we need them?
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Java Thread and Memory Model
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Barriers and Condition Variables
CHAPTER 7 CONCURRENT SOFTWARE Copyright © 2000, Daniel W. Lewis. All Rights Reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Java Primer 1: Types, Classes and Operators
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Pointers and Memory Overview
Introduction to C Programming Language
Variable Names Names are a sequence of visible characters
Introduction to Operating Systems
Object Oriented Programming COP3330 / CGS5409
Variables Title slide variables.
Multi-modules programming
C Programming Getting started Variables Basic C operators Conditionals
Programming Language C Language.
CSE 153 Design of Operating Systems Winter 19
Programming Languages and Paradigms
C Language B. DHIVYA 17PCA140 II MCA.
Storage Classes.
Presentation transcript:

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 10 SHARED MEMORY

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Shared Memory Shared memory is data that is accessed by two or more asynchronous instruction sequences. Asynchronous means that there is no predictable time relationship among the various instruction sequences. Thread: A sequence of instructions; threads are usually asynchronous relative to each other. All ISRs are threads, but not all threads are ISRs!

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Asynchronous Access Can Corrupt Shared Data! Thread A Thread B shared = 3 ; x = 2 * shared ; shared++ ; Asynchronous Task Switch or Interrupt

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Task Switch = Interrupt! Note that interrupts (and thus a context switch) can occur between any two CPU instructions, not just between any two C language statements!

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Program Complexity Threads are easier to design, understand, and debug when they are as independent of other threads as possible. Shared memory reduces thread independence. Shared memory increases program complexity.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. How Sharing Can Occur Shared Global Data: A public (global) object is accessed by two or more threads, or Shared Private Data: The address of a private object is given to another thread, or Shared Functions: A static object is accessed by a function that is called by more than one thread.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Shared Global Data Easiest cause of sharing to recognize. Minimize global objects, whether your program is multi-threaded or not, because global objects allow linkages between functions and thus contribute to program complexity.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Shared Private Data If one thread gives the address of one of its private objects to another thread, then that object can be accessed by both threads and is no longer private. Neither changing the object's scope or memory allocation method will eliminate this form of shared memory.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Shared Functions A shared function is one that is called by more than one thread. Any function called by a shared function is also a shared function. Any static object (local or global) referenced within a shared function is shared data.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Thread-Safe Functions Thread-safe functions are shared functions that only modify thread-specific data. Local automatic and dynamic objects are inherently thread-specific; local static objects are not.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Re-Entrant Functions Thread B Thread A Enters printf Exits printf Enters, executes, and exits printf Printf suspended

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Re-Entrant Functions Re-entrant functions are those which may be safely re-entered without data corruption. Re-entrant functions never modify local static objects. Re-entrant functions are inherently thread- safe.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Read-Only Data Data corruption only occurs when shared data is modified. Shared data that is read but never written can never be corrupted. Add the keyword “const” to have the compiler verify that all access to the object is read-only: static const char digits[] = “ ” ;

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Coding Practices to Avoid Functions that keep internal state in local static objects. –E.g., strtok, rand Functions that return the address of a local static object. –E.g., ctime, asctime, localtime, gmtime, getenv, strerror and tmpnam.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Function with Internal State char *strtok(char *string, char *delimeters) { static char *cursor ; char *beg, *end ; if (string != NULL) cursor = string ; if (cursor == NULL) return NULL ; beg = cursor + strspn(cursor, delimeters) ; if (*beg == '\0') return NULL ; end = strpbrk(beg, delimeters) ; if (end == NULL) cursor = NULL ; else { *end++ = '\0'; cursor = end; } return beg ; } Problem: Only one instance for all threads.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Fixing Internal State char *strtok_r(char *string, char *delimeters, char **cursor) { char *beg, *end ; if (string != NULL) *cursor = string ;... return beg ; } char *my_cursor ; … p = strtok_r(…, …, &my_cursor) ; … Thread-specific version supplied by caller.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Function Returning Static Buffer char *Make_Filename(char *name, int version) { static char fname_bfr[13] ;... return fname_bfr ; }

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Fixing Static Buffer (Sol’n #1) char *Make_Filename(char *name, int version, char *fname_bfr) { … return fname_bfr ; } Let caller provide a thread-specific instance of a buffer.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Fixing Static Buffer (Sol’n #2) char *Make_Filename(char *name, int version) { char *fname_bfr = (char *) malloc(13) ; if (fname_bfr == NULL) return NULL ; … return fname_bfr ; } Thread-specific instance of buffer allocated from heap when function is called; must be released by caller.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Corruption of Data in Shared Memory Main Program ISR SUB EAX,1 MOV [EBX+_count],EAX... MOV EBX,[_pointer2q] MOV EAX,[EBX+_count] ADD EAX,1 MOV [EBX+_count],EAX... pointer2q->count-- pointer2q->count++ MOV EBX,[_pointer2q] MOV EAX,[EBX+_count]

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Effect of Processor Word Size Previous example is somewhat contrived: – Most compilers would use single increment ( INC ) and decrement ( DEC ) instructions to update the count. –Interrupts only occur between instructions, so the decrement of the count would then complete before the interrupt is recognized. But what if count was 32 bits and processor is 16 bits?

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Protected Access extern long shared_counter ; disable() ; shared_counter++ ; enable() ;... disable() ; shared_counter = 0 ; enable() ; A simple increment may require more than one machine instruction - perhaps the processor has no single increment instruction, the compiler doesn't use it, or the integer exceeds the basic processor word size. A simple store may also require more than one instruction if the object exceeds the processor word size.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Multiple Read-Only Access extern long shared_variable ; long private_copy ; /* make a copy with interrupts disabled */ disable() ; private_copy = shared_variable ; enable() ; /* subsequent code can use "private_copy"*/ /* without fear of data corruption.*/

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. What to Worry About 8-bit processors: –Anything other than a char 16-bit processors: –longs, far pointers, and all floating-point data. 32-bit processors: –64-bit long long ints –Double- and extended-precision floating-point data.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Type Qualifier “volatile” Added to the declaration of any object in C to indicate that its value may be modified by mechanisms other than the code in which the declaration appears. Serves as a reminder that shared data may be modified by another thread or ISR. Prevents certain compiler optimizations that would otherwise be invalid. But does not prevent data corruption due to shared memory.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. long Get_Shared(void) { extern long shared ; long validated ; do { validated = shared ; } while (validated != shared) ; return validated ; } Volatile and Loop Invariants Since “shared” is not modified within the loop, an optimizing compiler might preload a CPU register with the value of "shared" before entering the loop, and access the register instead of the slower memory. Insert the keyword “volatile” here to prevent the compiler optimizations. This code attempts to read a shared variable without disabling interrupts or using spin locks or semaphores.