posix threads topics pthreadsreadings january 12, 2012 csce 713 advanced computer architecture

29
Posix Threads Topics Topics Pthreads Readings Readings January 12, 2012 CSCE 713 Advanced Computer Architecture

Upload: crystal-dennis

Post on 18-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

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

Posix Threads Posix Threads

Topics Topics Pthreads

ReadingsReadings

January 12, 2012

CSCE 713 Advanced Computer Architecture

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

– 2 –CSCE 713 Spring 2012

OverviewOverviewLast TimeLast Time

Power wall, ILP wall, to multicore Seven Dwarfs Amdahl’s Law, Gustaphson’s law Landscape of Parallel Computing Research Berkeley View

EECS-2006-183

Readings for todayReadings for today Chapter 25 Threads from Advanced Unix Programming 2nd

ed. Richard Stevens and … (emailed) http://www.kohala.com/start/

NewNew Finish Slides from Lecture 1 NP-Completeness and the Dwarves Posix Pthreads

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

– 3 –CSCE 713 Spring 2012

Books by Richard StevensBooks by Richard StevensUNIX Network Programming, Volume 2, Second Edition: Interprocess Communications

, Prentice Hall, 1999. , Prentice Hall, 1999.

UNIX Network Programming, Volume 1, Second Edition: Networking APIs: Sockets and XTI

, Prentice Hall, 1998. , Prentice Hall, 1998.

TCP/IP Illustrated, Volume 3: TCP for Transactions, HTTP, NNTP, and the UNIX Domain Protocols

, Addison-Wesley, 1996., Addison-Wesley, 1996.

TCP/IP Illustrated, Volume 2: The Implementation, Addison-Wesley, 1995., Addison-Wesley, 1995.

TCP/IP Illustrated, Volume 1: The Protocols, Addison-Wesley, 1994., Addison-Wesley, 1994.

Advanced Programming in the UNIX EnvironmentAdvanced Programming in the UNIX Environment, Addison-Wesley, 1992., Addison-Wesley, 1992.

UNIX Network ProgrammingUNIX Network Programming, Prentice Hall, 1990., Prentice Hall, 1990.

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

– 4 –CSCE 713 Spring 2012

More Dwarves laterMore Dwarves later

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

– 5 –CSCE 713 Spring 2012

What are the Dwarves?What are the Dwarves?

• Benchmark suites SPEC 2006Benchmark suites SPEC 2006

• Very important problemsVery important problems

• Prototypical problemsPrototypical problems

• ……

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

– 6 –CSCE 713 Spring 2012

No Efficient Algorithms part 1No Efficient Algorithms part 1

Computers and Intractability … NP-Completeness – Garey and Johnson

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

– 7 –CSCE 713 Spring 2012

No Efficient Algorithms part 2No Efficient Algorithms part 2

Computers and Intractability … NP-Completeness – Garey and Johnson

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

– 8 –CSCE 713 Spring 2012

No Efficient Algorithms part 3No Efficient Algorithms part 3

Computers and Intractability … NP-Completeness – Garey and Johnson

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

– 9 –CSCE 713 Spring 2012

NP-Completeness ReviewNP-Completeness Review

PP

NPNP

NP-P – intractableNP-P – intractable

P transforms to QP transforms to Q

P is NP-Complete ifP is NP-Complete if

1.1. P is in NPP is in NP

2.2. For all other problems Q in NP, Q transforms to PFor all other problems Q in NP, Q transforms to P

Computers and Intractability … NP-Completeness – Garey and Johnson

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

– 10 –CSCE 713 Spring 2012

Proving NP-Completeness and Cooke’s TheoremProving NP-Completeness and Cooke’s TheoremTo prove Problem P is NP-CompleteTo prove Problem P is NP-Complete

1.1. Show P is in NP andShow P is in NP and

2.2. Show some known NP-Complete problem Q transforms to PShow some known NP-Complete problem Q transforms to P

Cooke’s Theorem 1970 --- SAT (boolean satisfiability) is NP Complete.Cooke’s Theorem 1970 --- SAT (boolean satisfiability) is NP Complete.• http://en.wikipedia.org/wiki/Cook%E2%80%93Levin_theorem

Computers and Intractability … NP-Completeness – Garey and Johnson

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

– 11 –CSCE 713 Spring 2012

Links: Threads, Unix Processes, Links: Threads, Unix Processes,

1.1. https://computing.llnl.gov/tutorials/pthreads/https://computing.llnl.gov/tutorials/pthreads/

2.2. http://en.wikipedia.org/wiki/POSIX_Threadshttp://en.wikipedia.org/wiki/POSIX_Threads

3.3. http://download.oracle.com/javase/tutorial/essential/http://download.oracle.com/javase/tutorial/essential/concurrency/procthread.htmlconcurrency/procthread.html

4.4. http://www.cis.temple.edu/~ingargio/cis307/readings/system-http://www.cis.temple.edu/~ingargio/cis307/readings/system-commands.htmlcommands.html

5.5. http://www.yolinux.com/TUTORIALS/http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html LinuxTutorialPosixThreads.html

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

– 12 –CSCE 713 Spring 2012

Unix System Related CommandsUnix System Related Commands

ps, kill, ps, kill,

top, nice, jobs, fg, bgtop, nice, jobs, fg, bg

lscpulscpu

/dev/proc/dev/proc

https://computing.llnl.gov/tutorials/pthreads/

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

– 13 –CSCE 713 Spring 2012

What is a Thread?What is a Thread?

..

https://computing.llnl.gov/tutorials/pthreads/

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

– 14 –CSCE 713 Spring 2012

Threads in the Unix EnvironmentThreads in the Unix Environment• Exists within a process and uses the process resources Exists within a process and uses the process resources

• Has its own independent flow of control as long as its Has its own independent flow of control as long as its parent process exists and the OS supports it parent process exists and the OS supports it

• Duplicates only the essential resources it needs to be Duplicates only the essential resources it needs to be independently schedulable independently schedulable

• May share the process resources with other threads that May share the process resources with other threads that act equally independently (and dependently) act equally independently (and dependently)

• Dies if the parent process dies - or something similar Dies if the parent process dies - or something similar

• Is "lightweight" because most of the overhead has already Is "lightweight" because most of the overhead has already been accomplished through the creation of its process. 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. https://computing.llnl.gov/tutorials/pthreads/

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

– 15 –CSCE 713 Spring 2012

Threads Sharing of DataThreads Sharing of Data• Because threads within the same process Because threads within the same process

share resources: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.

https://computing.llnl.gov/tutorials/pthreads/

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

– 16 –CSCE 713 Spring 2012

Sharing of Data between ProcessesSharing of Data between Processes

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

– 17 –CSCE 713 Spring 2012

What are Pthreads? Why Pthreads?What are Pthreads? Why Pthreads?

• What? POSIX is an acronym for Portable Operating What? POSIX is an acronym for Portable Operating System InterfaceSystem Interface

• Why? The primary motivation for using Pthreads is Why? The primary motivation for using Pthreads is to realize potential program performance gains. to realize potential program performance gains.

https://computing.llnl.gov/tutorials/pthreads/

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

– 18 –CSCE 713 Spring 2012

Finding Information of Unix Finding Information of Unix

Script started on Thu 12 Jan 2012 09:12:55 AM ESTScript started on Thu 12 Jan 2012 09:12:55 AM EST

matthews@saluda$ man pthread_creatematthews@saluda$ man pthread_create

No manual entry for pthread_createNo manual entry for pthread_create

matthews@saluda$ man -k pthreadmatthews@saluda$ man -k pthread

pthread_attr_getaffinity_np (3) - set/get CPU affinity pthread_attr_getaffinity_np (3) - set/get CPU affinity attribute in thread aattribute in thread a

......

matthews@saluda$ man -s 7 pthreadsmatthews@saluda$ man -s 7 pthreads

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

– 19 –CSCE 713 Spring 2012

More UnixMore Unix

IncludesIncludes

LibrariesLibraries

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

– 20 –CSCE 713 Spring 2012

Pthread CreatePthread Create

#include <pthread.h>#include <pthread.h>

int pthread_create (pthread_t int pthread_create (pthread_t =tid , =tid , const pthread_attr_t const pthread_attr_t *attr,*attr,

void * void * (*func) (*func) (void *), void (void *), void *argv );*argv );

Returns: 0 if OK, positive Returns: 0 if OK, positive Exxx Exxx value on errorvalue on error

APUE – Stevens et al Chapter 25

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

– 21 –CSCE 713 Spring 2012

pthread_join Functionpthread_join Function

#include <pthread.h>#include <pthread.h>

int pthread_join(pthread_t, tid void int pthread_join(pthread_t, tid void **status);**status);

Returns: 0 if OK, positive Returns: 0 if OK, positive Exxx Exxx value on errorvalue on error

APUE – Stevens et al Chapter 25

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

– 22 –CSCE 713 Spring 2012

thread_self Functionthread_self Function

#include <pthread.h>#include <pthread.h>

pthread_t pthread_self(void);pthread_t pthread_self(void);

Returns: thread ID of calling threadReturns: thread ID of calling thread

APUE – Stevens et al Chapter 25

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

– 23 –CSCE 713 Spring 2012

pthread_detach Functionpthread_detach Function

#include <pthread.h>#include <pthread.h>

int pthread_detach(pthread_t int pthread_detach(pthread_t tid);tid);

Returns: 0 if OK, positive Returns: 0 if OK, positive Exxx Exxx value on errorvalue on error

APUE – Stevens et al Chapter 25

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

– 24 –CSCE 713 Spring 2012

pthread_exit Functionpthread_exit Function

#include <pthread.h>#include <pthread.h>

void pthread_exi t (void void pthread_exi t (void *status);*status);

Does not return to callerDoes not return to caller

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

– 25 –CSCE 713 Spring 2012

threads/tcpserv01.c - APUE Codethreads/tcpserv01.c - APUE Code

1 1 #include "unpthread.h"#include "unpthread.h"

2 static void *doit(void *); /* each thread executes this function */2 static void *doit(void *); /* each thread executes this function */

3 int3 int

4 main(int argc, char **argv)4 main(int argc, char **argv)

int listenfd, connfd;int listenfd, connfd;

pthread_t tid;pthread_t tid;

socklen_t addrlen, len;socklen_t addrlen, len;

struct sockaddr *cliaddr;struct sockaddr *cliaddr;

10 10 if (argc == 2)if (argc == 2)

11 11 listenfd = Tcp_Iisten(NULL, argv[1], &addrlen);listenfd = Tcp_Iisten(NULL, argv[1], &addrlen);

12 else if (argc == 3)12 else if (argc == 3)

13 13 listenfd = Tcp_Iisten(argv[1], argv[2], &addrlen);listenfd = Tcp_Iisten(argv[1], argv[2], &addrlen);

14 else14 else

15 err_quit("usage: tcpserv01 [ <host> ] <service or port>");15 err_quit("usage: tcpserv01 [ <host> ] <service or port>");

APUE – Stevens et al Chapter 25

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

– 26 –CSCE 713 Spring 2012

16 16 cliaddr Malloc (addrlen) ;cliaddr Malloc (addrlen) ;

17 17 for (; ) {for (; ) {

18 18 len addrlen;len addrlen;

19 19 connfd = Accept (listenfd, cliaddr, &len);connfd = Accept (listenfd, cliaddr, &len);

20 20 Pthread_create(&tid, NULL, &doit, (void *) connfd);Pthread_create(&tid, NULL, &doit, (void *) connfd);

2121

23 static void *23 static void *

24 doit(void *arg) {24 doit(void *arg) {

Pthread_detach(pthread_self()) ;Pthread_detach(pthread_self()) ;

str_echo( (int) arg); /* same function as before */str_echo( (int) arg); /* same function as before */

Close((int) arg); /* we are done with connected socket */Close((int) arg); /* we are done with connected socket */

return (NULL); return (NULL);

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

– 27 –CSCE 713 Spring 2012

Embarrassingly Parallel to Inherently SequentialEmbarrassingly Parallel to Inherently Sequential

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

– 28 –CSCE 713 Spring 2012

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

– 29 –CSCE 713 Spring 2012