Presentation is loading. Please wait.

Presentation is loading. Please wait.

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:

Similar presentations


Presentation on theme: "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:"— 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

4

5

6 Thread functions  Create threads: #include #include int pthread_create ( pthread_t* thread, pthread_attr_t* attr, void* (*start_routine)(void *), void* arg );

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

8 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

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

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

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

12 Threads  Thread types:  Detached  Joinable  (also popup)  Implementations:  User space  Kernel  Hybrid

13 #include #include 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

14 Pitfalls: global variables

15 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)

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

17 Other pthread functions  int thread_yield()  int pthread_join( pthread_t th, void** thread_return )  void pthread_exit( void* retval )  Many, many others

18 Multithreaded example


Download ppt "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:"

Similar presentations


Ads by Google