Download presentation
Presentation is loading. Please wait.
Published byCrystal Dennis Modified over 9 years ago
1
Posix Threads Topics PthreadsReadings January 12, 2012 CSCE 713 Advanced Computer Architecture
2
– 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-2006-183 Readings for today Chapter 25 Threads from Advanced Unix Programming 2 nd ed. Richard Stevens and … (emailed) http://www.kohala.com/start/ New Finish Slides from Lecture 1 NP-Completeness and the Dwarves Posix Pthreads
3
– 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, 1999. 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, 1998. 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, 1996. 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, 1995. TCP/IP Illustrated, Volume 2: The Implementation TCP/IP Illustrated, Volume 1: The ProtocolsTCP/IP Illustrated, Volume 1: The Protocols, Addison-Wesley, 1994. TCP/IP Illustrated, Volume 1: The Protocols Advanced Programming in the UNIX EnvironmentAdvanced Programming in the UNIX Environment, Addison-Wesley, 1992. Advanced Programming in the UNIX Environment UNIX Network ProgrammingUNIX Network Programming, Prentice Hall, 1990. UNIX Network Programming
4
– 4 – CSCE 713 Spring 2012 More Dwarves later
5
– 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
– 6 – CSCE 713 Spring 2012 No Efficient Algorithms part 1 Computers and Intractability … NP-Completeness – Garey and Johnson
7
– 7 – CSCE 713 Spring 2012 No Efficient Algorithms part 2 Computers and Intractability … NP-Completeness – Garey and Johnson
8
– 8 – CSCE 713 Spring 2012 No Efficient Algorithms part 3 Computers and Intractability … NP-Completeness – Garey and Johnson
9
– 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
– 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 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
11
– 11 – CSCE 713 Spring 2012 Links: Threads, Unix Processes, 1.https://computing.llnl.gov/tutorials/pthreads/ https://computing.llnl.gov/tutorials/pthreads/ 2.http://en.wikipedia.org/wiki/POSIX_Threads http://en.wikipedia.org/wiki/POSIX_Threads 3.http://download.oracle.com/javase/tutorial/essential/concurren cy/procthread.html http://download.oracle.com/javase/tutorial/essential/concurren cy/procthread.htmlhttp://download.oracle.com/javase/tutorial/essential/concurren cy/procthread.html 4.http://www.cis.temple.edu/~ingargio/cis307/readings/system- commands.html http://www.cis.temple.edu/~ingargio/cis307/readings/system- commands.htmlhttp://www.cis.temple.edu/~ingargio/cis307/readings/system- commands.html 5.http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThread s.html http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThread s.htmlhttp://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThread s.html
12
– 12 – CSCE 713 Spring 2012 Unix System Related Commands ps, kill, top, nice, jobs, fg, bg lscpu/dev/proc https://computing.llnl.gov/tutorials/pthreads/
13
– 13 – CSCE 713 Spring 2012 What is a Thread?. https://computing.llnl.gov/tutorials/pthreads/
14
– 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. https://computing.llnl.gov/tutorials/pthreads/
15
– 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. https://computing.llnl.gov/tutorials/pthreads/
16
– 16 – CSCE 713 Spring 2012 Sharing of Data between Processes
17
– 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. https://computing.llnl.gov/tutorials/pthreads/
18
– 18 – CSCE 713 Spring 2012 Finding Information of Unix Script started on Thu 12 Jan 2012 09:12:55 AM EST matthews@saluda$ man pthread_create No manual entry for pthread_create matthews@saluda$ man -k pthread pthread_attr_getaffinity_np (3) - set/get CPU affinity attribute in thread a... matthews@saluda$ man -s 7 pthreads
19
– 19 – CSCE 713 Spring 2012 More Unix IncludesLibraries
20
– 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
– 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
– 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
– 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
– 24 – CSCE 713 Spring 2012 pthread_exit Function #include #include void pthread_exi t (void *status); Does not return to caller
25
– 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
– 26 – CSCE 713 Spring 2012 16 cliaddr Malloc (addrlen) ; 17 for (; ) { 18 len addrlen; 19 connfd = Accept (listenfd, cliaddr, &len); 20 Pthread_create(&tid, NULL, &doit, (void *) connfd); 21 23 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
– 27 – CSCE 713 Spring 2012 Embarrassingly Parallel to Inherently Sequential
28
– 28 – CSCE 713 Spring 2012
29
– 29 – CSCE 713 Spring 2012
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.