Chapter 2 5 Threads Process concept n Process

























- Slides: 25
Chapter 2. 5 : Threads Process concept n Process scheduling n Interprocess communication n Deadlocks n Threads n 1
Threads These lecture notes have been adapted from n How to program with threads An introduction to multithreaded programming By Bil Lewis and Daniel J. Berg and n Tanenbaum slides 2
Processes & Threads Processes and threads are related concepts n A process is a kernel-level entity n n n Process structure can only be accessed through system calls A thread (or a lightweight process) is a user-level entity The thread structure is in user space n It is accessed directly with the thread library calls, which are just normal user-level functions (threads do not use system calls) n 3
Single vs. Multiple Threads of Execution Single Thread Start Multiple Threads Start Edit Document Print Document End 4
Thread Usage (1) A word processor with three threads 5
Thread Usage (2) A multithreaded Web server 6
Thread Usage (3) n Rough outline of code for previous slide (a) Dispatcher thread (b) Worker thread 7
The Classical Thread Model (1) (a) Three processes each with one thread (b) One process with three threads 8
The Thread Model (2) n n (Per process items) Items shared by all threads in a process (Per thread items) Items private to each thread 9
The Thread Model (3) Each thread has its own stack 10
Process and Thread Data Structures TCB 1 Code TCB 2 TCB 3 Data Stack User Space Kernel Space PCB 11
Characteristics of Threads n The TCB (thread control block) consist of n program counter n register set n stack space Thus the TCB is a reduced PCB n A traditional process is equal to a task with one thread n All threads in a process share the state of that process 12
Characteristics of Threads (Cont. ) n They reside in the exact same memory space (user memory), see the same code and data n When one thread alters a process variable (say, the working directory), all the others will see the change when they next access it n If one thread opens a file to read it, all the other threads can also read from it. 13
Characteristics of Threads (Cont. ) n Because no system calls are involved, threads are fast n There are no kernel structures affected by the existence of threads in a program, so no kernel resources are consumed -- threads are cheap n The kernel doesn't even know that threads exist 14
Thread Scheduling (1) Possible scheduling of user-level threads n n 50 -msec process quantum threads run 5 msec/CPU burst 15
Thread Scheduling (2) Possible scheduling of kernel-level threads n n 50 -msec process quantum threads run 5 msec/CPU burst 16
Threads of a Task Threads Program Counter Code segment Task Data segment 17
Implementing Threads in User Space A user-level threads package 18
Implementing Threads in the Kernel A threads package managed by the kernel 19
Hybrid Implementations Multiplexing user-level threads onto kernel- level threads 20
Some Benefits of Writing Multithreaded Programs: · Performance gains from multiprocessing hardware (parallelism) · Increased application throughput · Increased application responsiveness · Enhanced process-to-process communications 21
Parallellism n Different threads can run on different processors simultaneously with no special input from the user and no effort on the part of the programmer 22
Throughput n When a traditional, single-threaded program requests a service from the operating system, it must wait for that service to complete, often leaving the CPU idle n Multithreading provides progress even though one or more threads wait for an event as long as other threads are active 23
Responsiveness n Blocking one part of a process need not block the whole process. Single-threaded applications that do something lengthy when a button is pressed typically display a "please wait" cursor and freeze while the operation is in progress n If such applications were multithreaded, long operations could be done by independent threads, allowing the application to remain active and making the application more responsive to the user 24
Communications n An application that uses multiple processes to accomplish its tasks can be replaced by an application that uses multiple threads to accomplish those same tasks n Processes-to-process communication through traditional IPC (interprocess communications) facilities (e. g. , pipes or sockets) n The threaded application can use the inherently shared memory of the process 25