Download Processes

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Berkeley Software Distribution wikipedia , lookup

Plan 9 from Bell Labs wikipedia , lookup

Mobile operating system wikipedia , lookup

CP/M wikipedia , lookup

Spring (operating system) wikipedia , lookup

Copland (operating system) wikipedia , lookup

Unix security wikipedia , lookup

Burroughs MCP wikipedia , lookup

VS/9 wikipedia , lookup

DNIX wikipedia , lookup

Security-focused operating system wikipedia , lookup

Distributed operating system wikipedia , lookup

Process management (computing) wikipedia , lookup

Transcript
Chapter 5: Processes
„ Process Concept
Chapter 5: Processes &
Threads
„ Process Scheduling
„ Operations on Processes
„ Interprocess Communication
„ Communication in Client-Server Systems
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition,
Operating System Concepts – 8th Edition
Objectives
5.2
Silberschatz, Galvin and Gagne ©2009
Process Concept
„ An operating system executes a variety of programs:
„ To introduce the notion of a process -- a program in
execution which forms the basis of all computation
execution,
„ To describe the various features of processes, including
scheduling, creation and termination, and communication
„ To describe communication in client-server systems
z
Batch system – jobs
z
Time-shared systems – user programs or tasks
„ Textbook uses the terms job and process almost interchangeably
„ Process – a program in execution; process execution must
progress in sequential fashion
„ A process includes:
z
Operating System Concepts – 8th Edition
5.3
Silberschatz, Galvin and Gagne ©2009
z
program counter
stack
z
data section
Operating System Concepts – 8th Edition
5.4
Silberschatz, Galvin and Gagne ©2009
Process State
„
Diagram of Process State
As a process executes, it changes state
z
new: The process is being created
z
running: Instructions are being executed
z
waiting: The process is waiting for some event to occur
z
ready: The process is waiting to be assigned to a processor
z
terminated: The process has finished execution
Operating System Concepts – 8th Edition
5.5
Silberschatz, Galvin and Gagne ©2009
Process Control Block (PCB)
Operating System Concepts – 8th Edition
5.6
Silberschatz, Galvin and Gagne ©2009
Process Control Block (PCB)
Information associated with each process
„ Process state
„ Program counter
„ CPU registers
„ CPU scheduling information
„ Memory-management information
„ Accounting information
„ I/O status information
Operating System Concepts – 8th Edition
5.7
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
5.8
Silberschatz, Galvin and Gagne ©2009
CPU Switch From Process to Process
Process Scheduling Queues
„ Job queue – set of all processes in the system
„ Ready queue – set of all processes residing in main memory,
ready and waiting to execute
„ Device queues – set of processes waiting for an I/O device
„ Processes migrate among the various queues
Operating System Concepts – 8th Edition
5.9
Silberschatz, Galvin and Gagne ©2009
Ready Queue And Various I/O Device Queues
Operating System Concepts – 8th Edition
5.11
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
5.10
Silberschatz, Galvin and Gagne ©2009
Representation of Process Scheduling
Operating System Concepts – 8th Edition
5.12
Silberschatz, Galvin and Gagne ©2009
Schedulers
Addition of Medium Term Scheduling
„ Long
Long-term
term scheduler (or job scheduler) – selects which
processes should be brought into the ready queue
„ Short-term scheduler (or CPU scheduler) – selects which
process should be executed next and allocates CPU
5.13
Operating System Concepts – 8th Edition
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Schedulers (Cont)
Silberschatz, Galvin and Gagne ©2009
Context Switch
„ Short-term scheduler is invoked very frequently (milliseconds) ⇒
(must be fast)
„ Long-term scheduler is invoked very infrequently (seconds,
minutes) ⇒ (may be slow)
„ When CPU switches to another process, the system must save the state of
the old process and load the saved state for the new process via a context
switch
„ Context of a process represented in the PCB
„ The long-term scheduler controls the degree of multiprogramming
z
I/O bound process – spends more time doing I/O than
I/O-bound
computations, many short CPU bursts
z
CPU-bound process – spends more time doing computations;
f
few
very long CPU
C
bursts
5.15
„ Context-switch time is overhead; the system does no useful work while
switching
„ Processes can be described as either:
Operating System Concepts – 8th Edition
5.14
Silberschatz, Galvin and Gagne ©2009
„ Time dependent on hardware support
Operating System Concepts – 8th Edition
5.16
Silberschatz, Galvin and Gagne ©2009
Process Creation
Process Creation (Cont)
„ Parent process create children processes, which, in turn create other
processes forming a tree of processes
processes,
„ Generally, process identified and managed via a process identifier (pid)
„ Resource sharing
„ Address space
z
Child d
duplicate
li t off parentt
z
Child has a program loaded into it
„ UNIX examples
z
Parent and children share all resources
z
fork system call creates new process
z
Children share subset of parent’s resources
z
z
Parent and child share no resources
exec system call used after a fork to replace the process’ memory
space with a new program
„ Execution
z
Parent and children execute concurrently
z
Parent waits until children terminate
Operating System Concepts – 8th Edition
5.17
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Process Creation
5.18
Silberschatz, Galvin and Gagne ©2009
Process Termination
„ Process executes last statement and asks the operating system to
delete it (exit)
z
Output data from child to parent (via wait)
z
Process’ resources are deallocated by operating system
„ Parent may terminate execution of children processes (abort)
z
Child has exceeded allocated resources
z
Task assigned to child is no longer required
z
If parent is exiting
Some operating system do not allow child to continue if its
parent terminates
–
Operating System Concepts – 8th Edition
5.19
Silberschatz, Galvin and Gagne ©2009
All children terminated - cascading termination
Operating System Concepts – 8th Edition
5.20
Silberschatz, Galvin and Gagne ©2009
Interprocess Communication
Communications Models
„ Processes within a system may be independent or cooperating
„ Cooperating
C
ti process can affect
ff t or be
b affected
ff t d by
b other
th processes,
including sharing data
„ Reasons for cooperating processes:
z
Information sharing
z
Computation speedup
z
Modularity
z
Convenience
„ Cooperating processes need interprocess communication (IPC)
„ Two models of IPC
z
Shared memory
y
z
Message passing
Operating System Concepts – 8th Edition
5.21
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Cooperating Processes
„ Independent process cannot affect or be affected by the execution of
another process
„ Cooperating process can affect or be affected by the execution of another
process
„ Advantages of process cooperation
z
Information sharing
z
Computation speed
speed-up
up
z
Modularity
z
Convenience
Operating System Concepts – 8th Edition
5.23
Silberschatz, Galvin and Gagne ©2009
5.22
Silberschatz, Galvin and Gagne ©2009
Producer-Consumer Problem
„ Paradigm
g for cooperating
p
gp
processes, p
producer p
process
produces information that is consumed by a consumer
process
z
unbounded-buffer
unbounded
buffer places no practical limit on the size of
the buffer
z
bounded-buffer assumes that there is a fixed buffer size
Operating System Concepts – 8th Edition
5.24
Silberschatz, Galvin and Gagne ©2009
Bounded-Buffer – Producer
Bounded-Buffer – Shared-Memory Solution
„ Shared data
while (true) {
/* Produce an item */
#define BUFFER_SIZE 10
typedef struct {
while (((in = (in + 1) % BUFFER SIZE count) == out)
...
; /* do nothing -- no free buffers */
} item;;
buffer[in] = item;
in = (in + 1) % BUFFER SIZE;
item buffer[BUFFER_SIZE];
[
_
]
}
int in = 0;
int out = 0;
„ Solution is correct, but can only use BUFFER_SIZE-1 elements
5.25
Operating System Concepts – 8th Edition
Silberschatz, Galvin and Gagne ©2009
Bounded Buffer – Consumer
Operating System Concepts – 8th Edition
5.26
Silberschatz, Galvin and Gagne ©2009
Interprocess Communication – Message Passing
„ Mechanism for processes to communicate and to synchronize their actions
while (true) {
„ Message system – processes communicate with each other without
resorting to shared variables
while (in == out)
„ IPC facility provides two operations:
; // do nothing -- nothing to consume
z
send(message) – message size fixed or variable
receive(message)
„ If P and Q wish to communicate, they need to:
z establish a communication link between them
z exchange messages via send/receive
„ Implementation of communication link
z physical (e.g., shared memory, hardware bus)
z logical (e.g., logical properties)
z
// remove an item from the buffer
item = buffer[out];
out = (out + 1) % BUFFER SIZE;
return item;
}
Operating System Concepts – 8th Edition
5.27
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
5.28
Silberschatz, Galvin and Gagne ©2009
Implementation Questions
„ How are links established?
Direct Communication
„ Processes must name each other explicitly:
„ Can
C a lilink
kb
be associated
i t d with
ith more th
than ttwo processes?
?
z
send
d (P,
(P message)) – send
d a message tto process P
„ How many links can there be between every pair of communicating
z
receive(Q, message) – receive a message from process Q
processes?
„ Properties of communication link
„ What is the capacity of a link?
z
Links are established automatically
„ Is the size of a message that the link can accommodate fixed or variable?
z
A link is associated with exactly one pair of communicating processes
„ Is a link unidirectional or bi-directional?
z
Between each pair there exists exactly one link
z
The link may be unidirectional, but is usually bi-directional
Operating System Concepts – 8th Edition
5.29
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Indirect Communication
„ Messages are directed and received from mailboxes (also referred to as
ports)
5.30
Indirect Communication
„ Operations
z
create a new mailbox
z
Each mailbox has a unique id
z
send and receive messages through mailbox
z
Processes can communicate only
y if theyy share a mailbox
z
destroy a mailbox
„ Properties of communication link
Silberschatz, Galvin and Gagne ©2009
„ Primitives are defined as:
z
Link established only if processes share a common mailbox
send(A, message) – send a message to mailbox A
z
A link may be associated with many processes
receive(A, message) – receive a message from mailbox A
z
Each pair of processes may share several communication links
z
Link may be unidirectional or bi
bi-directional
directional
Operating System Concepts – 8th Edition
5.31
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
5.32
Silberschatz, Galvin and Gagne ©2009
Indirect Communication
„ Mailbox sharing
z
P1, P2, and
d P3 share
h
mailbox
ilb A
z
P1, sends; P2 and P3 receive
z
Who gets the message?
Synchronization
„
Message passing may be either blocking or non-blocking
„
Bl ki is
Blocking
i considered
id d synchronous
h
„ Solutions
z
Allow a link to be associated with at most two processes
z
Allow only one process at a time to execute a receive operation
z
Allow the system to select arbitrarily the receiver. Sender is notified
who the receiver was
was.
Operating System Concepts – 8th Edition
5.33
Silberschatz, Galvin and Gagne ©2009
Buffering
„
z
Blocking send has the sender block until the message is
received
z
Blocking receive has the receiver block until a message is
available
Non blocking is considered asynchronous
Non-blocking
z
Non-blocking send has the sender send the message and
continue
z
Non-blocking receive has the receiver receive a valid message
or null
Operating System Concepts – 8th Edition
5.34
Silberschatz, Galvin and Gagne ©2009
Communications in Client-Server Systems
„ Queue of messages attached to the link; implemented in one of three
ways
„ Sockets
„ Remote
R
t P
Procedure
d
Calls
C ll
1. Zero capacity – 0 messages
Sender must wait for receiver (rendezvous)
„ Remote Method Invocation (Java)
2. Bounded capacity – finite length of n messages
Sender must wait if link full
3. Unbounded capacity – infinite length
Sender never waits
Operating System Concepts – 8th Edition
5.35
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
5.36
Silberschatz, Galvin and Gagne ©2009
Sockets
Socket Communication
„ A socket is defined as an endpoint for communication
„ Concatenation
C
t
ti off IP address
dd
and
d portt
„ The socket 161.25.19.8:1625 refers to port 1625 on host
161.25.19.8
„ Communication consists between a pair of sockets
Operating System Concepts – 8th Edition
5.37
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Remote Procedure Calls
5.38
Silberschatz, Galvin and Gagne ©2009
Execution of RPC
„ Remote procedure call (RPC) abstracts procedure calls between processes
on networked systems
„ Stubs – client-side proxy for the actual procedure on the server
„ The client-side stub locates the server and marshalls the parameters
„ The server-side stub receives this message, unpacks the marshalled
parameters, and peforms the procedure on the server
Operating System Concepts – 8th Edition
5.39
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
5.40
Silberschatz, Galvin and Gagne ©2009
Remote Method Invocation
Marshalling Parameters
„ Remote Method Invocation (RMI) is a Java mechanism similar to RPCs
„ RMI allows
ll
a JJava program on one machine
hi tto iinvoke
k a method
th d on a
remote object
Operating System Concepts – 8th Edition
5.41
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
5.42
Silberschatz, Galvin and Gagne ©2009
Content
„ Overview
„ Multithreading
M ltith di M
Models
d l
„ Thread Libraries
Threads
Operating System Concepts – 8th Edition,
„ Threading Issues
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
5.44
Silberschatz, Galvin and Gagne ©2009
Objectives
Single and Multithreaded Processes
„ To introduce the notion of a thread — a fundamental unit of CPU utilization
that forms the basis of multithreaded computer systems
„ To examine issues related to multithreaded programming
Operating System Concepts – 8th Edition
5.45
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Benefits
Silberschatz, Galvin and Gagne ©2009
Multicore Programming
„ Responsiveness
„ Multicore systems putting pressure on programmers, challenges include
„ Resource Sharing
„ Economy
„ Scalability
Operating System Concepts – 8th Edition
5.46
5.47
Silberschatz, Galvin and Gagne ©2009
z
Di idi activities
Dividing
ti iti
z
Balance
z
Data splitting
z
Data dependency
z
Testing and debugging
Operating System Concepts – 8th Edition
5.48
Silberschatz, Galvin and Gagne ©2009
Multithreaded Server Architecture
Operating System Concepts – 8th Edition
5.49
Silberschatz, Galvin and Gagne ©2009
Concurrent Execution on a Single-core System
5.50
Operating System Concepts – 8th Edition
Silberschatz, Galvin and Gagne ©2009
User Threads
Parallel Execution on a Multicore System
„ Thread management done by user-level threads library
„ Three primary thread libraries:
Operating System Concepts – 8th Edition
5.51
Silberschatz, Galvin and Gagne ©2009
z
POSIX Pthreads
z
Win32 threads
z
Java threads
Operating System Concepts – 8th Edition
5.52
Silberschatz, Galvin and Gagne ©2009
Kernel Threads
Multithreading Models
„ Supported by the Kernel
„ Many-to-One
„ Examples
„ One-to-One
z
Windows XP/2000
z
Solaris
z
Linux
z
Tru64 UNIX
z
Mac OS X
„ Many-to-Many
5.53
Operating System Concepts – 8th Edition
Silberschatz, Galvin and Gagne ©2009
Many-to-One
Operating System Concepts – 8th Edition
5.54
Silberschatz, Galvin and Gagne ©2009
Many-to-One Model
„ Many user-level threads mapped to single kernel thread
„ Examples:
E
l
z
Solaris Green Threads
z
GNU Portable Threads
Operating System Concepts – 8th Edition
5.55
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
5.56
Silberschatz, Galvin and Gagne ©2009
One-to-One
One-to-one Model
„ Each user-level thread maps to kernel thread
„ Examples
E
l
z
Windows NT/XP/2000
z
Linux
z
Solaris 9 and later
Operating System Concepts – 8th Edition
5.57
Silberschatz, Galvin and Gagne ©2009
Many-to-Many Model
Operating System Concepts – 8th Edition
5.58
Silberschatz, Galvin and Gagne ©2009
Many-to-Many Model
„ Allows
All
many user llevell th
threads
d tto b
be mapped
d tto many kkernell
threads
„ Allows the operating system to create a sufficient number of
kernel threads
„ Solaris prior to version 9
„ Windows NT/2000 with the ThreadFiber package
Operating System Concepts – 8th Edition
5.59
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
5.60
Silberschatz, Galvin and Gagne ©2009
Two-level Model
Two-level Model
„ Similar to M:M, except that it allows a user thread to be
bound to kernel thread
„ Examples
z
IRIX
z
HP-UX
z
Tru64 UNIX
z
Solaris 8 and earlier
Operating System Concepts – 8th Edition
5.61
Silberschatz, Galvin and Gagne ©2009
Thread Cancellation
„ Terminating
g a thread before it has finished
„ Two general approaches:
z
Asynchronous cancellation terminates the target
th d immediately
thread
i
di t l
z
Deferred cancellation allows the target thread to
periodically check if it should be cancelled
Operating System Concepts – 8th Edition
5.63
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
5.62
Silberschatz, Galvin and Gagne ©2009