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

Slides:



Advertisements
Similar presentations
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Advertisements

Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Process Concept An operating system executes a variety of programs
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
9/13/20151 Threads ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original slides.
Operating Systems Chapter 5 Threads. Benefits Responsiveness Resource Sharing Economy Utilization of MP Architectures.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads Changes by MA Doman 2013.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Threads A thread is an alternative model of program execution
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Operating System Concepts
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
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:
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Realizing Concurrency using the thread model
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
Introduction to threads
Chapter 4: Threads.
Realizing Concurrency using the thread model
Day 12 Threads.
Threads in C Caryl Rahn.
Threads Threads.
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Linux Processes & Threads
Operating System Concepts
Realizing Concurrency using Posix Threads (pthreads)
Chapter 4: Threads.
Chapter 4: Threads.
Realizing Concurrency using the thread model
Thread Programming.
Realizing Concurrency using the thread model
Operating System Concepts
CHAPTER 4:THreads Bashair Al-harthi OPERATING SYSTEM
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Chapter 4: Threads.
Realizing Concurrency using Posix Threads (pthreads)
Presentation transcript:

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.

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

Single and multithreaded processes

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

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

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

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

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.

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

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.

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.

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.

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

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!

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.