Processes Operating System Concepts

advertisement
Processes
Operating System Concepts
Module 4: PROCESSES
Andrzej Bednarski, Ph.D. student
Department of Computer and Information Science
Linköping University, Sweden
E-mail: andbe@ida.liu.se
URL: http://www.ida.liu.se/~andbe
•
Process Concept
•
Process Scheduling
•
Operation on Processes
•
Cooperating Processes
•
Interprocess Communication
•
Threads
The lecture notes are mainly based on Silberschatz’s, Galvin’s and Gagne’s book (“Operating System Concepts”, 6th ed.,
Addison-Wesley, 2002). No part of the lecture notes may be reproduced in any form, due to the copyrights reserved by
Addison-Wesley. These lecture notes should only be used for internal teaching purposes at the Linköping University.
4.1
TDDB63 – Operating System Concepts –
TDDB63 – Operating System Concepts –
A. Bednarski
Process Concept
Process Concept (Cont.)
•
An operating system executes a variety of programs:
+ Batch system – jobs
+ 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.
+ Note, program is a passive entity (processes are not)
+ Multiple processes may be associated with the same program
TDDB63 – Operating System Concepts –
A. Bednarski
4.3
Process State
•
4.2
A process includes:
+ Program code (text section)
+ Program counter (PC: current activity)
+ Register contents
+ Stack (containing temporary data: optional)
+ Data section (global variable: optional)
TDDB63 – Operating System Concepts –
A. Bednarski
4.4
Diagram of Process State
As a process executes, it changes state
+ New: The process is being created.
+ Running: Instructions are being executed.
+ Waiting: The process is waiting for some event to occur.
+ Ready: The process is waiting to be assigned to a
processor.
+ Terminated: The process has finished its execution.
TDDB63 – Operating System Concepts –
A. Bednarski
4.5
TDDB63 – Operating System Concepts –
A. Bednarski
4.6
1
Process Control Block (PCB)
Process Control Block PCB (Cont.)
Information associated with each process.
•
Memory management information
+ Base and limit registers
•
Accounting information
•
I/O status information
+ List of I/O devices allocated to process, open files etc.
•
Process state
+ New, ready, running, waiting, …
•
Program counter
+ Address of the next instruction
•
CPU registers
+ Accumulators, index registers, stack pointers, general-purpose
registers
•
CPU scheduling information
+ Process priorities, pointers to scheduling queues etc.
TDDB63 – Operating System Concepts –
A. Bednarski
4.7
Process Control Block (PCB)
TDDB63 – Operating System Concepts –
A. Bednarski
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.
•
”Process migration” between the various queues.
TDDB63 – Operating System Concepts –
A. Bednarski
TDDB63 – Operating System Concepts –
A. Bednarski
4.8
CPU Switch From Process to Process
4.9
TDDB63 – Operating System Concepts –
A. Bednarski
4.10
Ready Queue And Various I/O Device
Queues
Process Scheduling Queues
•
PCB is sometimes denoted as task control block.
4.11
TDDB63 – Operating System Concepts –
A. Bednarski
4.12
2
Schedulers
Representation of Process Scheduling
TDDB63 – Operating System Concepts –
A. Bednarski
4.13
•
Long-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.
TDDB63 – Operating System Concepts –
A. Bednarski
Schedulers (Cont.)
Addition of Medium Term Scheduling
•
Short-term scheduler is invoked very frequently (milliseconds)
Ÿ (must be fast).
•
•
Long-term scheduler is invoked very infrequently (seconds,
minutes) Ÿ (may be slow).
•
The long-term scheduler controls the degree of
multiprogramming.
•
Processes can be described as either:
+ I/O-bound process – spends more time doing I/O than
computations, many short CPU bursts.
+ CPU-bound process – spends more time doing computations;
few very long CPU bursts.
+ Best performance: Balance between I/O-bound and CPUbound processes.
TDDB63 – Operating System Concepts –
A. Bednarski
4.15
4.14
Controls swapping
+ Removes processes from memory
Decreases degree of multiprogramming (swap out)
+ Reloads processes later (swap in)
TDDB63 – Operating System Concepts –
A. Bednarski
4.16
Context Switch
Process Creation
•
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.
•
Parent process creates children processes, which, in turn create
other processes, forming a tree of processes (system call).
•
Context-switch time is overhead; the system does no useful work
while switching.
•
•
Time dependent on hardware support (from 1µs to 1ms).
+ E.g., consider an architecture having multiple sets of registers.
Resource sharing
Goal: Preventing system overloading
+ Parent and children share all resources.
+ Children share subset of parent’s resources.
+ Parent and child share no resources.
•
Execution
+ Parent and children execute concurrently.
+ Parent waits until children terminate.
TDDB63 – Operating System Concepts –
A. Bednarski
4.17
TDDB63 – Operating System Concepts –
A. Bednarski
4.19
3
A Tree of Processes on a
Typical UNIX System
Process Creation (Cont.)
•
Address space
+ Child duplicate of parent.
+ Child has a program loaded into it.
•
UNIX examples
+ pid - process identifier
+ fork system call creates new process, holding a copy of the
memory space of the parent process
+ execve system call used after a fork to replace the process’
memory space with a new program.
•
Windows NT support two models
+ Duplication like fork
+ Creation of new process with address to new program to be
loaded
TDDB63 – Operating System Concepts –
A. Bednarski
4.20
TDDB63 – Operating System Concepts –
4.21
A. Bednarski
Process Termination
Cooperating Processes
•
Process executes last statement and asks the operating system to
decide it (exit).
+ Output data from child to parent (parent issues a wait).
+ Process’ resources are deallocated (released) by operating
system.
•
Independent process cannot affect or be affected by the
execution of another process (no data sharing).
•
Cooperating process can affect or be affected by the execution of
another process
Parent may terminate execution of children processes (abort).
+ Child has exceeded allocated resources.
+ Task assigned to child is no longer required.
+ Parent is exiting.
Operating system does not allow child to continue if its
parent terminates.
•
Advantages of process cooperation
+ Information sharing
Also enables fault-tolerance, e.g., N-version redundancy
+ Computation speed-up
+ Modularity
+ Convenience
•
Note, this depends on the type of coupling that is supported!
Cascading termination.
TDDB63 – Operating System Concepts –
A. Bednarski
4.22
•
Paradigm for cooperating processes, producer process produces
information that is consumed by a consumer process.
+ unbounded-buffer places no practical limit on the size of the
buffer.
+ bounded-buffer assumes that there is a fixed buffer size.
A. Bednarski
Producer
2
Shared
in:
1 Data
0
#define
BUFFER_SIZE
3
1
out:
2
0
typedef struct {
/* … */
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Variables in, out are initialized to 0
+ n is the number of slots in buffer
+ in points to the next free position in the buffer
+ out points to the first full position in the buffer
+ Buffer is empty when in = out
+ Buffer is full when in + 1 mod n = out
Buffer is a circular array
TDDB63 – Operating System Concepts –
4.23
A. Bednarski
Bounded-Buffer –
Shared-Memory Solution
Producer-Consumer Problem
•
TDDB63 – Operating System Concepts –
I1
I4
I2
I3
0
1
2
Bounded Buffer
4.24
TDDB63 – Operating System Concepts –
while (1) {
/* produce an item in nextProduced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
}
Consumer
while (1) {
/* produce an item in nextProduced */
while (in == out)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
/* consume the item in nextConsumed */
}
A. Bednarski
4.25
4
Interprocess Communication (IPC)
Implementation Questions
•
•
How are links established?
•
Can a link be associated with more than two processes?
•
How many links can there be between every pair of
communicating processes?
•
What is the capacity of a link?
•
Is the size of a message that the link can accommodate fixed or
variable?
•
Is a link unidirectional or bi-directional?
•
•
•
•
Mechanism for processes to communicate and to synchronize their
actions.
Message system – processes communicate with each other
without resorting to shared variables.
IPC facility provides two operations:
+ send(message) – message size fixed or variable
+ receive(message)
If P and Q wish to communicate, they need to:
+ establish a communication link between them
+ exchange messages via send/receive
Implementation of communication link
+ physical (e.g., shared memory, hardware bus)
+ logical (e.g., logical properties)
TDDB63 – Operating System Concepts –
A. Bednarski
4.26
Direct Communication
•
Processes must name each other explicitly:
+ send (P, message) – send a message to process P
+ receive (Q, message) – receive a message from process Q
•
Properties of communication link
+ Links are established automatically.
+ A link is associated with exactly one pair of communicating
processes.
+ Between each pair there exists exactly one link.
+ The link may be unidirectional, but is usually bi-directional.
TDDB63 – Operating System Concepts –
A. Bednarski
TDDB63 – Operating System Concepts –
4.28
•
Messages are directed to/received from mailboxes (ports).
+ Each mailbox has a unique id.
+ Processes can communicate only if they share a mailbox.
•
Properties of communication link
+ Link established only if processes share a common
mailbox
+ A link may be associated with many processes.
+ Each pair of processes may share several communication
links (different mailboxes).
+ Link may be unidirectional or bi-directional.
•
Operations on OS mailboxes
+ create a new mailbox
+ send and receive messages through mailbox
+ destroy a mailbox
TDDB63 – Operating System Concepts –
Synchronization
•
•
Mailbox sharing
+ P1 , P2 , and P3 share mailbox A.
+ P1 issues a send; P2 and P3 issues a receive.
+ Who gets the message?
Solutions
+ Allow a link to be associated with at most two processes.
+ Allow only one process at a time to execute a receive
operation.
+ Allow the system to select arbitrarily the receiver. Sender is
notified who the receiver was.
TDDB63 – Operating System Concepts –
A. Bednarski
4.27
Indirect Communication
Indirect Communication cont.
•
A. Bednarski
4.30
A. Bednarski
4.29
Communication: send/receive primitives
(different implementations)
+ Blocking/Synchronous
send: sender blocked until message received (phone)
receive: receiver blocks until a message is available
+ Nonblocking/Asynchronous
send: sends and resumes (SMS)
receive: if there is a message, retrieve it, otherwise empty
message
TDDB63 – Operating System Concepts –
A. Bednarski
4.31
5
Buffering
Threads
•
Temporary queues
•
Queue of messages attached to the link; implemented in one of
three ways.
1. Zero capacity – 0 messages
Sender must wait for receiver (rendezvous).
aka “message system with no buffering”
•
A thread (or lightweight process) is a basic unit of CPU
utilization; it consists of:
+ program counter
+ register set
+ stack space
•
A thread shares with its peer threads its:
+ code section
+ data section
+ operating-system resources
collectively know as a task.
•
A traditional or heavyweight process is equal to a task with
one thread
•
Advantage: Use of threads minimizes context switches
without loosing parallelism.
2. Bounded capacity – finite length of n messages
Sender must wait if link full.
aka “automatic buffering”
3. Unbounded capacity – infinite length
Sender never waits.
aka “automatic buffering”
TDDB63 – Operating System Concepts –
4.32
A. Bednarski
Threads (Cont.)
•
•
•
•
•
TDDB63 – Operating System Concepts –
A. Bednarski
4.33
Multiple Threads within a Task
In a multiple threaded task, while one server thread is
blocked and waiting, a second thread in the same task can
run.
+ Cooperation of multiple threads in same job confers
higher throughput and improved performance.
+ Applications that require sharing a common buffer (i.e.,
producer-consumer) benefit from thread utilization.
Threads provide a mechanism that allows sequential
processes to make blocking system calls while also achieving
parallelism.
Kernel-supported threads (Mach and OS/2).
+ System calls
User-level threads; supported above the kernel, via a set of
library calls at the user level.
Hybrid approach implements both user-level and kernelsupported threads (Solaris 2).
TDDB63 – Operating System Concepts –
4.34
A. Bednarski
Solaris 2 Threads
TDDB63 – Operating System Concepts –
A. Bednarski
4.35
Summary
•
•
•
•
•
•
Process / Process state (diagram)
PCB and Context Switch
Waiting queues
Long/Medium/Short – term scheduling
Independent vs dependent processes
IPC
Thread is
pinned to
a CPU
TDDB63 – Operating System Concepts –
A. Bednarski
4.37
TDDB63 – Operating System Concepts –
A. Bednarski
4.39
6
Recommended Reading and Exercises
•
Reading
+ Chapter 4 & 5 (6th edition), Chapter 4 (5th edition)
RPC, Client/Server, Sockets, RMI, Mach OS: recommended
•
Exercises:
+ All (4.8 needs sockets section)
TDDB63 – Operating System Concepts –
A. Bednarski
4.40
7
Download