程序代写代做 concurrency chain Processes and Threads

Processes and Threads
1

Learning Outcomes
• An understanding of fundamental concepts of processes and threads
2

Major Requirements of an
Operating System
• Interleave the execution of several processes to maximize processor utilization while providing reasonable response time
• Allocate resources to processes
• Support interprocess communication and user creation of processes
3

Processes and Threads
• Processes:
• Also called a task or job
• Execution of an individual program
• “Owner” of resources allocated for program execution • Encompasses one or more threads
• Threads:
• Unit of execution • Can be traced
• list the sequence of instructions that execute • Belongs to a process
• Executes within it.
4

Execution snapshot of three single-threaded processes (No Virtual Memory)

Logical Execution Trace

Combined Traces
(Actual CPU Instructions)
What are the shaded sections?

Summary: The Process Model
• Multiprogramming of four programs
• Conceptual model of 4 independent, sequential processes
(with a single thread each)
• Only one program active at any instant
8

Process and thread models of selected OSes • Single process, single thread
• MSDOS
• Single process, multiple threads
• OS/161 as distributed
• Multiple processes, single thread
• Traditional UNIX
• Multiple processes, multiple threads
• Modern Unix (Linux, Solaris), Windows
Note: Literature (incl. Textbooks) often do not cleanly distinguish between processes and threads (for historical reasons)
10

Process Creation
Principal events that cause process creation
1.
2.
3. 4.
System initialization
• Foreground processes (interactive programs)
• Background processes
• Email server, web server, print server, etc.
• Called a daemon (unix) or service (Windows)
Execution of a process creation system call by a running
process
• New login shell for an incoming ssh connection
User request to create a new process Initiation of a batch job
Note: Technically, all these cases use the same system mechanism to create new processes.
11

Process Termination
Conditions which terminate processes
1. Normal exit (voluntary)
2. Error exit (voluntary)
3. Fatal error (involuntary)
4. Killed by another process (involuntary)
12

Implementation of Processes • A processes’ information is stored in a
process control block (PCB)
• The PCBs form a process table
• Reality can be more complex (hashing, chaining, allocation bitmaps,…)
P7
P6
P5
P4
P3
P2
P1
P0
13

Implementation of Processes
Example fields of a process table entry
14

Process/Thread States
• Possible process/thread states • running
• blocked • ready
• Transitions between states shown
15

Some Transition Causing Events
Running → Ready
• Voluntary Yield() • End of timeslice
Running → Blocked
• Waiting for input • File, network,
• Waiting for a timer (alarm signal)
• Waiting for a resource to become available
16

Scheduler
• Sometimes also called the dispatcher
• The literature is also a little inconsistent on with terminology.
• Has to choose a Ready process to run • How?
• Itisinefficienttosearchthroughallprocesses
17

The Ready Queue
18

What about blocked processes?
• When an unblocking event occurs, we also wish to avoid
scanning all processes to select one to make Ready
19

Using Two Queues
20

Threads
The Thread Model
(a) Three processes each with one thread (b) One process with three threads
22

The Thread Model – Separating execution from the environment.
• Items shared by all threads in a process • Items private to each thread
23

Threads Analogy
The Hamburger Restaurant
24

Single-Threaded Restaurant
Customer Arrives
Take Order
Blocking operations delay all activities
Start Fries
Fries Cook
Wait for Customer
Serve Customer
Fries Finish
Assemble Order
Burger Finished
Burger Cooks
Start Burger
25

Multithreaded Restaurant
Customer Arrives
Start Fries
Fries Cook
Wait for Customer
Take Order
Fries Finish
Start Burger
Serve Customer
Assemble Order
Note: Ignoring synchronisation issues for now
Burger Finished
Burger Cooks
26

Multithreaded Restaurant with more worker threads
Customer Arrives
Start Fries
Fries Cook
Start Fries
Fries Cook
Fries Finish
Fries Finish
Wait for Customer
Take Order
Start Burger
Burger Cooks
Start Burger
Burger Finished
Burger Finished
Burger Cooks
Serve Customer
Assemble Order
Start Burger
Burger Finished
Burger Cooks
27

Finite-State Machine Model (Event-based model)
Input Events
Non-Blocking actions
Customer Arrives
Fries Finish
Burger Finished
Take Order
Assemble Order
Start Fries
Serve Customer
Start Burger
Fries Cook
Burger Cooks
Wait for Customer
External activities
28

Observation: Computation State
Thread Model
Finite State (Event) Model
Customer Arrives
Start Fries
Fries Cook
Start Fries
Fries Cook
Wait for Customer
Serve Customer
Take Order
Assemble Order
Fries Finish
Start Burger
Fries Finish
Start Burger
Customer Arrives
Fries Finish
Burger Finished
Take Order
Assemble Order
Burger Cooks
Start Fries
Serve Customer
Start Burger
Burger Finished
Burger Cooks
Burger Finished
Burger Cooks
Start Burger
Fries Cook
Wait for Customer
• State implicitly stored on the stack.
• State explicitly managed by program
Burger Finished
Burger Cooks
29

The Thread Model
Each thread has its own stack
30

Thread Model
• Local variables are per thread
• Allocated on the stack
• Global variables are shared between all threads • Allocated in data section
• Concurrency control is an issue
• Dynamically allocated memory (malloc) can be global or local
• Program defined (the pointer can be global or local)
31

Thread Usage
A word processor with three threads
32

Thread Usage
A multithreaded Web server
33

Thread Usage
• Rough outline of code for previous slide
(a) Dispatcher thread
(b) Worker thread – can overlap disk I/O with execution of other threads
34

Thread Usage
Three ways to construct a server
35

Summarising “Why Threads?”
• Simpler to program than a state machine
• Less resources are associated with them than a complete process
• Cheaper to create and destroy
• Shares resources (especially memory) between them
• Performance: Threads waiting for I/O can be overlapped with computing threads
• Note if all threads are compute bound, then there is no performance improvement (on a uniprocessor)
• Threads can take advantage of the parallelism available on machines with more than one CPU (multiprocessor)
36