Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.

Similar presentations


Presentation on theme: "Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating."— Presentation transcript:

1 Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating Systems Concepts, by Silberschatz, Galvin, and Gagne. They also include material from the slides that accompany that book.

2 What is a thread? Our textbook: “a basic unit of CPU utilization”
An independent unit of execution within a process A path of execution through a program A lightweight process

3 Single and multithreaded processes

4 Threads Each thread has: --A unique ID --Its own program counter
--Its own register set --Its own stack A thread shares with other threads in the same process: --The address space --Other resources, such as files

5 Concurrency vs. parallelism
Concurrency supports more than one task making progress Parallelism implies a system can perform more than one task simultaneously

6 Benefits of threads Responsiveness – may allow continued execution if part of process is blocked, especially important for user interfaces Resource Sharing – threads share resources of process, easier than shared memory or message passing Economy – cheaper than process creation, thread switching lower overhead than context switching Scalability – process can take advantage of multiprocessor architectures

7 Multithreaded server architecture
Why is a multithreaded architecture useful in this application?

8 Multithreading Advantages Disadvantages
Concurrency: waiting for user I/O, network I/O, and doing calculations at the same time Better performance if more than one processor Race conditions and deadlock One thread can accidentally modify another thread’s resources. Concurrency can lead to worse performance rather than better.

9 Multithreading vs. multiprocessing
Both allow concurrency and both have issues with synchronization and deadlock. Multithreading Multiprocessing Easier communication (shared memory) Less overhead Can be written in different languages Can be on different machines

10 Pitfalls Watch out for libraries that aren’t thread-safe
Don’t make any assumptions about when threads will execute. Don’t use reasoning like “that will hardly ever happen”. Testing is necessary but not sufficient. Test on a variety of systems.

11 Pthreads The solution to writing portable, multi-threaded applications is to use POSIX threads. POSIX threads work on Linux, FreeBSD, Solaris, AIX, macOS, and other *nix systems. They are not shipped with Windows, but pthread packages are available from other venders.

12 Reentrant code When using threads, you must take care that any code you use is reentrant. Reentrant code can be called more than once, perhaps from different threads, without conflicts. Each copy of the code must have its own copy of any variables that it uses, so must not use global or static variables Be careful when using threads, because some system calls are not threadsafe.

13

14 Creating a new thread the thread handle gets stored here
#include <pthread.h> int pthread_create(pthread_t *thread, pthread_att *attr, void *(*start_routine)(void *), void *arg); thread attributes, usually NULL. returns a 0 if successful otherwise an errno! Note that this differs from the normal Unix standard of returning -1 for an error. the function to begin executing on this thread. It must take a void * as an argument and return a void *. When the function returns, the thread exits. argument given to the thread function. It’s usually a pointer to a struct or an array

15 Terminating a thread A thread exits when the thread function returns.
Alternatively, the thread can call the pthread_exit function. #include <pthread.h> void pthread_exit(void *retval); this must point to data that exists after the thread terminates! Never point to local data defined in the thread!

16 Waiting for a thread the thread to wait for 0 on success
#include <pthread.h> int pthread_join(pthread_t th, void **retval); the thread to wait for 0 on success or error code. pointer to the return value. NULL if there is nothing to return. Note that the thread that started the new thread does not necessarily have to be the thread that executes the pthread_join() function.


Download ppt "Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating."

Similar presentations


Ads by Google