20
03/17/22 1 Threads • ICS 240: Operating Systems – William Albritton • Information and Computer Sciences Department at Leeward Community College – Original slides by Silberschatz, Galvin, and Gagne ©2007 from Operating System Concepts with Java, 7th Edition with some modifications – Also includes material by Dr. Susan Vrbsky from the Computer Science Department at the University of Alabama

9/13/20151 Threads ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original slides

Embed Size (px)

Citation preview

04/19/23 1

Threads• ICS 240: Operating Systems

– William Albritton• Information and Computer Sciences Department at

Leeward Community College– Original slides by Silberschatz, Galvin, and Gagne ©2007

from Operating System Concepts with Java, 7th Edition with some modifications

– Also includes material by Dr. Susan Vrbsky from the Computer Science Department at the University of Alabama

04/19/23 2

Threads• A thread (light-weight process) is a single

sequential flow of control consisting of a PC (program counter), register set, stack space– Threads share code section, data section, and OS

resources (such as files)• These items are combined for a task

– A task is the execution environment in which threads run consisting of a code section, data section, and OS resources (such as files)

• A traditional process is a task with one thread (heavy-weight process)

04/19/23 3

Single & Multithreaded Processes

04/19/23 4

Examples• Multiple threads allow for multiple points

of execution– A Web browser may have one thread display

text, while another thread retrieves data from a network

– A word processor may have one thread handling the user’s keystrokes, and another thread doing the spell check

– A Web server may be serving 1,000 different clients with 1,000 different threads

04/19/23 5

Benefits of Using Threads1. Responsiveness

– An application can do several things as once• For example, a browser can use one thread for I/O

and another thread to download an image

2. Resource Sharing– Code, data, and other resources are shared

• Can have multiple threads using the same address space in memory

04/19/23 6

Benefits of Using Threads3. Economy

– Thread creation is less costly than process creation

• With the Solaris Operating System of Sun Microsystems, creating a thread is 30 times faster than creating a process, and 5 times faster than context switching

4. Utilization of MP (Multiple Processor) Architectures

– Schedule a separate thread on each processor

04/19/23 7

User and Kernel Threads• User threads

– Thread management done by user-level threads library.

• Kernel threads – Threads directly supported by the kernel.

04/19/23 8

Multithreading Models• Mapping user threads to kernel threads

1. Many-to-One

2. One-to-One

3. Many-to-Many

04/19/23 9

Many-to-One• Many user-level threads mapped to a

single kernel thread– Efficient, because thread management done

by thread library in user space– Cannot take advantage of running multiple

threads in parallel on multiprocessors

• Examples– Solaris Green Threads– GNU Portable Threads

04/19/23 10

Many-to-One Model

04/19/23 11

One-to-One• Each user-level thread maps to a single

kernel thread– Kernel threads can run in parallel on a

multiprocessor– Possible to create too many kernel threads

and overburden the system

• Examples– Windows NT/XP/2000– Linux– Solaris 9 and later

04/19/23 12

One-to-one Model

04/19/23 13

Many-to-Many Model• Allows many user level threads to be

mapped to many kernel threads– Allows the operating system to create a

sufficient number of kernel threads– Kernel threads can run in parallel on a

multiprocessor

• Examples – Solaris prior to version 9– Windows NT/2000 with the ThreadFiber

package

04/19/23 14

Many-to-Many Model

04/19/23 15

Java Threads• Java threads are managed by the JVM

(Java Virtual Machine)– The JVM is can be thought of as a software

computer that runs inside a hardware computer

• Java threads may be created by:– Implementing the Runnable interface

04/19/23 16

Summation Program

04/19/23 17

Summation Program

04/19/23 18

Producer-Consumer Program

04/19/23 19

Producer-Consumer Program

04/19/23 20

Producer-Consumer Program