Concurrent Programming

Similar documents
Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Process Synchronization(2)

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Classic Problems of Synchronization

Lecture 6. Process Synchronization

Process Management And Synchronization

Process Synchronization(2)

Process Synchronization(2)

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Introduction to OS Synchronization MOS 2.3

Chapter Machine instruction level 2. High-level language statement level 3. Unit level 4. Program level

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Process Synchronization

Performance Throughput Utilization of system resources

Process Synchronization. studykorner.org

Lesson 6: Process Synchronization

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

Chapter 6: Process Synchronization

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

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

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

CS370 Operating Systems Midterm Review

PROCESS SYNCHRONIZATION

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

IT 540 Operating Systems ECE519 Advanced Operating Systems

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

CS370 Operating Systems

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

CS3502 OPERATING SYSTEMS

Chapter 3: Processes. Operating System Concepts 8th Edition,

Chapter 3: Process Concept

Chapter 3: Process Concept

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

UNIX Input/Output Buffering

CS510 Operating System Foundations. Jonathan Walpole

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Chapter 3: Process Concept

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

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Multitasking / Multithreading system Supports multiple tasks

Concurrency and Synchronisation

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

CS370 Operating Systems

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Process Synchronization

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Chapter 6: Process Synchronization

Administrivia. Assignments 0 & 1 Class contact for the next two weeks Next week. Following week

Interprocess Communication By: Kaushik Vaghani

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

CS 318 Principles of Operating Systems

1 Process Coordination

Roadmap. Tevfik Ko!ar. CSC Operating Systems Spring Lecture - III Processes. Louisiana State University. Virtual Machines Processes

Silberschatz and Galvin Chapter 6

Concurrency: a crash course

File Access. FILE * fopen(const char *name, const char * mode);

Models of concurrency & synchronization algorithms

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

Semaphores. Mutual Exclusion. Inference Rules. Fairness: V may release a waiting process. Weakly fair, Strongly fair, or FIFO

Module 6: Process Synchronization

The Kernel Abstraction. Chapter 2 OSPP Part I

Today: Process Management. The Big Picture So Far. What's in a Process? Example Process State in Memory

518 Lecture Notes Week 3

CS420: Operating Systems. Process Synchronization

Operating System Study Notes Department of Computer science and Engineering Prepared by TKG, SM and MS

The Big Picture So Far. Today: Process Management

Operating System Structure

Semaphores INF4140. Lecture 3. 0 Book: Andrews - ch.04 ( ) INF4140 ( ) Semaphores Lecture 3 1 / 34

Semaphores. Derived Inference Rules. (R g>0) Q g (g 1) {R} V (g) {Q} R Q g (g+1) V (s), hs := 1; i

Chapter 13 Topics. Introduction. Introduction

Concurrency(I) Chapter 5

CSE Traditional Operating Systems deal with typical system software designed to be:

CS370 Operating Systems

Concurrency and Synchronisation

Chapter 13. Concurrency ISBN

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

Process Synchronization

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

Chapter 7: Process Synchronization. Background. Illustration

CS 350 : COMPUTER SYSTEM CONCEPTS SAMPLE TEST 2 (OPERATING SYSTEMS PART) Student s Name: MAXIMUM MARK: 100 Time allowed: 70 minutes

Chapter 7: Process Synchronization!

Concept of a process

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

CS510 Operating System Foundations. Jonathan Walpole

Processes & Threads. Recap of the Last Class. Microkernel System Architecture. Layered Structure

Chapter 3: Processes. Operating System Concepts 8 th Edition,

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

CS3733: Operating Systems

Transcription:

Concurrent Programming Yuh-Jzer Joung Dept. of Information Management National Taiwan University May, 2001 CONCURRENT PROGRAMMING Operations in the source text are concurrent if they could be, but need not be, executed in parallel. Operations that occur one after the other, ordered in time, are said to be sequential. The fundamental concept of concurrent programming is the notion of a process, which corresponds to a sequential computation, with its own thread of control. The thread of a sequential computation is the sequence of program points that are reached as control flows through the source text of the program. Spring, 2001 Concurrent Programming 2 1

THREAD #include <stdio.h> /* copy input to output */ main() { int c; } c = getchar(); while (c!= EOF) { putchar(c); c =getchar(); } putchar( \n ); Spring, 2001 Concurrent Programming 3 INTERACTIONS BETWEEN PROCESSES Communication: involves the exchange of data between processes, either by an explicit message or through the values of shared variables. Synchronization: relates the thread of one process with that of another. P Q Synchronization can be used to constraint the order in which P reaches x and Q reaches y. x y Interactions between processes can also be visualized in terms of competition and cooperation between processes. Spring, 2001 Concurrent Programming 4 2

HARDWARE ARCHITECTURES PROCESSOR PROCESSOR PROCESSOR PROCESSOR PROCESSOR MEMORY MEMORY MEMORY MEMORY (a) Single processor (b) Shared memory (c) Distributed machine Spring, 2001 Concurrent Programming 5 AN ADA PROGRAM with text_io; use text_io; -- import character input/output procedures procedure hello is put_line( hello world ); end hello; Spring, 2001 Concurrent Programming 6 3

AN ADA PROGRAM with text_io; use text_io; procedure identify is task p; task body p is put_line( p ); end p; task q; task body q is put_line( q ); end q; put_line( r ); end identify; -- task specification for p -- task specification for q -- procedure body sets up parent of p and q Spring, 2001 Concurrent Programming 7 THE DINING PHILOSOPHERS The design issues: how to avoid deadlock and livelock? how to guarantee fairness (avoid starvation)? Spring, 2001 Concurrent Programming 8 4

CRITICAL SECTIONS A critical section in a process is a portion or section of code that must be treated as an atomic event. Two critical sections are said to be mutually exclusive because their execution must not overlap. In a sequential language, the following two statements increment x by 3, but in a concurrent language, the following two statements may be executed in parallel by two different threads, thereby producing three different outcomes. x:=x+1; x:=x+2 P Q critical section Spring, 2001 Concurrent Programming 9 A RENDEZVOUS A rendezvous combines two events: 1. A call within a client process P. 2. Acceptance of the call by the server process Q. P Q Q.synch call return accept synch do end synch end P end Q Spring, 2001 Concurrent Programming 10 5

RENDEZVOUS init INITAILIZES TASKS OF TYPE emitter with text_io; use text_io; procedure task_init is task type emitter is entry init(c : character) end emitter; p, q : emitter; task body emitter is me : character; accept init(c : character) do me := c; end init put(me); new_line; end emitter p.init( p ); q.init( q ); put( r ); new_line; end task_init; Spring, 2001 Concurrent Programming 11 THREADS FO THE TASKS SET UP BY THE PROGRAM task p procedure task_init task q accept init call p.init end init return q.init call accept init return end init end p end end q Spring, 2001 Concurrent Programming 12 6

SELECTIVE ACCEPTANCE The select construct in Ada allows a server to offer a selection of services to its clients. select accept deliver_milk do end deliver_milk; or accept deliver_juice do end deliver_juice; end select; Spring, 2001 Concurrent Programming 13 GUARDED SELECTIVES Alternatives in a select command can also be guarded. select when notfull accept enter(c : in character) do end enter; or when notempty accept leave(c : out character) do end leave; end select; Spring, 2001 Concurrent Programming 14 7

DYNAMIC CREATION OF TASKS THROUGH ACCESS TYPES with text_io; use text_io; procedure pointers is task type emitter is entry init(c : character) end emitter; type emitter_ptr is access emitter; p, q : emitter; task body emitter is me : character; accept init(c : character) do me := c; end init; put(me); new_line; end emitter Spring, 2001 Concurrent Programming 15 DYNAMIC CREATION OF TASKS THROUGH ACCESS TYPES (cont.) p := new emitter; q := new emitter; p.init( p ); q.init( q ); put( r ); new_line; end pointers; Spring, 2001 Concurrent Programming 16 8

MONITOR Program Process SUB1 Process SUB2 Process SUB3 Insert Remove Monitor B U F F E R Process SUB4 Spring, 2001 Concurrent Programming 17 A PROGRAM USING A MONITOR TO CONTROL ACCESS TO A SHARED BUFFER type databuf = monitor const bufsize = 100; var buf : array [1..bufsize] of integer; next_in; next_out : 1..bufsize; filled : 0..bufsize; sender_q; receiver_q : queue; procedure entry deposit(item : integer); if filled = bufsize then delay(sender_q); buf[next_in] := item; next_in := (next_in mod bufsize) + 1; filled := filled + 1; continue(receiver_q); end; Spring, 2001 Concurrent Programming 18 9

A PROGRAM USING A MONITOR TO CONTROL ACCESS TO A SHARED BUFFER procedure entry fetch(var item : integer); if filled = 0 then delay(receive_q); item := buf[next_out]; next_out := (next_out mod bufsize) + 1; filled := filled 1; continue(sender_q) end; filled := 0; next_in := 1; next_out := 1; end; Spring, 2001 Concurrent Programming 19 A PROGRAM USING A MONITOR TO CONTROL ACCESS TO A SHARED BUFFER type producer = process(buffer : databuf); var newvalue : integer; cycle -- produce newvalue -- buffer.deposit(newvalue); end end; type consumer = process(buffer : databuf) var stored_value : integer; cycle buffer.fetch(stored_value); -- consume stored_value -- end end; Spring, 2001 Concurrent Programming 20 10

A PROGRAM USING A MONITOR TO CONTROL ACCESS TO A SHARED BUFFER -- type declarations -- var new_producer : producer; new_consumer : consumer; new_buffer : databuf; init new_buffer, new_producer(new_buffer), new_consumer(new_buffer); end; Spring, 2001 Concurrent Programming 21 SOLUTIONS TO THE PRODUCER- CONSUMER PROBLEM Producer Consumer (a) Direct access Producer Consumer (b) Synchronized direct access Spring, 2001 Concurrent Programming 22 11

SOLUTIONS TO THE PRODUCER- CONSUMER PROBLEM (cont.) Producer procedure enter(char x); procedure leave() : char; Consumer (c) Access through a monitor Producer Consumer Buffer (d) The buffer as a separate process Spring, 2001 Concurrent Programming 23 PSEUDOCODE FOR UNSYNCHRONIZED ACCESS TO THE BUFFER with text_io; use text_io; procedure direct is size : constant integer := 5; buf : array(0..size-1) of character; front, rear : integer := 0; function notfull return boolean is end notfull; function notempty return boolean is end notempty; task producer; task body producer is c : character; while not end_of_file loop end if end loop; end producer if notfull then get(c); buf(rear) := c; rear := (rear +1) mod size; Spring, 2001 Concurrent Programming 24 12

PSEUDOCODE FOR UNSYNCHRONIZED ACCESS TO THE BUFFER (cont.) task consumer; task body consumer is c : character; loop if notempty then c := buf(front); front := (front+1) mod size; end if; end loop; end consumer; put(c); null; end direct; Spring, 2001 Concurrent Programming 25 SEMAPHORES: MUTUAL EXCLUSION A semaphore is a construct that has an integer variable value and supports two operations: 1. If value 1, then a process can perform a p operation to decrement the value by 1. Otherwise, a process attempting a p operation waits until the value becomes greater than or equal to 1. 2. A process can perform a v operation to increment variable value by 1. A binary semaphore is a semaphore whose value is constrained to be either 0 or 1. If the value of a binary semaphore is 1, then a process attempting a v operation on it is suspended until its value becomes 0. In other words, the p and v operations on a semaphore must be performed alternately. Spring, 2001 Concurrent Programming 26 13

IMPLEMENTATIONS OF SEMAPHORES task type binary_semaphore is entry p; entry v; end binary_semaphore; task body binary_semaphore is loop accept p; accept v; end loop; end binary_semaphore; Spring, 2001 Concurrent Programming 27 MUTUAL EXCLUSION Mutual exclusion can be implemented by enclosing each critical section between the operations s.p and s.v, where s is a binary semaphore: process Q process R s.p; s.p; critical section for Q; critical section for R; s.v s.v... Spring, 2001 Concurrent Programming 28 14

THE PRODUCER AND CONSUMER AS CYCLIC PROCESSES WITH CRITICAL SECTIONS producer consumer if notfull then get(c); buf(rear) := c; update rear; end if if notempty then c := buf(front); update front; put(c); end if... Spring, 2001 Concurrent Programming 29 A SEMAPHORE AS A TASK IN ADA task body semaphore is value : integer; accept init(n : integer) do -- initialization value := n; end init; loop select when value 1 -- p operation accept p do value := value 1; end p; or accept v do -- v operation value := value + 1; end v; end select; end loop; end semaphore; Spring, 2001 Concurrent Programming 30 15

USE OF THE SEMAPHORES filling, emptying, AND critical task body producer is c : character; while not end_of_file loop get(c); filling.p; critical.p; buf(rear) := c; rear := (rear+1) mod size; critical.v; emptying.v; end loop; end producer; task body consumer is c : character; loop emptying.p; critical.p; c = buf(front); front := (front+1) mod size; critical.v; filling.v; put(c); end loop; end consumer; Spring, 2001 Concurrent Programming 31 A MONITOR FOR A BOUNDED BUFFER monitor buffer is buf : ; procedure enter(c : in character); if buffer full then wait(filling); -- block producer enter c into buffer; signal(empty); -- unblock consumer end enter; procedure leave(c : out character); if buffer empty then wait(emptying); -- block consumer c := next character; signal(filling); -- unblock producer end leave; initialize private data; end buffer; Spring, 2001 Concurrent Programming 32 16

THE BUFFER AS A PROCESS task body buffer is <data-declarations> loop select when notfull accept enter(x : in integer) do end enter; or end select end loop end buffer; when notempty accept leave(x : out integer) do end leave; Spring, 2001 Concurrent Programming 33 Two Possible Execution Control Sequences for Two Coroutines Without loops resume from master A resume B resume B resume B B resume A resume A Spring, 2001 Concurrent Programming 34 17

Two Possible Execution Control Sequences for Two Coroutines Without loops A resume B resume B resume from master B resume A resume A Spring, 2001 Concurrent Programming 35 Coroutine execution sequence with loops resume from master A resume B First resume Subsequent resume B resume A Spring, 2001 Concurrent Programming 36 18

UNIX PROCESSES A program can be any of several things: A file containing instructions and data used to initialize a process; An algorithm represented in the source code of some programming language, probably stored in a file; A process, briefly, is a running program. Processes are resources that are managed by the operating system (OS). Spring, 2001 Concurrent Programming 37 THE COMPONENTS OF A PROCESS the text (code) segment the user data segment (on modern Unix systems divided into the initialized and uninitialized (called bss) data segments) the system data segment Spring, 2001 Concurrent Programming 38 19

The fork and exec System Calls The exec system call is the only way a process s execution; the fork system call is the only way to create a new process. /* ignoring errors... */ if (fork() == 0) { /* i am the child */ exec("new program"); } else { /* i am the parent */ wait(); } Spring, 2001 Concurrent Programming 39 The exec System Calls exec in all its forms overlays a new process image on an old process. The new process image is constructed from an ordinary, executable file. There can be no return from a successful exec because the calling process image is overlaid by the new process image. If exec returns to the calling process, an error has occurred; the return value is -1 and errno is set to indicate the error. The system() function forks to create a child process that in turn execs the shell in order to execute string. If the fork() or exec() fails, system() returns a value of -1 and sets errno. Spring, 2001 Concurrent Programming 40 20

The fork System Calls fork creates a child process that differs from the parent process only in its PID and PPID, and in the fact that resource utilizations are set to 0. File locks and pending signals are not inherited. On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. On failure, a -1 will be returned in the parent's context, no child process will be created, and errno will be set appropriately. Because the child process is an exact copy of the parent, both processes pick up execution from the next statement after fork. They share identical all resources the original one had at the time of fork()ing, but not any allocated later. Spring, 2001 Concurrent Programming 41 PROCESS CREATION IN UNIX int pid; int status = 0; if (pid = fork()) { /* parent */.. pid = wait(&status); } else { /* child */.. exit(status); } Another example Spring, 2001 Concurrent Programming 42 21