CMPSC 311- Introduction to Systems Programming Module: Concurrency

Similar documents
CMPSC 311- Introduction to Systems Programming Module: Concurrency

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Concurrency and Threads

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

Threads. Threads (continued)

Concurrency: Threads. CSE 333 Autumn 2018

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

Threads (light weight processes) Chester Rebeiro IIT Madras

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

CSE 333 SECTION 9. Threads

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

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010

CS 3305 Intro to Threads. Lecture 6

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

LSN 13 Linux Concurrency Mechanisms

Operating systems fundamentals - B06

pthreads CS449 Fall 2017

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Introduction to PThreads and Basic Synchronization

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

CS 261 Fall Mike Lam, Professor. Threads

Threads. studykorner.org

CS510 Operating System Foundations. Jonathan Walpole

CSE 153 Design of Operating Systems Fall 2018

CSE 333 Lecture fork, pthread_create, select

Processes and Threads

A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs

CSE 306/506 Operating Systems Threads. YoungMin Kwon

Introduction to pthreads

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Roadmap. Concurrent Programming. Concurrent Programming. Communication Between Tasks. Motivation. Tevfik Ko!ar

CSC Systems Programming Fall Lecture - XVIII Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 11 th, 2008

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

High Performance Computing Course Notes Shared Memory Parallel Programming

Computer Science 162, Fall 2014 David Culler University of California, Berkeley Midterm 1 September 29, 2014

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

Multithreaded Programming

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

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

THREADS. Jo, Heeseung

POSIX Threads. HUJI Spring 2011

CSCB09: Software Tools and Systems Programming. Bianca Schroeder IC 460

Concurrent Programming

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

Operating Systems. Lecture 05

Operating System Structure

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

Operating Systems & Concurrency: Process Concepts

CS333 Intro to Operating Systems. Jonathan Walpole

Computer Systems Laboratory Sungkyunkwan University

Concurrent Programming

Multicore and Multiprocessor Systems: Part I

Approaches to Concurrency

The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

Concurrent Programming

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

CSC209H Lecture 11. Dan Zingaro. March 25, 2015

Concurrent Server Design Multiple- vs. Single-Thread

ENCM 501 Winter 2019 Assignment 9

Chapter 4 Concurrent Programming

CS 326: Operating Systems. Process Execution. Lecture 5

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

Processes, Threads, SMP, and Microkernels

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

Overview. Administrative. * HW 2 Grades. * HW 3 Due. Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads

PThreads in a Nutshell

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

Concurrency, Thread. Dongkun Shin, SKKU

Lecture 19: Shared Memory & Synchronization

Threads. lightweight processes

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Thread Concept. Thread. No. 3. Multiple single-threaded Process. One single-threaded Process. Process vs. Thread. One multi-threaded Process

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

CHAPTER 2: PROCESS MANAGEMENT

Computer Systems Laboratory Sungkyunkwan University

CS510 Operating System Foundations. Jonathan Walpole

Concurrency. Johan Montelius KTH

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

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Last class: Today: Thread Background. Thread Systems

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

CISC2200 Threads Spring 2015

Threads. CS3026 Operating Systems Lecture 06

Processes. Process Concept

call connect call read call connect ret connect call fgets Client 2 blocks waiting to complete its connection request until after lunch!

Lecture 2 Process Management

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

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

UNIX Processes. by Armin R. Mikler. 1: Introduction

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

Lecture #23 Concurrent Programming

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

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

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

Threads and Too Much Milk! CS439: Principles of Computer Systems January 31, 2018

Transcription:

CMPSC 311- Introduction to Systems Programming Module: Concurrency Professor Patrick McDaniel Fall 2016

Sequential Programming Processing a network connection as it arrives and fulfilling the exchange completely is sequential processing i.e., connections are processed in sequence of arrival listen_fd = Listen(port); while(1) { client_fd = accept(listen_fd); buf = read(client_fd); write(client_fd, buf); close(client_fd); } Page 2

Whither sequential? Benefits super simple to build very little persistent state to maintain Disadvantages Sometimes poor performance one slow client causes all others to block poor utilization of network, CPU cannot interleave processing Think about it this way: if the class took the final exam sequentially, it would take 17 days, 12 hours to complete Page 3

An alternate design... Why not process multiple requests at the same time, interleaving processing while waiting for other actions (e.g., read requests) to complete? This is known as concurrent processing... Process multiple requests concurrently We have/will explore three concurrency approaches 1. Event programming using select() [see last lecture] 2. Creating child processes using fork() 3. Creating multithreaded programs using pthreads Page 4

Concurrency with processes The server process blocks on accept(), waiting for a new client to connect when a new connection arrives, the parent calls fork() to create another process the child process handles that new connection, and exit() s when the connection terminates Children become zombies after death wait() to reap children Page 5

Graphically client server server fork() child client server client server client server client server Page 6

fork() The fork function creates a new process by duplicating the calling process. pid_t fork(void); The new child process is an exact duplicate of the calling parent process, except that it has its own process ID and pending signal queue The fork() function returns 0 (zero) for the child process OR... The child s process ID in the parent code Idea: think about duplicating the process state and running... Page 7

Process control Parent fork (pid == child PID) wait for child to complete (maybe) Child begins at fork (pid == 0) runs until done calls exit Page 8

exit() The exit causes normal process termination with a return value Where void exit(int status); status is sent to the to the parent (&0377) Note: exit vs. return from a C program return simply returns a normal execution exit kills the process via system call Consequence: return is often cleaner for certain things that run atexit (more of a C++ concern) Page 9

wait() The wait function is used to wait for state changes in a child of the calling process (e.g., to terminate) Where pid_t wait(int *status); returns the process ID of the child process status is return value set by the child process Page 10

wait() and the watchdog Often you will want to safeguard a process by watching it execute. If the process terminates then you take some action (when wait() returns). This is called a watchdog process (or watchdog fork) Idea: you fork the process and simply wait for it to terminate Take some action based on the idea that the termination should not have happened or that some recovery is needed. e.g., restart a web server when it crashes Watchdogs are very often used for critical services. Page 11

Putting it all together... int main( void ) { pid_t childpid; // Child process ID int status; // Childs exist status printf( "Stating our process control example...\n" ); childpid = fork(); // Fork the process } if ( childpid >= 0 ) { if ( childpid == 0 ) { // In child process printf( "Child processing,\n" ); sleep( 1 ); exit( 19 ); } else { // In parent process printf( "Parent processing (child=%d),\n", childpid ); wait( &status ); printf( "Child exited with status=%d.\n", WEXITSTATUS(status) ); } } else { perror( "What the fork happened" ); exit( -1 ); } return( 0 ); $./forker Stating our process control example... Parent processing (child=3530), Child processing. Child exited with status=19. $ Page 12

Process Control Tradeoffs Benefits almost as simple as sequential in fact, most of the code is identical! parallel execution; good CPU, network utilization often better security (isolation) Disadvantages processes are heavyweight relatively slow to fork context switching latency is high communication between processes is complicated Page 13

Concurrency with threads A single process handles all of the connections but, a parent thread forks (or dispatches) a new thread to handle each connection the child thread: handles the new connection exits when the connection terminates Note: you can create as many threads as you want (up to a system limit) Page 14

Threads A thread is defined as an independent stream of instructions that can be scheduled to run as such by the operating system. To the software developer, the concept of a "procedure" that runs independently from its main program. To go one step further, imagine a main program that contains a number of procedures. Now imagine all of these procedures being able to be scheduled to run simultaneously and/or independently by the operating system. That would describe a "multi-threaded" program. Idea: forking multiple threads of execution in one process! Page 15

Threads (cont.) accept( ) server Page 16

Threads (cont.) client connect accept( ) server Page 17

Threads (cont.) client pthread_create( ) server Page 18

Threads (cont.) client server Page 19

Threads (cont.) client client pthread_create( ) server Page 20

Threads (cont.) client client client client shared data structures client client server Page 21

Threads (cont.) UNIX Process... and with threads Page 22

Threads (cont.) This independent flow of control is accomplished because a thread maintains its own: Stack pointer Registers Scheduling properties (such as policy or priority) Set of pending and blocked signals Thread specific data. Page 23

Thread Summary Exists within a process and uses the process resources Has its own independent flow of control as long as its parent process exists and the OS supports it Duplicates only the essential resources it needs to be independently schedulable May share the process resources with other threads that act equally independently (and dependently) Dies if the parent process dies - or something similar Is "lightweight" because most of the overhead has already been accomplished through the creation of its process. Page 24

Caveots Because threads within the same process share resources: Changes made by one thread to shared system resources (such as closing a file) will be seen by all other threads. Two pointers having the same value point to the same data. Reading and writing to the same memory locations is possible, and therefore requires explicit synchronization by the programmer. Page 25

Thread control main pthread_create() (create thread) wait for thread to finish via pthread_join() (maybe) thread begins at function pointer runs until pthread_exit() Page 26

pthread_create() The pthread_create function starts a new thread in the calling process. int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); Where, thread is a pthread library structure holding thread info attr is a set of attributes to apply to the thread start_routine is the thread function pointer arg is an opaque data pointer to pass to thread Page 27

pthread_join() The pthread_join function waits for the thread specified by thread to terminate. int pthread_join(pthread_t thread, void **retval); Where, thread is a pthread library structure holding thread info retval is a double pointer return value Page 28

pthread_exit() The pthread_exit function terminates the calling thread and returns a value Where, void pthread_exit(void *retval); retval is a pointer to a return value Note: better be dynamically allocated because the thread stack will go away when the thread exits Page 29

Putting it all together... typedef struct { int num; const char *str; } MY_STRUCT; void * thread_function( void * arg ) { MY_STRUCT *val = (MY_STRUCT *)arg; // Cast to expected type printf( "Thread %lx has vaules %x,%s]\n", pthread_self(), val->num, val->str ); pthread_exit( &val->num ); } int main( void ) { MY_STRUCT v1 = { 0x12345, "Val 1" }; MY_STRUCT v2 = { 0x54312, "Va1 2" }; pthread_t t1, t2; printf( "Starting threads\n" ); pthread_create( &t1, NULL, thread_function, (void *)&v1 ); pthread_create( &t2, NULL, thread_function, (void *)&v2 ); pthread_join( t1, NULL ); pthread_join( t2, NULL ); } printf( "All threads returned\n" ); return( 0 ); $./concurrency Starting threads Thread 7f51c3e05700 has vaules 54312,Va1 2] Thread 7f51c4606700 has vaules 12345,Val 1] All threads returned $ Page 30

Thread pooling A systems design in which a number of threads are pre-created and assigned to requests/tasks as needed Once they are done processing, they go back on the queue Idea: amortize the cost of thread creation over many requests Page 31

Challenge... Consider the following function: int accounts[5] = { 0, 0, 0, 0, 0 }; int addvalue( int account, int amount ) { int num = accounts[account]; num = num+amount; accounts[account] = num; return( 0 ); } Now suppose thread a executes the following addvalue( 1, 100 );... at the same time thread b executes addvalue( 1, 200 ); What is the final value of accounts[1]? Page 32

Watch the execution! Q: What happened? A: The temporary value of shared held stomped on the other thread s data/computation. This is a known as a race condition The OS course will teach you how to handle this using synchronization via MUTEXes (mutual exclusion) Page 33

Thread tradeoffs Benefits straight-line code, line processes or sequential still the case that much of the code is identical! parallel execution; good CPU, network utilization lower overhead than processes shared-memory communication is possible Disadvantages synchronization is complicated shared fate within a process; one rogue thread can hurt you security (no isolation) Page 34

Approximating System Performance A real question asks what performance improvements can one expect out of a concurrent system. We consider the performance of the system in terms of transactions (transactions per second) Each transaction in a concurrent system consists of 1. Processing time (computing the result) 2. Wait time (waiting for I/O, networks, DB ) 3. Overheads (thread overheads, process scheduling..) Note sequential systems do not have this overhead How do we calculate performance? Page

Performance (sequential) Processor P performance is measured in instructions per second (P ips ) Transactions t consists of t = t c + t w (computation plus wait) Transactions per second (for a sequential system): TPS(P, t) = P ips t Processor Scheduling Transaction thread t1 t2 t3 t4 t c t w t c t c t w t c t c t w t c t c t w t c Page

Performance (concurrent) Processor P performance is measured in instructions per second (P ips ) Transactions t consists of t = t c + t w (computation plus wait) Per transaction overhead r o (thread/process management) Transactions per second (for a concurrent system): TPS(P, t) = P ips t c +r o Processor Scheduling t1 t c t c thread t2 t3 t c t c t c t c t4 t c t c overhead Page

Example Consider a system: P ips = 750000 t c = 1000 t w = 500 r o = 25 Sequential system : TPS s = 750000 1000+500 Concurrent system : TPS c = 750000 1000+25 = 500 TPS = 731.7 TPS Improvement : TPS c TPS s TPS s = 231.7 500 = 46.34% Page