Operating Systems: Threa ds 1 Threads •Process : –program in execution and its context –resources assigned to it »a virtual machine with memory - segments and pages, and high level facilities »open files, semaphores, buffers etc. –scheduling and dispatching »execution interleaved with other processes; priorities »volatile context of PC, Stack pointers, registers etc. •Threads : –multiple threads can exist within a single process »creation, deletion, synchronisation –can execute concurrently »thread priorities »multiprocessor machines
24
Embed
Operating Systems: Threads 1 Threads Process : –program in execution and its context –resources assigned to it »a virtual machine with memory - segments.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Operating Systems: Threads
1
Threads
•Process :
–program in execution and its context
–resources assigned to it
»a virtual machine with memory - segments and pages, and high level facilities
»open files, semaphores, buffers etc.
–scheduling and dispatching
»execution interleaved with other processes; priorities
»volatile context of PC, Stack pointers, registers etc.
•Threads :
–multiple threads can exist within a single process
»creation, deletion, synchronisation
–can execute concurrently
»thread priorities
»multiprocessor machines
Operating Systems: Threads
2
•Unit of resource ownership remains the process
–all threads within a process share the same resources
»virtual memory - all code and data, open files, pipes, sockets etc.
–processes compete for resources from the kernel
–threads must cooperate in using their resources
•Unit of scheduling and dispatching becomes the thread
–each thread needs its own volatile context
–PC, stack and stack pointers, registers and status
Operating Systems: Threads
3
•Threads can be created much more quickly than processes
–no virtual machine to be created
–just a new volatile context, stack area and registration with the scheduler
–threads are typically created to run procedures within a program
•Thread libraries available for most Operating Systems
–Windows NT/2000, Solaris, Mach, OS/2
–standard threads package - POSIX threads
•Multi-threaded concurrent languages :
–ADA, Occam
»a thread per task
»intercommunication by means of rendezvous
»inter-thread message passing very efficient - via virtual memory
–Java
»extend Thread class in java.lang package
Operating Systems: Threads
4
•Benefits of threads :
–better programming paradigm for many applications
»a thread for each inherently concurrent activity
»simpler and more reliable programs
–performance gains
»example: less hold-up by blocking system calls
»ability to use multi-processor systems easily and conveniently
data can be partitioned to take advantage of multi-processing
–real-time programs:
»interrupts, signals and events occurring asynchronously
»create a new thread for each asynchronous event as in happens
–network servers:
»servers handle multiple concurrent requests from network machines
»create a thread to deal with each request separately
Operating Systems: Threads
5
–distributed object servers in a network - CORBA object brokers
»a thread per object
»databases and transaction systems
–dealing with multiple input sources and outputs
»a thread per stream
–user interaction:
»foreground keyboard, mouse and screen interaction concurrent with background computation e.g. a spreadsheet
»create separate threads for foreground and background
a menu selection thread
screen update and scrolling threads
command execution thread
–checkpoint threads
»a background thread, executed periodically, that saves data
»protects against crashes
Operating Systems: Threads
6
•Example: a CS4 graphics project : A Virtual Orrery
–each planet and moon represented by a thread
–work out their own motions in cooperation with other threads
•Example: a Network File Server
–for each request from a user:
»file directories accessed to get disc addresses
»page faults will cause a hiatus - delaying later requests
»server initiates disc transfers and waits for completion
»disc device driver will reschedule transfers depending on head position
»transfers may be completed out of order depending on disc rotation position
»server has to remember all requested actions still outstanding and deal with completions in any order
»effectively having to incorporate concurrency control into the server
–much easier to start a new thread for each request
»a thread held up for actions to complete does not hold up other threads
»each thread a simple sequential series of actions
Operating Systems: Threads
7
–service thread :
»initialisation - takes up idle time whilst creating and opening files
»printing, importing data and “flowing” text
–event-handing thread :
»menu item selection
»window operations
»synchronising with service thread is tricky user may continue to type, or use mouse,
whilst service thread activity still in progress
user activity restricted by disabling menu items until service thread activities complete
–screen-redraw thread :
»allows redrawing to be aborted
»dynamic scrolling as user drags scroll bar event-handling thread monitors scroll bar and
redraws margin rulers
screen-redraw thread constantly monitors rulers and tries to redraw screen and catch up
Example: Pagemaker DTP package
•three threads always active
Operating Systems: Threads
8
•Solaris Threads :
–kernel level threads
»kernel knows nothing about user-level threads
»only knows about kernel threads
»can be multiplexed onto any processor
»can be tied to run only on a specific processor
»can be pinned to a specific processor
»kernel threads for OS device drivers and other kernel functions
»all scheduling done on kernel level threads
Operating Systems: Threads
9
–lightweight processes (LWP)
»one kernel thread per LWP
»LWPs scheduled with associated kernel thread
a pre-emptive priority-based scheduler
»can have several LWPs per process
–user-level threads
»only user-level threads perform user’s work
»each has to be connected to an LWP to be scheduled
LWPs can be blocked
need as many LWPs as number of concurrent kernel operations
»can be multiplexed around available LWPs dynamically or tied to one
does not require kernel interaction to do connection
may have to wait for a free LWP to be connected to
»can synchronise amongst themselves
Operating Systems: Threads
10
•Solaris threads resource needs :
–kernel threads :
»small data structure and stack
»context switching between kernel threads relatively fast
–lightweight processes :
»need a process control block (PCB)
»may be quite slow
–user level threads :
»program counter, stack and pointer needed
»no kernel resources involved
»connecting user level threads to LWPs very fast
»may be lots of user level threads and fewer LWPs
kernel only sees LWPs
Operating Systems: Threads
11
POSIX Threads
•system of choice for portability
–similar to Solaris threads
–incompatible with Microsoft Win32 NT threads!
•textbook
–Pthreads Primer - A Guide to Multithreaded Programming, Lewis & Berg
d1c2_args, c1b2_args, d1d2_args;int i, j;void *status;
// arbitrary matrix values for testingfor (i=0; i<N; i++) {
for (j=0; j<N; j++) {p[i][j] = i*j; q[i][j] = i*j;
} }
// normal N*N multiplication first with one threadfill_args (&mmt_args, p, 0, 0, q, 0, 0, r, N);pthread_create (&mmt, NULL, matrix_mult, (void *)&mmt_args);
// print resultsprintf(“\nSingle thread result :\n”);for (i=0; i<N; i++) {
// arbitrary matrix values for testingfor (i=0; i<N; i++) {
for (j=0; j<N; j++) {p[i][j] = i*j; q[i][j] = i*j;
} }
sema_init (&args_sem);sema_init (&fin_sem);
// normal N*N multiplication first with one threadfill_args (&mmt_args, p, 0, 0, q, 0, 0, r, N, &args_sem, &fin_sem);pthread_create (&mmt, NULL, matrix_mult, (void *)&mmt_args);sema_P (&args_sem); // wait until arguments copiedsema_P (&fin_sem); // wait until thread finished