Transcript 4. Process
Operating Systems
Certificate Program in Software Development
CSE-TC and CSIM, AIT
September -- November, 2002
4. Processes and Threads
(Ch. 4 S&G)
Objectives
chs 4 and 5
in the 6th ed.
– a review of OS processes and threads
OSes: 4. Processes
1
Overview
1.
2.
3.
4.
5.
6.
OSes: 4. Processes
Processes
Process Scheduling
Operations on Processes
Cooperating Processes
Interprocess Communication (IPC)
Client/Server Communication
continued
2
7. Threads
8. Thread Types
9. Multithreading Models
10. Pthreads
OSes: 4. Processes
3
1. Processes
A process
is an executing program.
Each
process has its own program counter,
stack pointer, address space.
Several
processes may execute the same
program, but they will have copies of the
program data.
OSes: 4. Processes
4
Process Control Block (PCB)
Fig. 4.2., p.91
process
next
previous
state
Process ID (PID)
program counter
registers
memory structure
open file table
etc
OSes: 4. Processes
5
States of a Process
dispatch
new
ready
Fig. 4.1, p.90
running
exit
interrupt
terminated
event
completed
OSes: 4. Processes
waiting
I/O event
or wait
6
2. Process Scheduling
Job
scheduler (long-term scheduler)
– select processes from storage to add to ready
queue
CPU
scheduler (short-term scheduler)
– allocates a processor to a ready process
OSes: 4. Processes
7
Queuing Diagram
Fig. 4.5, p.95;
VUW CS 305
ready queue
I/O
cpu
I/O queue
child
executes
join
wait queue
resource queue
OSes: 4. Processes
I/O request
time slice
expired
fork a
child
resource
request
8
Medium-term Scheduling
swap in
partially executed
swapped out processes
ready queue
I/O
OSes: 4. Processes
Fig. 4.6, p.96
swap out
cpu
I/O queue
end
I/O request
9
Context Switching
Switching
from one process to another
– often takes tens of microseconds
Save
executing process in its PCB
– registers, program counter, state, …
Load/reload
OSes: 4. Processes
a process from its PCB
continued
10
OSes: 4. Processes
11
3. Operations on Processes
Process
creation (fork)
– parent/child relationship
– child is usually a copy of its parent
– usually they continue in parallel
Process
termination
– exit, abort, or kill
Suspension
– internal (wait) or external (resource wait)
OSes: 4. Processes
12
4. Cooperating Processes
Cooperating
processes can affect each other
– compare to independent processes
Advantages
of process cooperation:
– information sharing, (possible) computation
speed-up, modularity, convenience
OSes: 4. Processes
13
Producer/Consumer
A producer
process produces information
that is consumed by a consumer process.
Many
variations, e.g.:
– unbounded-buffer
– bounded-buffer
shared memory
buffer
p
OSes: 4. Processes
c
14
5. Interprocess Communication (IPC)
IPC
is allows processes to communicate and
synchronize their actions.
A message-based approach:
– processes communicate without shared vars
Two basic operations:
– send(message)
– receive(message)
OSes: 4. Processes
15
Messaging Issues
Message
format
Unidirectional, bi-directional?
One-to-one, broadcasting, multicasting?
Direct, indirect links?
– names, locating, mailboxes
Blocking
(synchronous), non-blocking
(asynchronous)?
Sender/receiver buffering
OSes: 4. Processes
16
6. Client/Server Communication
A server
is a program (or collection of
cooperating programs) that provides
services and/or manages resources on the
behalf of other programs (its clients).
OSes: 4. Processes
17
Client/Server Environment
clients
LAN
or WAN
network
Server
OSes: 4. Processes
Data
18
Example
The ATM
network:
– the clients are the ATM machines
user interfaces;
some simple application processing
– the server is at the bank
most application processing;
very large database of customer accounts
OSes: 4. Processes
19
Architectural Requirements
Reliable, robust communication between the clients
and server.
Client/server cooperation
– started by the client
Application processing is usually distributed
between a client and the server.
Server controls services/data that the client accesses.
Server handles conflicting requests.
OSes: 4. Processes
20
Communication Types
Sockets
– (TCP) sockets are like telephones
– the client and server sockets are the endpoints
of their communications link
Remote
Procedure Calls (RPC)
– the client ‘calls’ a function in the server
Remote
Method Invocation (RMI)
– the Java version of RPC
OSes: 4. Processes
21
7. Threads
A thread
shares its data and OS resources
(e.g. open files) with its peer threads
Sharing
and smaller size makes threads less
expensive to create & context switch
– sometimes called light-weight processes
(LWPs)
OSes: 4. Processes
22
Threads Diagram
Fig. 4.8, p.104
PC
PC
PC
peer threads
shared data
OSes: 4. Processes
23
Typical Thread States
JUST_CREATED
READY
RUNNING
BLOCKED
OSes: 4. Processes
24
Typical Thread Operations
fork()
– creates a new thread sharing global structures
yield()
– yields processor, enters ready queue
sleep()
– yields processor, enters a wait queue
finish()
OSes: 4. Processes
25
Threading Issues
Semantics
of fork() and exec() calls
Thread cancellation
Signal handling
Thread pools
Thread specific data
OSes: 4. Processes
26
8. Thread Types
Kernel
threads
– useful for parallelising system features
– e.g. Windows 9x/NT/2000, Linux
User
threads
– scheduled within a user process
– not seen by the kernel
– e.g. POSIX Pthreads, Solaris threads
OSes: 4. Processes
27
9. Multithreading Models
Many-to-One
One-to-One
Many-to-Many
OSes: 4. Processes
28
9.1 Many-to-One
Many
user-level threads are mapped to a
single kernel thread (or process).
Often
used on systems
that do not support
kernel threads.
OSes: 4. Processes
29
9.2. One-to-One
Each
user-level thread maps to a kernel thread.
Examples
– Windows 9x/NT/2000
– OS/2
OSes: 4. Processes
30
Windows 2000
Each
–
–
–
–
OSes: 4. Processes
thread contains
a thread id
a register set
separate user and kernel stacks
a private data storage area
31
9.3. Many-to-Many Model
Allows
many user level threads to be
mapped to many kernel threads.
Examples:
– Solaris 2
– Windows NT/2000
with the ThreadFiber
package
OSes: 4. Processes
32
Solaris 2
Fig. 4.9, p.107
user
threads
tasks
lwp
lwp
lwp
lwp
lwp
lwp
kernel
threads
kernel
cpu
OSes: 4. Processes
cpu
cpu
33
10. Pthreads
A POSIX
standard API for thread creation
and synchronization.
The API
specifies the behavior of the thread
library, but the implementation is up to the
library developer.
Commonly
OSes: 4. Processes
found in UNIX, and variants.
34