Posix Threads Topics PthreadsReadings January 12, 2012 CSCE 713 Advanced Computer Architecture.

Slides:



Advertisements
Similar presentations
Threads. Readings r Silberschatz et al : Chapter 4.
Advertisements

Threads Programming Thread creation Synchronization.
TELE 402 Lecture 11: Advanced UDP… 1 by Dr Z. Huang Overview Last Lecture –Nonblocking I/O and ioctl operations –Source: Chapter 16 & 17 of Stevens’ book.
Professor: Shu-Ching Chen TA: Hsin-Yu Ha.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int c;
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
UNIX Socket Programming CS 6378
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
POSIX Threads Programming Operating Systems. Processes and Threads In shared memory multiprocessor architectures, such as SMPs, threads can be used to.
Includes slides from course CS194 at UC Berkeley, by prof. Katherine Yelick Shared Memory Programming Pthreads: an overview Ing. Andrea Marongiu
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
Pthreads: A shared memory programming model
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
S -1 Posix Threads. S -2 Thread Concepts Threads are "lightweight processes" –10 to 100 times faster than fork() Threads share: –process instructions,
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
Pthreads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Outline n Overview n Multithreading.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
Threads A thread is an alternative model of program execution
POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Realizing Concurrency using the thread model
Realizing Concurrency using the thread model
Threads Threads.
Thread Programming.
Chapter 4: Threads.
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Realizing Concurrency using the thread model
Thread Programming.
Realizing Concurrency using the thread model
CSCE 513 Computer Architecture
Operating System Concepts
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Presentation transcript:

Posix Threads Topics PthreadsReadings January 12, 2012 CSCE 713 Advanced Computer Architecture

– 2 – CSCE 713 Spring 2012 Overview Last Time Power wall, ILP wall,  to multicore Seven Dwarfs Amdahl’s Law, Gustaphson’s law Landscape of Parallel Computing Research Berkeley View EECS Readings for today Chapter 25 Threads from Advanced Unix Programming 2 nd ed. Richard Stevens and … ( ed) New Finish Slides from Lecture 1 NP-Completeness and the Dwarves Posix Pthreads

– 3 – CSCE 713 Spring 2012 Books by Richard Stevens UNIX Network Programming, Volume 2, Second Edition: Interprocess CommunicationsUNIX Network Programming, Volume 2, Second Edition: Interprocess Communications, Prentice Hall, UNIX Network Programming, Volume 2, Second Edition: Interprocess Communications UNIX Network Programming, Volume 1, Second Edition: Networking APIs: Sockets and XTIUNIX Network Programming, Volume 1, Second Edition: Networking APIs: Sockets and XTI, Prentice Hall, UNIX Network Programming, Volume 1, Second Edition: Networking APIs: Sockets and XTI TCP/IP Illustrated, Volume 3: TCP for Transactions, HTTP, NNTP, and the UNIX Domain ProtocolsTCP/IP Illustrated, Volume 3: TCP for Transactions, HTTP, NNTP, and the UNIX Domain Protocols, Addison-Wesley, TCP/IP Illustrated, Volume 3: TCP for Transactions, HTTP, NNTP, and the UNIX Domain Protocols TCP/IP Illustrated, Volume 2: The ImplementationTCP/IP Illustrated, Volume 2: The Implementation, Addison-Wesley, TCP/IP Illustrated, Volume 2: The Implementation TCP/IP Illustrated, Volume 1: The ProtocolsTCP/IP Illustrated, Volume 1: The Protocols, Addison-Wesley, TCP/IP Illustrated, Volume 1: The Protocols Advanced Programming in the UNIX EnvironmentAdvanced Programming in the UNIX Environment, Addison-Wesley, Advanced Programming in the UNIX Environment UNIX Network ProgrammingUNIX Network Programming, Prentice Hall, UNIX Network Programming

– 4 – CSCE 713 Spring 2012 More Dwarves later

– 5 – CSCE 713 Spring 2012 What are the Dwarves? Benchmark suites SPEC 2006Benchmark suites SPEC 2006 Very important problemsVery important problems Prototypical problemsPrototypical problems …

– 6 – CSCE 713 Spring 2012 No Efficient Algorithms part 1 Computers and Intractability … NP-Completeness – Garey and Johnson

– 7 – CSCE 713 Spring 2012 No Efficient Algorithms part 2 Computers and Intractability … NP-Completeness – Garey and Johnson

– 8 – CSCE 713 Spring 2012 No Efficient Algorithms part 3 Computers and Intractability … NP-Completeness – Garey and Johnson

– 9 – CSCE 713 Spring 2012 NP-Completeness Review PNP NP-P – intractable P transforms to Q P is NP-Complete if 1.P is in NP 2.For all other problems Q in NP, Q transforms to P Computers and Intractability … NP-Completeness – Garey and Johnson

– 10 – CSCE 713 Spring 2012 Proving NP-Completeness and Cooke’s Theorem To prove Problem P is NP-Complete 1.Show P is in NP and 2.Show some known NP-Complete problem Q transforms to P Cooke’s Theorem SAT (boolean satisfiability) is NP Complete. Computers and Intractability … NP-Completeness – Garey and Johnson

– 11 – CSCE 713 Spring 2012 Links: Threads, Unix Processes, cy/procthread.html cy/procthread.htmlhttp://download.oracle.com/javase/tutorial/essential/concurren cy/procthread.html 4. commands.html commands.htmlhttp:// commands.html 5. s.html s.htmlhttp:// s.html

– 12 – CSCE 713 Spring 2012 Unix System Related Commands ps, kill, top, nice, jobs, fg, bg lscpu/dev/proc

– 13 – CSCE 713 Spring 2012 What is a Thread?.

– 14 – CSCE 713 Spring 2012 Threads in the Unix Environment Exists within a process and uses the process resourcesExists 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 itHas 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 schedulableDuplicates only the essential resources it needs to be independently schedulable May share the process resources with other threads that act equally independently (and dependently)May share the process resources with other threads that act equally independently (and dependently) Dies if the parent process dies - or something similarDies 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.Is "lightweight" because most of the overhead has already been accomplished through the creation of its process. Because threads within the same process share resources: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.

– 15 – CSCE 713 Spring 2012 Threads Sharing of Data Because threads within the same process share resources: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.

– 16 – CSCE 713 Spring 2012 Sharing of Data between Processes

– 17 – CSCE 713 Spring 2012 What are Pthreads? Why Pthreads? What? POSIX is an acronym for Portable Operating System InterfaceWhat? POSIX is an acronym for Portable Operating System Interface Why? The primary motivation for using Pthreads is to realize potential program performance gains.Why? The primary motivation for using Pthreads is to realize potential program performance gains.

– 18 – CSCE 713 Spring 2012 Finding Information of Unix Script started on Thu 12 Jan :12:55 AM EST man pthread_create No manual entry for pthread_create man -k pthread pthread_attr_getaffinity_np (3) - set/get CPU affinity attribute in thread a... man -s 7 pthreads

– 19 – CSCE 713 Spring 2012 More Unix IncludesLibraries

– 20 – CSCE 713 Spring 2012 Pthread Create #include #include int pthread_create (pthread_t =tid, const pthread_attr_t *attr, void * (*func) (void *), void *argv ); Returns: 0 if OK, positive Exxx value on error APUE – Stevens et al Chapter 25

– 21 – CSCE 713 Spring 2012 pthread_join Function #include #include int pthread_join(pthread_t, tid void **status); Returns: 0 if OK, positive Exxx value on error APUE – Stevens et al Chapter 25

– 22 – CSCE 713 Spring 2012 thread_self Function #include #include pthread_t pthread_self(void); Returns: thread ID of calling thread APUE – Stevens et al Chapter 25

– 23 – CSCE 713 Spring 2012 pthread_detach Function #include #include int pthread_detach(pthread_t tid); Returns: 0 if OK, positive Exxx value on error APUE – Stevens et al Chapter 25

– 24 – CSCE 713 Spring 2012 pthread_exit Function #include #include void pthread_exi t (void *status); Does not return to caller

– 25 – CSCE 713 Spring 2012 threads/tcpserv01.c - APUE Code 1 #include "unpthread.h" 2 static void *doit(void *); /* each thread executes this function */ 3 int 4 main(int argc, char **argv) int listenfd, connfd; pthread_t tid; socklen_t addrlen, len; struct sockaddr *cliaddr; 10 if (argc == 2) 11 listenfd = Tcp_Iisten(NULL, argv[1], &addrlen); 12 else if (argc == 3) 13 listenfd = Tcp_Iisten(argv[1], argv[2], &addrlen); 14 else 15 err_quit("usage: tcpserv01 [ ] "); APUE – Stevens et al Chapter 25

– 26 – CSCE 713 Spring cliaddr Malloc (addrlen) ; 17 for (; ) { 18 len addrlen; 19 connfd = Accept (listenfd, cliaddr, &len); 20 Pthread_create(&tid, NULL, &doit, (void *) connfd); static void * 24 doit(void *arg) { Pthread_detach(pthread_self()) ; str_echo( (int) arg); /* same function as before */ Close((int) arg); /* we are done with connected socket */ return (NULL);

– 27 – CSCE 713 Spring 2012 Embarrassingly Parallel to Inherently Sequential

– 28 – CSCE 713 Spring 2012

– 29 – CSCE 713 Spring 2012