1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Operating system principles - Chapter 4: Threads

20 55 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 20
Dung lượng 160,42 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

This chapter examines some more advanced concepts related to process management, which are found in a number of contemporary operating systems. We show that the concept of process is more complex and subtle than presented so far and in fact embodies two separate and potentially independent concepts: one relating to resource ownership and another relating to execution.

Trang 1

Chapter 4 Threads

• Threads: Resource ownership and

execution

Trang 2

Processes and Threads

• Processes have two characteristics:

– Resource ownership – a process includes

an address space to hold the process image and may be allocated control or ownership of resources

– Scheduling/execution – process execution

follows an execution path that may be

interleaved with other processes

• These two characteristics are treated

independently by the operating system

Trang 3

Processes and Threads

• The unit of dispatching is referred to as a

thread or lightweight process

• The unit of resource ownership is referred

to as a process or task

Trang 4

• The ability of an

OS to support

multiple,

concurrent paths

of execution within

a single process

Trang 5

Single Thread Approaches

• MS-DOS supports a

single user process

and a single thread

• Some UNIX, support

multiple user

processes but only

support one thread

per process

Trang 6

• Java run-time

environment is a

single process with

multiple threads

• Multiple processes

and threads are found

in Windows, Solaris,

and many modern

versions of UNIX

Trang 7

• In a multithreaded environment, a process

is defined as

– a unit of resource allocation: a virtual address space which holds the process image

– a unit of protection: protected access to

processors, other processes (for IPC), files,

I/O resources

Trang 8

One or More Threads in

Process

• Each thread has

– An execution state (running, ready, etc.)

– Saved thread context when not running

– 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)

Trang 9

One view…

• One way to view a thread is as an

independent program counter operating

within a process.

Trang 10

Threads vs processes

Trang 11

Threads vs processes

• The thread control block contains register

values, priority, and other thread-related

state information

• All threads of a process share the state

and resources of that process

– reside in the same address space

– have access to the same data

• when one thread alters a data, other threads see

Trang 12

Benefits of Threads

• Takes less time to create a new thread

than a process (can be 10 times faster)

• Less time to terminate a thread than a

process

• Switching between two threads takes less time that switching processes

• Threads can communicate with each other

– without invoking the kernel

Trang 13

Benefits of Threads

• If an application is implemented as a set of related units of execution, it is far more

efficient to do so as a collection of threads rather than a collection of separate

processes

Trang 14

Example Applications of

Threads

• In a file server, a new thread can be spawned for the file management program per each new file request 

multiple threads within the same process can be

executing simultaneously on different processors

• In a spreadsheet program, one thread could read user

input while another thread executes user commands

• In a word processor, a thread can be created to do

periodic backup asynchronously

• In a multithreaded process, one thread can compute one batch of data while another thread reads the next batch

from an I/O device

Trang 15

Threads vs processes

• Like processes, threads have execution

states

– Running, Ready, and Blocked

• Some states are process-level

– Suspend: if a process is swapped out, all of its threads are necessarily swapped out because they all share the address space of the

Trang 16

Threads vs processes

• Like processes, thread may synchronize

with one another

– Any alteration of a resource by one thread

affects other threads in the same process

• consider two threads each try to add an element to

a linked list at the same time

Trang 17

Example:

Remote Procedure Call

• Consider:

– A program that performs two remote

procedure calls (RPCs)

• to two different hosts

• to obtain a combined result.

Trang 18

RPC Using Single Thread

The program has to wait for a response from

Trang 19

RPC Using One Thread per Server

Trang 20

Multithreading

on a Uniprocessor

Interleaving of multiple threads within

multiple processes on a uniprocessor

Ngày đăng: 30/01/2020, 02:33