Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
Concurrency. What is Concurrency Ability to execute two operations at the same time Physical concurrency –multiple processors on the same machine –distributing.
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
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.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
Real-Time Kernels and Operating Systems. Operating System: Software that coordinates multiple tasks in processor, including peripheral interfacing Types.
Processes April 5, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Phones OFF Please Processes Parminder Singh Kang Home:
Fundamentals of Python: From First Programs Through Data Structures
Introduction to Processes CS Intoduction to Operating Systems.
Threads, Thread management & Resource Management.
Implementing Processes and Process Management Brian Bershad.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Processes & Threads Emery Berger and Mark Corner University.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Processes CSCI 4534 Chapter 4. Introduction Early computer systems allowed one program to be executed at a time –The program had complete control of the.
NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
4300 Lines Added 1800 Lines Removed 1500 Lines Modified PER DAY DURING SUSE Lab.
1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R E L E V E N Concurrent Programming.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Threads. Readings r Silberschatz et al : Chapter 4.
Slides created by: Professor Ian G. Harris Operating Systems  Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled.
Introduction Contain two or more CPU share common memory and peripherals. Provide greater system throughput. Multiple processor executing simultaneous.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
Processes and threads.
Operating Systems Review ENCE 360.
Process Management Process Concept Why only the global variables?
Chapter 3: Process Concept
Chapter 3: Process Concept
Section 10: Last section! Final review.
Processes A process is a running program.
Concurrent Programming
CSE 451: Operating Systems Autumn 2003 Lecture 5 Threads
Processes Hank Levy 1.
CSE 451: Operating Systems Winter 2003 Lecture 5 Threads
Concurrency: Mutual Exclusion and Process Synchronization
Threaded Programming in Python
Jonathan Walpole Computer Science Portland State University
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
- When you approach operating system concepts there might be several confusing terms that may look similar but in fact refer to different concepts:  multiprogramming, multiprocessing, multitasking,
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CS510 Operating System Foundations
Processes Hank Levy 1.
CS703 – Advanced Operating Systems
CSE 451: Operating Systems Winter 2001 Lecture 5 Threads
Ch 3.
Chapter 3: Process Management
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

Concurrent Programming

Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:  A web server can handle connections from several clients while still listening for new connections.  A multi-player game may allow several players to move things around the screen at the same time. How can we do this? How to the tasks communicate with each other?

Performing Concurrency A computer can implement concurrency by...  parallel execution: run tasks on different CPU in the same computer (requires multi-processor machine)  time-sharing: divide CPU time into slices, and multi- process several tasks on the same CPU  distributed computing: use CPU of several computers in a cluster to run different tasks tasks time

Design for Concurrency If we have multiple processes or threads of execution for a single job, we must decide issues of...  Allocating CPU: previous slide  Memory: do all tasks share the same memory? Or separate memory?  Communication: how can tasks communicate?  Control: how do we start a task? stop a task? wait for a task?

Memory  shared memory: if one thread changes memory, it effects the others. problem: how to you share the stack?  separate memory: each thread has its own memory area  combination: each thread has a separate stack area they share a static area, may share the current environment, may compete for same heap.

Shared Memory and Environment /* fork( ) creates new process */ pid = vfork( ); if ( pid == 0 ) { /* child process, pid=0 */ task1( ); task3( ); } else { /* parent process, pid=child */ task2( ); } free space task1() frame main stack: SP Tasks don't share registers. After the parent calls task1() where is the Stack Pointer (SP) of the child process? Where will task2( ) be placed?

Processes and memory  Heavy-weight processes: each process gets its own memory area and own environment. Unix "process" fits this model.  Light-weight processes: processes share the same memory area; but each has its own context and maybe its own stack. "threads" in Java, C, and C# fit this model

Heavy-weight Processes  UNIX fork( ) system call: child process gets a copy of parent's memory pages

Example: Web Server /* bind to port 80 */ bind( socket, &address, sizeof(address) ); while ( 1 ) { /* run forever */ /* wait for a client to connect */ client = accept(socket, &clientAddress, &len ); /* fork a new process to handle client */ pid = fork( ); if ( pid == 0 ) handleClient( client, clientAddress ); } Server forks a new process to handle clients, so server can listen for more connections.

Example: fork and wait for child pid = fork( ); if ( pid == 0 ) childProcess( ); else { wait( *status ); // wait for child to exit } wait( ) cause the process to wait for a child to exit.

Threads: light-weight processes  Threads share memory area.  Conserve resources, better communication between tasks. task1 = new Calculator( ); task2 = new AlarmClock( ); Thread thread1 = new Thread( task1 ); Thread thread2 = new Thread( task2 ); thread1.start( ); thread2.start( );

States of a Thread

Stack Management for Threads  In some implementations (like C) threads share the same memory, but require their own stack space.  Each thread must be able to call functions separately. thread2 stack space main stack: thread3 stack space thread1 stack thread4 stack thread5 stack Cactus Stack: Dynamic and static links can refer to parent's stack.

Communication between Tasks  Reading and writing to a shared buffer. Producer - consumer model (see Java Tutorial)  Using an I/O channel called a pipe.  Signaling: exceptions or interrupts. pin = new PipedInputStream( ); pout = new PipedOutputStream( pin ); task1 = new ReaderTask( pin ); task2 = new WriterTask( pout ); Thread thread1 = new Thread( task1 ); Thread thread2 = new Thread( task2 ); thread1.start( ); thread2.start( ); task1 task2 pipe

Thread Coordination thread1thread2 processing sleeping notify( ); wait( ); processing notify( ); wait( ); sleeping yield( ); yield gives other threads a chance to use CPU.

Critical Code: avoiding race conditions Example: one thread pushes data onto a stack, another thread pops data off the stack. Problem: you may have a race condition where one thread starts to pop data off stack. but thread is interrupted (by CPU) and other thread pushes data onto stack. race this a is push("problem?") { n = top; stack[n] = "problem?"; top=n++; top pop() { return stack[top--]; } thread1: this a is problem? thread2:

Exclusive Access to Critical Code programmer control: use a shared flag variable or semaphore to indicate when critical block is free executer control: use synchronization features of the language to restrict access to critical code public void synchronized push(Object value) { if ( top < stack.length ) stack[top++] = value; } public Object synchronized pop( ) { if ( top >= 0 ) return stack[top--]; }

Avoiding Deadlock  Deadlock: when two or more tasks are waiting for each other to release a required resource.  Program waits forever.  Rule for Avoiding Deadlock: exercise for students

Design Patterns and Threads Observer Pattern: one task is a source of events that other tasks are interested in. Each task wants to be notified when an interesting event occurs. Solution: wrap the source task in an Observable object. Other tasks register with Observable as observers. Observable task calls notifyObservers ( ) when interesting event occurs

Simple Producer-Consumer Cooperation Using Semaphores Figure 11.2

Multiple Producers-Consumers Figure 11.3

Producer-Consumer Monitor Figure 11.4

States of a Java Thread Figure 11.5

Ball Class Figure 11.6

Initial Application Class Figure 11.7

Final Bouncing Balls init Method Figure 11.8

Final Bouncing Balls paint Method Figure 11.9

Bouncing Balls Mouse Handler Figure 11.10

Bouncing Balls Mouse Handler Figure 11.11

Buffer Class Figure 11.12

Producer Class Figure 11.13

Consumer Class Figure 11.14

Bounded Buffer Class Figure 11.15

Sieve of Eratosthenes Figure 11.16

Test Drive for Sieve of Eratosthenes Figure 11.17