1 / 29

Posix Threads

Posix Threads. CSCE 713 Advanced Computer Architecture. Topics Pthreads Readings. January 12, 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

durgan
Télécharger la présentation

Posix Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Posix Threads CSCE 713 Advanced Computer Architecture • Topics • Pthreads • Readings January 12, 2012

  2. 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 2nd ed. Richard Stevens and … (emailed) • http://www.kohala.com/start/ • New • Finish Slides from Lecture 1 • NP-Completeness and the Dwarves • PosixPthreads

  3. Books by Richard Stevens • UNIX Network Programming, Volume 2, Second Edition: Interprocess Communications, Prentice Hall, 1999. • UNIX Network Programming, Volume 1, Second Edition: Networking APIs: Sockets and XTI, Prentice Hall, 1998. • TCP/IP Illustrated, Volume 3: TCP for Transactions, HTTP, NNTP, and the UNIX Domain Protocols, Addison-Wesley, 1996. • TCP/IP Illustrated, Volume 2: The Implementation, Addison-Wesley, 1995. • TCP/IP Illustrated, Volume 1: The Protocols, Addison-Wesley, 1994. • Advanced Programming in the UNIX Environment, Addison-Wesley, 1992. • UNIX Network Programming, Prentice Hall, 1990.

  4. More Dwarves later

  5. What are the Dwarves? • Benchmark suites SPEC 2006 • Very important problems • Prototypical problems • …

  6. No Efficient Algorithms part 1 Computers and Intractability … NP-Completeness – Garey and Johnson

  7. No Efficient Algorithms part 2 Computers and Intractability … NP-Completeness – Garey and Johnson

  8. No Efficient Algorithms part 3 Computers and Intractability … NP-Completeness – Garey and Johnson

  9. NP-Completeness Review • P • NP • NP-P – intractable • P transforms to Q • P is NP-Complete if • P is in NP • For all other problems Q in NP, Q transforms to P Computers and Intractability … NP-Completeness – Garey and Johnson

  10. Proving NP-Completeness and Cooke’s Theorem • To prove Problem P is NP-Complete • Show P is in NP and • Show some known NP-Complete problem Q transforms to P • Cooke’s Theorem 1970 --- SAT (booleansatisfiability) is NP Complete. • http://en.wikipedia.org/wiki/Cook%E2%80%93Levin_theorem Computers and Intractability … NP-Completeness – Garey and Johnson

  11. Links: Threads, Unix Processes, • https://computing.llnl.gov/tutorials/pthreads/ • http://en.wikipedia.org/wiki/POSIX_Threads • http://download.oracle.com/javase/tutorial/essential/concurrency/procthread.html • http://www.cis.temple.edu/~ingargio/cis307/readings/system-commands.html • http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

  12. Unix System Related Commands • ps, kill, • top, nice, jobs, fg, bg • lscpu • /dev/proc https://computing.llnl.gov/tutorials/pthreads/

  13. What is a Thread? • . https://computing.llnl.gov/tutorials/pthreads/

  14. Threads in the Unix Environment • Exists 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 it • Duplicates only the essential resources it needs to be independently schedulable • May share the process resources with other threads that act equally independently (and dependently) • Dies 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. • 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. Threads Sharing of Data • 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. Sharing of Data between Processes

  17. What are Pthreads? Why Pthreads? • What? POSIX is an acronym for Portable Operating System Interface • Why? The primary motivation for using Pthreads is to realize potential program performance gains. https://computing.llnl.gov/tutorials/pthreads/

  18. 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. More Unix • Includes • Libraries

  20. Pthread Create • #include <pthread.h> • intpthread_create (pthread_t=tid , constpthread_attr_t*attr, • void * (*func) (void *), void *argv ); • Returns: 0 if OK, positive Exxxvalue on error APUE – Stevens et al Chapter 25

  21. pthread_join Function • #include <pthread.h> • intpthread_join(pthread_t, tidvoid **status); • Returns: 0 if OK, positive Exxxvalue on error APUE – Stevens et al Chapter 25

  22. thread_self Function • #include <pthread.h> • pthread_tpthread_self(void); • Returns: thread ID of calling thread APUE – Stevens et al Chapter 25

  23. pthread_detach Function • #include <pthread.h> • intpthread_detach(pthread_ttid); • Returns: 0 if OK, positive Exxxvalue on error APUE – Stevens et al Chapter 25

  24. pthread_exit Function • #include <pthread.h> • void pthread_exi t (void *status); • Does not return to caller

  25. threads/tcpserv01.c - APUE Code • 1 #include "unpthread.h" • 2 static void *doit(void *); /* each thread executes this function */ • 3 int • 4 main(intargc, char **argv) • intlistenfd, connfd; • pthread_ttid; • socklen_taddrlen, len; • structsockaddr *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 [ <host> ] <service or port>"); APUE – Stevens et al Chapter 25

  26. 16 cliaddrMalloc (addrlen) ; • 17 for (; ) { • 18 lenaddrlen; • 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. Embarrassingly Parallel to Inherently Sequential

More Related