A thread is a separate flow of execution in a program. A single program can have multiple threads running within the same address space. ________________________________________________________________________ A thread has: - a register set (including program counter) - a call stack - errno - thread ID number (TID) - exists in a shared address space with other threads in the same process. - NOTE: NO parent TID Multithreaded programs can perform independent tasks using data shared within a process: - client/server architectures - processing ongoing data streams (news, financial, etc) - giving status on demand - modifying program behavior by human operators Benefits: - Responsiveness -- multithreaded apps can continue execution even if some threads are blocked. - Shared resources -- multiple parallel activities, all cheaply sharing data. - Economical -- Threads are cheaper to create than processes (10 to 100 times faster) and switching between threads is cheaper than switching between processes. - Utilization of (hardware) multiprocessors -- running different threads on different processors => better performance ________________________________________________________________________ Draw an interleaved picture for 3 threads on a uniprocesor and a multiprocessor. ________________________________________________________________________ Thread scheduling can be handled by the process or by the kernel. These are called kernel-level and user-level threads. User-level threads run in user code. The mechanics of thread creation, destruction, scheduling (changing from thread to thread), etc run as (library) code in the user program without kernel support. Pros: - Faster than kernel threads for CPU-intensive threads. Cons: - lernel is unaware of multiple user threads. If one user thread gets blocked, all user thread stops. - Can't schedule user-level threads on different processors. Kernel-level threads run the mechanics in the kernel, not the user program. All thread operations, including creation, destruction, scheduling involve system calls to the kernel. Pros: - One blocked kernel-level thread does not block other kernel-level threads. - Faster for I/O-intensive threads. - Can schedule kernel-level threads on different processors. Cons: - Slower to create and manage than user-level threads (but not a lot slower). ________________________________________________________________________ Basic threading operations are: - create - join - detach - yield - thread exit POSIX functions return 0 on success, some sort of errno on failure. Link with -lpthread to get POSIX library (see Makefile). UNIX handles kernel-level and user-level, but Linux handles only one. #include #include int pthread_attr_init( pthread_attr_t * attr ); // initialize default values are: PTHREAD_CREATE_JOINABLE, PTHREAD_SCOPE_PROCESS and PTHREAD_INHERIT_SCHED int pthread_attr_destroy( pthread_attr_t * attr ); int pthread_attr_setscope( pthread_attr_t * attr, int contentionscope ); contentionscope is PTHREAD_SCOPE_SYSTEM kernel-level thread PTHREAD_SCOPE_PROCESS user-level thread int pthread_attr_getscope( const pthread_attr_t * attr, int * contentionscope ); int pthread_create( pthread_t * thread, const pthread_attr_t * attr, void *(*start_routine)(void*), void * arg ); Start a new thread, invoking (*start_routine) function that takes arg. Exactly which thread is running immediately after this call is UNDEFINED. Any thread can create any other thread. How are more complicated parameters passed to the thread? int pthread_join( pthread_t thread, void ** status ); Suspend calling thread until parameter thread exits. Note: no parent-child relationship assumed. Like wait(2) (?) int pthread_detach( pthread_t thread ); Make argument thread non-joinable. pthread_t pthread_self(); Get a thread's own TID. int sched_yield(); Let some other thread run, if there is one. Always returns zero. void pthread_exit( void * value_ptr ); Exit this thread, but no others. How is this different from exit(2)? QUESTION: Which thread operation is missing here? HINT: it's one of the ten commandments.