Lecture 9 -_pthreads-linux_threads


Published on

hi wassup

Published in: Economy & Finance
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Lecture 9 -_pthreads-linux_threads

  1. 1. Threads
  2. 2.
  3. 3. Figure 2-12. The first column lists some items shared by all threads in a process. The second one lists some items private to each thread. Thread/Process Items Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13- 6006639
  4. 4. Figure 2-16. (a) A user-level threads package. (b) A threads package managed by the kernel. User Threads, Kernel Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13- 6006639
  5. 5. Threads in Linux
  6. 6. Story of Unix (Vahalia)‏ <ul><li>Bell Labs, GE and MIT collaborating on Multics </li></ul><ul><ul><li>Canceled in 1969 </li></ul></ul><ul><li>Multics developer Thomson, and Ritchie created an environment to support a game Space Travel , on PDP-7 </li></ul><ul><li>Unix was born </li></ul>
  7. 7. Story of Unix <ul><li>Popular within Bell Labs: Unix programmers manual – 1971 </li></ul><ul><li>Paper in SOSP: 1974, The Unix Timesharing System </li></ul><ul><li>AT&T wasn’t so interested – licensed by UC Berkeley </li></ul><ul><li>Many ports </li></ul><ul><ul><li>Microsoft and SCO ported to Intel 8086 - XENIX </li></ul></ul>
  8. 8. Flavours of Unix <ul><li>Berkeley Software Distribution: Famous for sockets </li></ul><ul><li>System V: AT&T’s version (finally got interested)‏ </li></ul><ul><li>IBM – AIX </li></ul><ul><li>HP – UX </li></ul><ul><li>Sun – Solaris </li></ul><ul><ul><li>Famous for NFS </li></ul></ul>
  9. 9. POSIX <ul><li>Too many variants – needed standardization </li></ul><ul><li>1986 - IEEE committee formed to publish a proper standard </li></ul><ul><li>POSIX: Portable Operating Systems based on unIX </li></ul><ul><li>POSIX1003.1 standard – known as POSIX.1 published in 1990 </li></ul><ul><ul><li>Minimum system call interface to be supported </li></ul></ul>
  10. 10. Linux <ul><li>Tanenbaum released MINIX in 1987 </li></ul><ul><ul><li>Educational operating system </li></ul></ul><ul><li>Linus Torvalds, inspired by MINIX, wrote Linux (developed on MINIX)‏ </li></ul>
  11. 11. Threads in Linux <ul><li>Original: LinuxThreads (1996)‏ </li></ul><ul><li>Now: Native Posix Threads Library (2005)‏ </li></ul>
  12. 12. Threads in Linux <ul><li>Before any thread library was created: </li></ul><ul><ul><li>Clone() system call supported by Linux </li></ul></ul><ul><ul><li>Creates copy of process that shares address space of the caller </li></ul></ul>
  13. 13. LinuxThreads <ul><li>User-space thread library built using clone() system call </li></ul><ul><li>Basic assumption: context switches between related processes are fast enough to handle each user-level thread by one kernel thread (1:1 model)‏ </li></ul>
  14. 14. … Linux Threads <ul><li>Since “threads” were actually separate processes, needed the concept of manager thread. This is required for (from IBM article): </li></ul><ul><ul><li>The system must be able to react to fatal signals and kill the entire process. </li></ul></ul><ul><ul><li>The de-allocation of memory used as stacks must happen after a thread is finished. Therefore, the thread cannot do this itself. </li></ul></ul><ul><ul><li>Terminating threads must be waited on so that they don't turn into zombies. </li></ul></ul><ul><ul><li>The de-allocation of thread-local data requires iterating over all threads; this must be done by the manager thread. </li></ul></ul><ul><ul><li>If the main thread needs to call pthread_exit(), the process is not terminated. The main thread goes to sleep, and it is the job of the manager thread to wake up the main thread when all other threads have been killed. </li></ul></ul>
  15. 15. … Linux Threads <ul><li>Each thread had a different process ID </li></ul><ul><li>Scheduling Handled by Kernel Scheduler </li></ul>
  16. 16. … LinuxThreads: Limitations <ul><li>Requires manager thread: overhead </li></ul><ul><ul><li>Including context switching (lot of it, as everything is managed through manager thread)‏ </li></ul></ul><ul><ul><li>Caused scalability problems </li></ul></ul><ul><li>Each thread having different PID: POSIX incompatible </li></ul><ul><li>No concept of sending a signal to the main process (many signal-related complications)‏ </li></ul><ul><li>/proc directory is flooded </li></ul><ul><li>Number of threads limited by total number of processes the architecture can support </li></ul>
  17. 17. NPTL Goals (paper by NPTL authors)‏ <ul><li>The new threading library should be POSIX compliant. </li></ul><ul><li>The thread implementation should work well on systems with large number of processors. </li></ul><ul><li>Creating new threads for even a small piece of work should have a low startup cost. </li></ul><ul><li>The NPTL thread library should be binary compatible with LinuxThreads. </li></ul><ul><li>The new threading library should be able to take advantage of NUMA support. </li></ul>
  18. 18. NPTL Design Decisions <ul><li>1:1 vs M:N threading model: 1:1 prevailed </li></ul><ul><ul><li>No motivation to deal with complications due to the M:N threading model </li></ul></ul><ul><ul><li>Scheduler is fast enough O(1), can deal with large number of threads </li></ul></ul><ul><li>No manager thread </li></ul><ul><ul><li>All tasks taken care of by kernel itself </li></ul></ul>
  19. 19. … NPTL Design <ul><li>Different threads have different thread ID, same process ID </li></ul><ul><li>Exec system call keeps same process ID, all threads of previous process are terminated. </li></ul><ul><li>/proc structure (process list) has only the first thread </li></ul><ul><li>2 billion threads can be supported </li></ul>
  20. 20. Pthread calls <ul><li>int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg); </li></ul><ul><ul><li>pthread_t: thread ID </li></ul></ul><ul><ul><li>start_routing: function that this thread calls </li></ul></ul><ul><ul><li>arg; argument to this function </li></ul></ul><ul><li>int pthread_join(pthread_t thread, void *value_ptr); </li></ul><ul><ul><li>Waits for thread thread to finish, the functions return value is pointed to by value_ptr </li></ul></ul>
  21. 21. … pthread calls <ul><li>Function to be called must be declared as a function pointer. </li></ul>