180 likes | 287 Vues
This guide explores the fundamental concepts of threads and processes in computing. A process encompasses a single address space with the capacity for multiple lines of execution through threads. Threads share global variables, stack space, and resources, enabling efficient execution and overlapping computing tasks, particularly useful in scenarios like web servers and word processors. We discuss how to create threads in C using `pthread`, the significance of thread types, potential pitfalls related to global variables, and solutions to avoid common threading issues. Gain insights into enhancing performance through parallelism using threads. ###
E N D
2.2 Threads • Process: address space + code execution • There is no law that states that a process cannot have more than one “line” of execution. • Threads: single address space + many threads of execution • Shared global variables, stack address space, open files, signals, child processes, etc.
Threads • Process – used to group resources together • Threads – entities scheduled for execution on CPU • Sometimes called lightweight processes • Multithreading – multiple threads in the same process
Thread functions • Create threads: #include <pthread.h> int pthread_create ( pthread_t* thread, pthread_attr_t* attr, void* (*start_routine)(void *), void* arg );
Thread termination void pthread_exit ( void* retval ); int pthread_cancel ( pthread_t thread );
Why threads? • Easier to create a thread than a process • Share resources • Compute and I/O within a single process on a single processor can be overlapped • Compute and compute within a single process on a multiprocessor can be overlapped
Example: word processor • Multiple threads: • User interaction • Document reformatting • Automatic saving/backups • Alternative is everything else stops when (2) or (3) occurs.
Example: web server • Dispatcher – handles incoming requests • Workers – performs request • Checks cache • Reads from disk if necessary • Adds to cache
Threads and alternatives • Threads retain the simple sequential, blocking model while allowing for parallelism. • The single threaded server retains the simple sequential, block model but performance suffers (no parallelism). • Finite state machine = each computation has a saved state and there exists some set of events that can occur to change the state • High performance through parallelism • Uses nonblocking calls and interrupts (not simple) • May be in user space or kernel.
Threads • Thread types: • Detached • Joinable • (also popup) • Implementations: • User space • Kernel • Hybrid
Pitfall: global variables #include <stdio.h> bool err = false; void* t1 ( void* p ) { … err = true; if (err) puts( “hello” ); //may never get here! … } void* t2 ( void* p ) { … err = false; … } int main ( int argc, char* argv[] ) { … if (err) puts( “something bad happened.” ); … }
Pitfall: global variables • Solution: don’t use ‘em. • Solution: create thread-wide globals (using your own libraries) • Solution: (other mechanisms that we will explore in the future)
Other pitfalls • Libraries may not be reentrant • Solution: rewrite the library • Solution: wrap each library call with a wrapper • Signals, alarms, and I/O may not be thread specific.
Other pthread functions • int thread_yield() • int pthread_join( pthread_t th, void** thread_return ) • void pthread_exit( void* retval ) • Many, many others