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

Similar documents
CSE 333 SECTION 9. Threads

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

LSN 13 Linux Concurrency Mechanisms

POSIX PTHREADS PROGRAMMING

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

Introduction to PThreads and Basic Synchronization

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

Introduction to pthreads

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

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

Threads. Threads (continued)

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

Pthread (9A) Pthread

POSIX Threads. HUJI Spring 2011

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

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

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

CS444 1/28/05. Lab 03

Thread and Synchronization

pthreads CS449 Fall 2017

Operating systems fundamentals - B06

POSIX Threads. Paolo Burgio

CS333 Intro to Operating Systems. Jonathan Walpole

Threads. Jo, Heeseung

Threads. studykorner.org

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

C Grundlagen - Threads

ENCM 501 Winter 2019 Assignment 9

ANSI/IEEE POSIX Standard Thread management

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

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

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

CS510 Operating System Foundations. Jonathan Walpole

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

PThreads in a Nutshell

real time operating systems course

CS 261 Fall Mike Lam, Professor. Threads

Lecture 19: Shared Memory & Synchronization

Creating Threads. Programming Details. COMP750 Distributed Systems

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

This resource describes how to program the myrio in C to perform timer interrupts.

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization

Threads. Jo, Heeseung

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

CS 3305 Intro to Threads. Lecture 6

CS510 Operating System Foundations. Jonathan Walpole

CSE 333 Section 9 - pthreads

TCSS 422: OPERATING SYSTEMS

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

An Application of Pthreads and Mutexes

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

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

Threads. lightweight processes

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

THREADS. Jo, Heeseung

Unix. Threads & Concurrency. Erick Fredj Computer Science The Jerusalem College of Technology

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

Oct 2 and 4, 2006 Lecture 8: Threads, Contd

COMP 3430 Robert Guderian

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

Programming Assignment #4

Parallel Computing: Programming Shared Address Space Platforms (Pthread) Jin, Hai

CPSC 313: Intro to Computer Systems. POSIX Threads. Latency Hiding / Multiprogramming (covered earlier) Ease of Programming (covered now)

Concurrency. Johan Montelius KTH

Concurrent Programming. Concurrent Programming with Processes and Threads

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

Multicore and Multiprocessor Systems: Part I

Pthreads: POSIX Threads

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

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

Chapter 4 Concurrent Programming

Introduction to Threads

Multithreaded Programming

Concurrency and Threads

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Threaded Programming. Lecture 9: Alternatives to OpenMP

POSIX Threads: a first step toward parallel programming. George Bosilca

CS240: Programming in C. Lecture 18: PThreads

CS345 Opera,ng Systems. Φροντιστήριο Άσκησης 2

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Threads (light weight processes) Chester Rebeiro IIT Madras

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

Parallel Programming with Threads

Computer Systems Laboratory Sungkyunkwan University

Last class: Today: Thread Background. Thread Systems

CS370 Operating Systems

CSE 421/521 - Operating Systems Fall 2011 Recitations. Recitation - III Networking & Concurrent Programming Prof. Tevfik Kosar. Presented by...

PRACE Autumn School Basic Programming Models

CS 220: Introduction to Parallel Computing. Condition Variables. Lecture 24

CMPSC 311- Introduction to Systems Programming Module: Concurrency

This exam paper contains 8 questions (12 pages) Total 100 points. Please put your official name and NOT your assumed name. First Name: Last Name:

Reading compiler errors

POSIX Threads and OpenMP tasks

Concurrent Server Design Multiple- vs. Single-Thread

CS370 Operating Systems

Chapter 10. Threads. OS Processes: Control and Resources. A process as managed by the operating system groups together:

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

Section 4: Threads and Context Switching

Transcription:

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

Outline Introduction What is Multi-thread Programming Why to use Multi-thread Programming Basic Pthread Programming Recommended Materials

Socket programming Introduction Server accepts connection requests Exchange data while(1){ int client_sd = accept( ); // Do something } while(1){ int len = recv( ); // Handle received messages }

Introduction Recall the blocking functions in the last tutorial. If we do not have multi-thread programming: The whole program will be blocked waiting for incoming connection requests and data. We cannot handle both with only One thread. while(1){ int len = recv( ); } while(1){ int sd = accept( ); }

What is Multi-thread Programming A thread is a sequence of instructions within a program that can be executed independently of other code. Thread Exists within one process. Has independent flow of control. Duplicates the essential resources only. May share the process resources. Dies if the parent dies. Is lightweight.

Why Multi-thread Programming Multi-thread programming Shared data in one process. A thread can be created with little operation system overhead. Managing threads requires less system resources than managing processes.

Why Multi-thread Programming To accomplish the functionalities of the server within one program, we use multiple threads. The blocking operations, will block one thread instead of the whole program. Thread 1: Thread 2: while(1){ int len = recv( ); } while(1){ int sd = accept( ); }

Basic Pthread Programming To implement a multi-threads program using pthread library: #include <pthread.h> pthread_t, to define a thread id pthread_create, to create a thread pthread_join, join with a terminated thread pthread_mutex_t, to create a mutex in pthread pthread_mutex_lock, to lock a mutex in pthread pthread_mutex_unlock, unlock a mutex in pthead While compiling your program, you should use - lpthread flag gcc -o main main.c -lpthread

Basic Pthread Programming pthread_create() Starts a new thread in the calling process. Syntax int pthread_create(pthread_t * thread, const pthread_attr * attr, void* (*start_routine)(void *), void* arg); Parameters thread: the thread handler of the newly created thread; attr: the attributes of the thread, in most cases set to NULL; start_routine: the pointer pointing to the function which will run in the thread; arg: the argument for the start_routine function NULL when there is no arguments.

Basic Pthread Programming pthread_create() The new thread starts execution by invoking start_routine(); arg is passed as the sole argument of start_routine(). Example pthread_t thread; int rc = pthread_create(&thread, NULL, start_routine, NULL);

Basic Pthread Programming pthread_join() Waiting for another thread to terminate Syntax int pthread_join( thread_t* th, void ** thread_ret); th: waiting for the thread with the thread handler th to terminate thread_ret: if the return value is not NULL, thread_ret will point to the place where the return value of thread th is stored Example pthread_join(thread, NULL);

Basic Pthread Programming pthread_detach() detach a thread Syntax int pthread_detach(pthread_t thread); The resources of the detached thread can be reclaimed when that thread terminates. This routine can be used to explicitly detach a thread even though it was created as joinable. Detached thread can never be joined.

Basic Pthread Programming pthread_exit() Termination of the calling thread Syntax void pthread_exit( void * ret_value) ret_value is the return value of the thread, setting to NULL will be OK for most cases Example pthread_exit(null);

Basic Pthread Programming Return value of pthread_exit() pthread_exit() will kill the thread and never return. Thus, Remember that the return value cannot be of local scope, otherwise when the thread terminates, the return value will not exist. This value can be get and examined by some other thread with function pthread_join()

Transfer Data Among Threads Using global variable. Do not forget mutex. Initialize the worker threads with arguments. pthread_create() Multiple arguments for start_routine Always using a structure to pass the arguments Example:

Recommended Materials Here are some links from which you can get more guidance on pthread programming POSIX Threads Programming POSIX thread (pthread) libraries Wikipedia Always take Manual for reference. man pthread_create