1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 38: Implementing Concurrency COMP 144 Programming Language Concepts Spring 2002.

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

Processes Management.
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Scheduler Activations: Effective Kernel Support for the User-level Management of Parallelism Thomas E. Anderson, Brian N. Bershad, Edward D. Lazowska,
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Threads Irfan Khan Myo Thein What Are Threads ? a light, fine, string like length of material made up of two or more fibers or strands of spun cotton,
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
1 Multiprocessors. 2 Idea: create powerful computers by connecting many smaller ones good news: works for timesharing (better than supercomputer) bad.
User Level Interprocess Communication for Shared Memory Multiprocessor by Bershad, B.N. Anderson, A.E., Lazowska, E.D., and Levy, H.M.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 37: Beyond Sequential Programs COMP 144 Programming Language Concepts Spring 2002.
Scheduler Activations Effective Kernel Support for the User-Level Management of Parallelism.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
3.5 Interprocess Communication
Chapter 11: Distributed Processing Parallel programming Principles of parallel programming languages Concurrent execution –Programming constructs –Guarded.
Threads CSCI 444/544 Operating Systems Fall 2008.
Chapter 11 Operating Systems
1 Chapter 4 Threads Threads: Resource ownership and execution.
Inter-Process Communication  most OSs provide several abstractions for inter- process communication: message passing, shared memory, etc.  communication.
1 Organization of Programming Languages-Cheng (Fall 2004) Concurrency u A PROCESS or THREAD:is a potentially-active execution context. Classic von Neumann.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
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.
Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
What is Concurrent Programming? Maram Bani Younes.
COP 4600 Operating Systems Spring 2011 Dan C. Marinescu Office: HEC 304 Office hours: Tu-Th 5:00-6:00 PM.
1 Computer System Overview Chapter 1. 2 n An Operating System makes the computing power available to users by controlling the hardware n Let us review.
Operating System 4 THREADS, SMP AND MICROKERNELS
Fast Multi-Threading on Shared Memory Multi-Processors Joseph Cordina B.Sc. Computer Science and Physics Year IV.
Threads and Concurrency. A First Look at Some Key Concepts kernel The software component that controls the hardware directly, and implements the core.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
CPS 506 Comparative Programming Languages
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Threads G.Anuradha (Reference : William Stallings)
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Lecture 2 Page 1 CS 111 Online System Services for OSes One major role of an operating system is providing services – To human users – To applications.
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.
Introduction to Operating Systems and Concurrency.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Department of Computer Science and Software Engineering
Processes & Threads Introduction to Operating Systems: Module 5.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Threads-Process Interaction. CONTENTS  Threads  Process interaction.
C H A P T E R E L E V E N Concurrent Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Assembly Language Co-Routines
Operating System Concepts
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Big Picture Lab 4 Operating Systems C Andras Moritz
Chapter 4 – Thread Concepts
Threads & Multithreading
PROCESS MANAGEMENT IN MACH
Chapter 4 – Thread Concepts
Lecture 12 Concepts of Programming Languages
Threads Chapter 4.
Fast Communication and User Level Parallelism
Threads Chapter 4.
Chapter 4 Threads, SMP, and Microkernels
CS510 Operating System Foundations
CS703 – Advanced Operating Systems
Chapter 3: Process Management
Presentation transcript:

1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 38: Implementing Concurrency COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos April 26 The University of North Carolina at Chapel Hill

2 COMP 144 Programming Language Concepts Felix Hernandez-Campos Concurrent Programming Concurrent Program Sequential Program

3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Multiprocessors

4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Shared-Memory Multiprocessor Local cache has much lower latency

5 COMP 144 Programming Language Concepts Felix Hernandez-Campos Threads and Processes OS Space versus User Space

6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Threads and Processes Tradeoffs One-process-per-thread is acceptable in personal computer with a single address spaceOne-process-per-thread is acceptable in personal computer with a single address space  This is too expensive in most OSes, since each operation on them requires a system call  Processes are general-purpose, so threads may pay the price of features they do not use »Processes are heavy-weight All-threads-on-one-process are acceptable in simple languages for uniprocessorsAll-threads-on-one-process are acceptable in simple languages for uniprocessors  This precludes parallel execution in a multiprocessor machine  System call will block the entire set of threads

7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Threads and Processes Multiprocessors A multiprocessor OS may allocate processes to processors following one the following main strategiesA multiprocessor OS may allocate processes to processors following one the following main strategies Coscheduling (a.k.a. gang scheduling): attempts to run each process in a different processorCoscheduling (a.k.a. gang scheduling): attempts to run each process in a different processor –Maximize parallelism Space sharing (a.k.a. processor partitioning): give an application exclusive use of some subset of the processorsSpace sharing (a.k.a. processor partitioning): give an application exclusive use of some subset of the processors –Minimizing context switching cost and/or communication cost

8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Coroutines User-level threads are usually built on top of coroutinesUser-level threads are usually built on top of coroutines –Simulate parallel execution in a single processor »p(a,b,c) »d:=q(e,f) ; r(d,g,h) »s(i,j) p(a, b, c) s(i, j) d:=q(e, f) r(d, g, h) Coroutines are execution contexts that exist concurrently and execute one at a timeCoroutines are execution contexts that exist concurrently and execute one at a time Coroutines transfer control to each other explicitly (by name)Coroutines transfer control to each other explicitly (by name)

9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Coroutines Example Screen-saver programScreen-saver programloop -- update picture on screen -- perform next sanity check –Successive updates and sanity checks usually depend on each other »Save and restore state of the computation –Coroutines are more attractive for this problem

10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Coroutines Example A coroutine can detach itself from the main programA coroutine can detach itself from the main program –detach created the coroutine object Control can be transferred from one coroutine to anotherControl can be transferred from one coroutine to another –transfer saves the program counter and resumes the coroutines specifies as a parameter –Transfers can occur anywhere in the code of the coroutine

11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Turning Coroutines Into Threads The programming language environment provides a scheduler in charge of transferring control automaticallyThe programming language environment provides a scheduler in charge of transferring control automatically The scheduler chooses which threads to run first after the the current thread yields the processorsThe scheduler chooses which threads to run first after the the current thread yields the processors The scheduler may implement preemption mechanism that suspend the current thread on a regular basisThe scheduler may implement preemption mechanism that suspend the current thread on a regular basis –Make processor allocation more fair If the scheduler data structures are shared, threads can run in multiple processorsIf the scheduler data structures are shared, threads can run in multiple processors

12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Uniprocessor Scheduler

13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Scheduling reschedule is used to give up the processorreschedule is used to give up the processor yield is used to give up the processor temporarilyyield is used to give up the processor temporarily Threads can wait on specific conditionsThreads can wait on specific conditions –sleep_on –Intended for synchronization

14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Scheduling In preemptive multithreading, multiplexing does not require to explicitly invoke yieldIn preemptive multithreading, multiplexing does not require to explicitly invoke yield Switching is driven by signalsSwitching is driven by signals –Force the current thread to yield Race conditions may occur inside yield if signals are not disabledRace conditions may occur inside yield if signals are not disabled Preemptive Multithreading

15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Multiprocessor Scheduling The goal of most languages that support parallel threads is to that there should be no difference from the programmer point of viewThe goal of most languages that support parallel threads is to that there should be no difference from the programmer point of view This is an increasingly more important issue, since a very significant number of machines will be multiprocessors in the near futureThis is an increasingly more important issue, since a very significant number of machines will be multiprocessors in the near future –At least, Intel is trying hard to do this… Multiprocessor thread scheduling requires additional synchronization mechanism that prevent race conditionsMultiprocessor thread scheduling requires additional synchronization mechanism that prevent race conditions

16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Concurrent Programming The two most crucial issues areThe two most crucial issues are –Communication –Synchronization Communication refers to any mechanism that allows one thread to obtain information from anotherCommunication refers to any mechanism that allows one thread to obtain information from another –It is usually based on using shared memory or message passing Synchronization refers to any mechanism that allows the programmer to control the relative order in which operations occur in different threadsSynchronization refers to any mechanism that allows the programmer to control the relative order in which operations occur in different threads

17 COMP 144 Programming Language Concepts Felix Hernandez-Campos Shared Memory Example of synchronization: the semaphoreExample of synchronization: the semaphore –P decrements a counter, and waits till it is non-negative –V increments a counter, waking up waiting threads

18 COMP 144 Programming Language Concepts Felix Hernandez-Campos Message Passing Models (a) processes name each other explicitly(a) processes name each other explicitly (b) senders name input ports(b) senders name input ports (c) a channel abstraction(c) a channel abstraction

19 COMP 144 Programming Language Concepts Felix Hernandez-Campos Ada Example

20 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment Read ScottRead Scott –Sect –Sect –Sect. 8.6 –Sect 12.3 intro –Sect intro