1 / 18

2.2 Threads

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.

makya
Download Presentation

2.2 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. 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.

  2. 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

  3. Thread functions • Create threads: #include <pthread.h> int pthread_create ( pthread_t* thread, pthread_attr_t* attr, void* (*start_routine)(void *), void* arg );

  4. Thread termination void pthread_exit ( void* retval ); int pthread_cancel ( pthread_t thread );

  5. 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

  6. Example: word processor • Multiple threads: • User interaction • Document reformatting • Automatic saving/backups • Alternative is everything else stops when (2) or (3) occurs.

  7. Example: web server • Dispatcher – handles incoming requests • Workers – performs request • Checks cache • Reads from disk if necessary • Adds to cache

  8. 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.

  9. Threads • Thread types: • Detached • Joinable • (also popup) • Implementations: • User space • Kernel • Hybrid

  10. 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.” ); … }

  11. Pitfalls: global variables

  12. 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)

  13. 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.

  14. Other pthread functions • int thread_yield() • int pthread_join( pthread_t th, void** thread_return ) • void pthread_exit( void* retval ) • Many, many others

  15. Multithreaded example

More Related