Вы находитесь на странице: 1из 26

Threads, SMP, and Microkernels

Chapter 4

Process
Resource ownership - process includes a virtual address space to hold the process image Scheduling/execution- follows an execution path that may be interleaved with other processes These two characteristics are treated independently by the operating system
2

Process
Dispatching is referred to as a thread or lightweight process ownership of Resource is referred to as a process or task

Multithreading
Operating system supports multiple threads of execution within a single process MS-DOS supports a single thread UNIX supports multiple user processes but only supports one thread per process Windows, Solaris, Linux, Mach, and OS/2 support multiple threads
4

Process
Have a virtual address space which holds the process image Protected access to processors, other processes, files, and I/O resources

Thread
An execution state (running, ready, etc.) Saved thread context when not running Has an execution stack Some per-thread static storage for local variables Access to the memory and resources of its process
all threads of a process share this
7

Benefits of Threads
Takes less time to create a new thread than a process Less time to terminate a thread than a process Less time to switch between two threads within the same process Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel
10

Uses of Threads in a SingleUser Multiprocessing System


Foreground to background work Asynchronous processing Speed of execution Modular program structure

11

Threads
Suspending a process involves suspending all threads of the process since all threads share the same address space Termination of a process, terminates all threads within the process

12

Thread States
States associated with a change in thread state
Spawn
Spawn another thread

Block Unblock Finish


Deallocate register context and stacks

13

Multithreading

14

User-Level Threads
All thread management is done by the application The kernel is not aware of the existence of threads

15

User-Level Threads

16

17

Advantages of User-Level Threads


Thread switching does not require kernel mode privileges Scheduling can be application specific ULT can run on any operating system

18

Disadvantages of ULT
System calls are blocking, as thread executes the system call it gets blocked Pure multithreading application cant take advantage of multiprocessing as kernel assigns one process to only one processor

#include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 void *PrintHello(void *threadid) { long tid; tid = (long)threadid; printf("Hello World! It's me, thread #%ld!\n", tid); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc; long t; for(t=0; t<NUM_THREADS; t++){ printf("In main: creating thread %ld\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } pthread_exit(NULL); }

OUT PUT
In main: creating In main: creating Hello World! It's In main: creating Hello World! It's Hello World! It's In main: creating In main: creating Hello World! It's Hello World! It's thread 0 thread 1 me, thread thread 2 me, thread me, thread thread 3 thread 4 me, thread me, thread

#0! #1! #2!

#3! #4!

Kernel-Level Threads
Windows is an example of this approach Kernel maintains context information for the process and the threads Scheduling is done on a thread basis

22

Kernel-Level Threads

23

Combined Approaches
Example is Solaris Thread creation done in the user space Bulk of scheduling and synchronization of threads within application

24

Combined Approaches

25

Relationship Between Threads and Processes

26

Вам также может понравиться