Concurrent Programming

Similar documents
Total Score is updated. The score of PA4 will be changed! Please check it. Will be changed

Computer Systems Laboratory Sungkyunkwan University

Concurrent Programming

Concurrent Programming. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Announcement (1) Due date for PA3 is changed (~ next week) PA4 will also be started in the next class. Not submitted. Not scored

Concurrent Programming

Concurrent Programming is Hard! Concurrent Programming. Reminder: Iterative Echo Server. The human mind tends to be sequential

Lecture #23 Concurrent Programming

Concurrent Programming

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

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

Concurrent Programming

Concurrent Programming

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

Concurrent Programming April 21, 2005

Iterative Servers The course that gives CMU its Zip! Iterative servers process one request at a time. Concurrent Servers

Concurrent Programming

Approaches to Concurrency

Concurrent Programming November 28, 2007

Three Basic Mechanisms for Creating Concurrent Flows. Systemprogrammering 2009 Föreläsning 10 Concurrent Servers. Process-Based Concurrent Server

Concurrent Servers Dec 2, 2009"

Concurrent programming

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

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

Concurrent Programming

Threading Language and Support. CS528 Multithreading: Programming with Threads. Programming with Threads

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

Proxy: Concurrency & Caches

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Carnegie Mellon Performance of Mul.threaded Code

ANSI/IEEE POSIX Standard Thread management

Threads. Jo, Heeseung

CS 5523 Operating Systems: Thread and Implementation

Threads. CS 475, Spring 2018 Concurrent & Distributed Systems

Threads (SGG 4) Outline. Traditional Process: Single Activity. Example: A Text Editor with Multi-Activity. Instructor: Dr.

CS Operating Systems: Threads (SGG 4)

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

Today. Threads review Sharing Mutual exclusion Semaphores

CS333 Intro to Operating Systems. Jonathan Walpole

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

pthreads CS449 Fall 2017

Concurrent Programming

Threads. Jo, Heeseung

CS510 Operating System Foundations. Jonathan Walpole

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CSE 306/506 Operating Systems Threads. YoungMin Kwon

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

Concurrent Programming

Last class: Today: Thread Background. Thread Systems

CS 3305 Intro to Threads. Lecture 6

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Recitation 14: Proxy Lab Part 2

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Threads. studykorner.org

CS 3723 Operating Systems: Final Review

Recitation 14: Proxy Lab Part 2

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

CS510 Operating System Foundations. Jonathan Walpole

Computer Systems Laboratory Sungkyunkwan University

Threads. Threads (continued)

Concurrency on x86-64; Threads Programming Tips

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

Synchronization: Basics

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

LSN 13 Linux Concurrency Mechanisms

Concurrency. Johan Montelius KTH

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

THREADS. Jo, Heeseung

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

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

CSE 333 SECTION 9. Threads

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

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

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

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

Parallel Programming with Threads

Threads. lightweight processes

Preview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017

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

CS 261 Fall Mike Lam, Professor. Threads

Thread and Synchronization

Network Programming November 3, 2008

Introduction to PThreads and Basic Synchronization

Introduction to Client-Server Model

CSE 333 Section 9 - pthreads

Agenda. Process vs Thread. ! POSIX Threads Programming. Picture source:

Concurrent Programming. Concurrent Programming with Processes and Threads

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

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

socketservertcl a Tcl extension for using SCM_RIGHTS By Shannon Noe - FlightAware

Parallel Programming. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Synchronization: Advanced

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

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

Chapter 4 Threads. Images from Silberschatz 03/12/18. CS460 Pacific University 1

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

Transcription:

Concurrent Programming Prof. Jin-Soo Kim( jinsookim@skku.edu) TA Jinhong Kim( jinhong.kim@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

Echo Server Revisited int main (int argc, char *argv[]) {... listenfd = socket(af_inet, SOCK_STREAM, 0); bzero((char *)&saddr, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(inaddr_any); saddr.sin_port = htons(port); bind(listenfd, (struct sockaddr *)&saddr, sizeof(saddr)); } listen(listenfd, 5); while (1) { connfd = accept(listenfd, (struct sockaddr *)&caddr, &clen); while ((n = read(connfd, buf, MAXLINE)) > 0) { printf ("got %d bytes from client.\n", n); write(connfd, buf, n); } close(connfd); } 2

Iterative Servers (1) One request at a time client 1 server client 2 call connect ret connect call read ret read close call accept ret accept write close call accept ret accept write close call connect ret connect call read ret read close 3

Iterative Servers (2) Fundamental flaw call connect ret connect call fgets User goes out to lunch Client 1 blocks waiting for user to type in data client 1 server client 2 Server blocks waiting for data from Client 1 call accept ret accept call read Solution: use concurrent servers instead Use multiple concurrent flows to serve multiple clients at the same time. call connect Client 2 blocks waiting to complete its connection request until after lunch! 4

Concurrent Programming Thread-based

Traditional View Process = process context + address space Process context Program context: Data registers Condition codes Stack pointer (SP) Program counter (PC) Kernel context: VM structures Descriptor table brk pointer SP brk PC 0 Code, data, and stack stack shared libraries run-time heap read/write data read-only code/data 6

Alternate View Process = thread context + kernel context + address space Thread (main thread) Code and Data SP stack Thread context: Data registers Condition codes Stack pointer (SP) Program counter (PC) brk PC 0 shared libraries run-time heap read/write data read-only code/data Kernel context: VM structures Descriptor table brk pointer 7

A Process with Multiple Threads Multiple threads can be associated with a process. Each thread has its own logical control flow (sequence of PC values) Each thread shares the same code, data, and kernel context Each thread has its own thread id (TID) Thread 1 (main thread) Shared code and data stack 1 Thread 1 context: Data registers Condition codes SP1 PC1 shared libraries run-time heap read/write data read-only code/data 0 Kernel context: VM structures Descriptor table brk pointer Thread 2 (peer thread) stack 2 Thread 2 context: Data registers Condition codes SP2 PC2 8

Logical View of Threads Threads associated with a process form a pool of peers Unlike processes which form a tree hierarchy Threads associated with process foo Process hierarchy T1 T2 shared code, data and kernel context T4 P0 P1 sh sh sh T5 T3 foo 9

Threads vs. Processes How threads and processes are similar Each has its own logical control flow. Each can run concurrently. Each is context switched. How threads and processes are different Threads share code and data, processes (typically) do not. Threads are somewhat less expensive than processes. Linux 2.4 Kernel, 512MB RAM, 2 CPUs -> 1,811 forks()/second -> 227,611 threads/second (125x faster) 10

Pthreads Interface POSIX Threads Interface Creating and reaping threads pthread_create() pthread_join() Determining your thread ID pthread_self() Terminating threads pthread_cancel() pthread_exit() exit (terminates all threads), return (terminates current thread) Synchronizing access to shared variables pthread_mutex_init() pthread_mutex_[un]lock() pthread_cond_init() pthread_cond_[timed]wait() pthread_cond_signal(), etc. 11

hello, world Program (1) /* * hello.c - Pthreads "hello, world" program */ #include "pthread.h" void *thread(void *vargp); int main() { pthread_t tid; } pthread_create(&tid, NULL, thread, NULL); pthread_join(tid, NULL); exit(0); /* thread routine */ void *thread(void *vargp) { printf("hello, world!\n"); return NULL; } Thread attributes (usually NULL) Thread arguments (void *p) return value (void **p) 12

hello, world Program (2) Execution of threaded hello, world main thread call pthread_create() pthread_create() returns call Pthread_join() main thread waits for peer thread to terminate pthread_join() returns peer thread printf() return NULL; (peer thread terminates) exit() terminates main thread and any peer threads 13

Echo Server: Thread-based int main (int argc, char *argv[]) { int *connfdp; pthread_t tid;... while (1) { connfdp = (int *) malloc(sizeof(int)); *connfdp = accept (listenfd, (struct sockaddr *)&caddr, &caddrlen)); void *thread_main(void *arg) { int n; char buf[maxline]; int connfd = *((int *)arg); pthread_detach(pthread_self()); free(arg); while((n = read(connfd, buf, MAXLINE)) > 0) write(connfd, buf, n); } } pthread_create(&tid, NULL, thread_main, connfdp); } close(connfd); return NULL; 14

Implementation Issues (1) Must run detached to avoid memory leak. At any point in time, a thread is either joinable or detached. Joinable thread can be reaped and killed by other threads Must be reaped (with pthread_join()) to free memory resources. Detached thread cannot be reaped or killed by other threads. Resources are automatically reaped on termination. Exit state and return value are not saved. Default state is joinable. Use pthread_detach(pthread_self()) to make detached. 15

Implementation Issues (2) Must be careful to avoid unintended sharing For example, what happens if we pass the address connfd to the thread routine? int connfd;... pthread_create(&tid, NULL, thread_main, &connfd);... All functions called by a thread must be thread-safe. A function is said to be thread-safe or reentrant, when the function may be called by more than one thread at a time without requiring any other action on the caller s part. 16

Thread-based Designs Pros Easy to share data structures between threads. e.g., logging information, file cache, etc. Threads are more efficient than processes. Cons Unintentional sharing can introduce subtle and hardto-reproduce errors! The ease with which data can be shared is both the greatest strength and the greatest weakness of threads. 17

Concurrent Programming Examples

Example Make chatting server N client + 1 server Server read messages from 1 client, send messages all client Use pthread_mutex 19