Download Processes and Process Control Blocks

Document related concepts

VS/9 wikipedia , lookup

Unix security wikipedia , lookup

Distributed operating system wikipedia , lookup

DNIX wikipedia , lookup

Burroughs MCP wikipedia , lookup

Thread (computing) wikipedia , lookup

Process management (computing) wikipedia , lookup

Transcript
1
 Process
can be defined as the followings:
A program in execution
 An instance of a program running on a computer.
 The entity that can be assigned to and executed on a
processor.
A unit of activity characterized by the execution of a
sequence of instructions, a current state, and an
associated set of system resources.
2
A
batch system executes jobs, whereas a timeshared system has user programs, or tasks.
 Even on a single-user system, such as
Microsoft Windows and Macintosh OS, a user
may be able to run several programs at one
time: a word processor, web browser, and email package.
3
 Even
if the user can execute only one program
at a time, the operating system may need to
support its own internal programmed activities,
such as memory management.
4
OS can manage the execution of applications so
that:
 Resources are made available to multiple
applications.
 The physical processor is switched among
multiple applications so all will appear to be
progressing.
 The processor and I/O devices can be used
efficiently.
5
 Two
essential elements of a process are
program code(which may be shared with other
processes that are executing the same program)
and a set of data associated with that code.
 At any given point in time, while the program
is executing, this process can be uniquely
characterized by a number of elements,
including the following:
6
The information in the list
is stored in a data
structure, typically called a
process control block
(PCB), that is created and
managed by the OS and
uniquely identifies a
process.
7
 Accounting
information: This information
includes the amount of CPU and real time
used, time limits, account numbers, job or
process numbers, and so on.
 status information: The information includes
the list of I/O devices allocated to this process,
a list of open files(Active files) , and so on.
8
 The
process control block is the key tool that
enables the OS to support multiple processes
and to provide for multiprocessing.
 When a process is interrupted, the current
values of the program counter and the
processor registers (context data) are saved in
the appropriate fields of the corresponding
process control block, and the state of the
process is changed to some other value, such as
blocked or ready.
9
 We
can characterize the behavior of an
individual process by listing the sequence of
instructions that execute for that process.
 Such a listing is referred to as a trace of the
process.
 We can characterize behavior of the processor
by showing how the traces of the various
processes are interleaved.
 A small dispatcher program is used to switch
the processor from one process to another.
10

The operating system’s principal responsibility is controlling
the execution of processes; this includes determining the
interleaving pattern for execution and allocating resources to
processes.
11
The Creation and Termination of Processes
 When a new process is to be added to those
currently being managed, the OS builds the
data structures that are used to manage the
process and allocates address space in main
memory to the process.
 When one process spawns another, the former
is referred to as the parent process, and the
spawned process is referred to as the child
process.
12
 Reasons
for job creation
13
Any computer system must provide a means for a
process to indicate its completion.
 A batch job should include a Halt instruction or an
explicit OS service call for termination.
 In the former case, the Halt instruction will generate an
interrupt to alert the OS that a process has completed.
 For an interactive application, the action of the user will
indicate when the process is completed.
 On a personal computer or workstation, a user may quit
an application (e.g., word processing or spreadsheet).
 All of these actions ultimately result in a service request
to the OS to terminate the requesting process.

14
 Five
state process model
15
 Running:
The process that is currently being
executed
 Ready: A process that is prepared to execute
when given the opportunity.
 Blocked/Waiting: A process
that cannot
execute until some event occurs, such as the
completion of an I/O operation.
16
 New:
A process that has just been created but
has not yet been admitted to the pool of
executable processes by the OS. Typically, a
new process has not yet been loaded into main
memory, although its process control block has
been created.
 Terminated: A process that has been released
from the pool of executable processes by the
OS, either because it halted or because it
aborted for some reason.
17

The new process will terminate, usually
due to one of the following conditions:
1. Normal exit (voluntary).
2.Error exit (voluntary).
3.Fatal error (involuntary).
4.Killed by another process(involuntary).
18
19

Process can be executed in one of the following modes:
User mode
Kernel mode
Considerations of memory protection and privileged
instructions lead to the concept of modes of operation.
 A user program executes in a user mode, in which
certain areas of memory are protected from the user’s
use and in which certain instructions may not be
executed.
 The monitor executes in a system mode, or called kernel
mode, in which privileged instructions may be executed
and in which protected areas of memory may be
accessed.

20
If the OS is to manage processes and resources, it must
have information about the current status of each
process and resource.
 Although the details will differ from one OS to another,
fundamentally, all operating systems maintain
information in these four categories.

Memory tables
I/O tables
File tables and
Process tables
21
To manage and control a process the OS must know first
where the process is located; second, it must know the
attributes of the process that are necessary for its
management (e.g., process ID and process state).
 A process includes a set of programs to be executed,
data, stack, and attributes. We can refer to this collection
as the process image.

22


At some time, a running process is interrupted and the OS
assigns another process to the Running state and turns control
over to that process.
A process switch may occur any time that the OS has gained
control from the currently running process.
23
 First, let us consider system interrupts.
 Actually, we can distinguish, as many systems
do,
two kinds of system interrupts, one of which is
simply referred to as an interrupt, and the other as
a trap.
 The former is due to some sort of event that is
external to and independent of the currently
running process, such as the completion of an I/O
operation.
 The latter relates to an error or exception
condition generated within the currently running
process, such as an illegal file access attempt.
24
The concurrent processes executing in the operating
system may be either independent processes or
cooperating processes.
 A process is independent if it cannot affect or be
affected by the other processes executing in the system.
 Clearly, any process that does not share any data
(temporary or persistent) with any other process is
independent.
 On the other hand, a process is cooperating if it can
affect or be affected by the other processes executing in
the system.
 Clearly, any process that shares data with other
processes is a cooperating process.

25
We may want to provide an environment that allows
process cooperation for several reasons:
 Information sharing: Since several users may be
interested in the same piece of
information(for
instance, a shared file), we must provide an
environment to allow concurrent access to these types
of resources.
 Computation speedup: If we want a particular task to
run faster, we must break it into subtasks, each of which
will be executing in parallel with the others. Such a
speedup can be achieved only if the computer has
multiple processing elements (such as CPU or I/O
channels).
26
 Modularity:
We may want to construct the
system in a modular fashion, dividing the
system functions into separate processes or
threads.
 Convenience: Even an individual user may
have many tasks on which to work at one time.
For instance, a user may be editing, printing,
and compiling in parallel.
27
 To
illustrate the concept of cooperating processes,
let us consider the producer-consumer problem,
which is a common paradigm for cooperating
processes.
 A producer process produces information that is
consumed by a consumer process.
 For example, a print program produces characters
that are consumed by the printer driver.
 A compiler may produce assembly code, which is
consumed by an assembler. The assembler, in turn,
may produce object modules, which are consumed
by the loader.
28
The concept of a process as embodying two characteristics:
 Resource ownership: A process includes a virtual address space
to hold the process image; recall that the process image is the
collection of program, data, stack, and attributes defined in the
process control block. From time to time, a process may be
allocated control or ownership of resources. The OS performs a
protection function to prevent unwanted interference between
processes with respect to resources.
 Scheduling/execution: The execution of a process follows an
execution path(trace) through one or more programs. This
execution may be interleaved with that of other processes.
 Thus, a process has an execution state and a dispatching
priority and is the entity that is scheduled and dispatched by the
OS.
29
 These
two characteristics are independent and
could be treated independently by the OS.
 To distinguish the two characteristics, the unit
of dispatching is usually referred to as a thread
or lightweight process, while the unit of
resource ownership is usually still referred to
as a process or task.
30
 Processes
do not share resources well
 high context switching overhead
 Idea:
Separate concurrency from protection
 Multithreading: a single program made up of a
number of different concurrent activities
 A thread (or lightweight process)
 basic unit of CPU utilization; it consists of: program counter,
register set and stack space
 A thread shares the following with peer threads: code section, data
section and OS resources (open files, signals)
No protection between threads
Collectively called a task.
 Heavyweight
process is a task with one thread.
31
32
1.
2.
3.
Benefits of Multithreaded programming
Responsiveness
Resource Sharing
Economy: It is much more time consuming to
create and manage processes that threads. Eg. In
Solaris, creating a process is thirty times slower than creating
thread and context switching is five times slower.
4.
Utilization of Multiprocessor Architectures
33
Multithreading
 Multithreading refers to the ability of an OS to
support multiple, concurrent paths of execution
within a single process.
 In a multithreaded environment, a process is
defined as the unit of resource allocation and a
unit of protection.
34
Thread State
 State shared by all threads in process/addr space
 Contents of memory (global variables, heap)
 I/O state (file system, network connections, etc)
 State
“private” to each thread
 Kept in TCB (Thread Control Block)
 CPU registers (including, program counter)
 Execution stack
 Parameters, Temporary variables
 return PCs are kept while called procedures are executing
35
 Thread
context switch still requires a register set
switch, but no memory management related
work!!
 Thread states –
 ready, blocked, running, terminated
 Threads
share CPU and only one thread can run
at a time.
 No protection among threads.
36
Types of threads
Kernel Supported Threads (Mach and OS/2)
User Level Threads
Hybrid approach implements both user-level
and kernel-supported threads (Solaris 2)
37
Kernel Threads
 Supported by the Kernel
1.
 Native threads supported directly by the kernel
 Every thread can run or block independently
 Downside of kernel threads: a bit expensive
 Need to make a crossing into kernel mode to schedule
 Examples
 Windows XP/2000, Solaris, Linux,Tru64 UNIX, Mac OS X,
Mach, OS/2
38
2. User Level Threads
 Supported above the kernel,
via a set of library calls
at the user level.
Thread management done by user-level threads library
 User program provides scheduler and thread package
May have several user threads per kernel thread
User threads may be scheduled non-premptively relative to each other
(only switch on yield())
 Advantages
Cheap, Fast
Threads do not need to call OS and cause interrupts to kernel
 Disadv: If kernel is single threaded, system call from
any thread can block the entire task.
 Example thread libraries:
 POSIX Pthreads, Win32 threads, Java threads
39
Multithreading Models
 There are three common ways of establishing
relationship between user threads and kernel
threads
Many – to – one
One – to – one
Many – to – many
40
1.

Many – to – one model
Maps many user level threads to one kernel
thread
• Efficient
• The entire process may block
if a thread make a blocking
system call
•Examples:
Solaris Green Threads
GNU Portable Threads
41
2.
One – to – One model

Each user thread maps to a kernel thread

Creating kernel threads for each user can burden the
performance of an application: Most OS limit the
number of threads
Example:

 Windows NT/XP/2000; Linux; Solaris 9 and later
42
3.
Many – to – Many Model
 Allows
many user level
threads to be mapped to
many kernel threads
 Allows the OS to create
a sufficient number of
kernel threads
 Solaris prior to version 9
 Windows NT/2000 with
the ThreadFiber package
43

Separate address space isolates processes
• High Creation/Memory Overhead; (Relatively) High Context-Switch
Overhead

Mechanism for processes to communicate and synchronize actions.
 Via shared memory - Accomplished by mapping addresses to common
DRAM
 Read and Write through memory
 Via Messaging system - processes communicate without resorting to shared
variables.
 send() and receive() messages
 Can be used over the network!
 Messaging system and shared memory not mutually exclusive can be
used simultaneously within a single OS or a single process.
44

Communication occurs by “simply” reading/writing to shared
address page
Really low overhead communication
Introduces complex synchronization problems
45
 IPC facility provides two operations.
send (message) - message size can be fixed or variable
receive (message)
 If processes P and Q wish to communicate,
they
need to:
 establish a communication link between them
 exchange messages via send/receive
 Fixed vs. Variable size message
 Fixed message size – straightforward physical implementation,
programming task is difficult due to fragmentation
 Variable message size - simpler programming, more complex
physical implementation.
46
 Sender
and Receiver 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.
 Exactly one link between each pair.
 Link may be unidirectional, usually bidirectional.
47
 Messages
are directed to and received from
mailboxes (also called ports)
 Unique ID for every mailbox.
 Processes can communicate only if they share a mailbox.
send(A, message) /* send message to mailbox A */
receive(A, message) /* receive message from mailbox A */
 Properties
of communication link
 Link established only if processes share a common
mailbox.
 Link can be associated with many processes.
 Pair of processes may share several communication links
 Links may be unidirectional or bidirectional
48
49
 Operations
create a new mailbox
send/receive messages through mailbox
destroy a mailbox
 Issue:
Mailbox sharing
P1, P2 and P3 share mailbox A.
P1 sends message, P2 and P3 receive… who gets message??
 Possible
Solutions
disallow links between more than 2 processes
allow only one process at a time to execute receive operation
allow system to arbitrarily select receiver and then notify
sender.
50
 Link
has some capacity - determine the number
of messages that can reside temporarily in it.
 Queue of messages attached to link
Zero-capacity Queues: 0 messages
 sender waits for receiver (synchronization is called
rendezvous)
Bounded capacity Queues: Finite length of n
messages
 sender waits if link is full
Unbounded capacity Queues: Infinite queue
length
 sender never waits
51
 Lost
Messages
• OS guarantees retransmission
• sender is responsible for detecting it using timeouts
• sender gets an exception
 Scrambled
Messages
• Message arrives from sender P to receiver Q, but
information in message is corrupted due to noise in
communication channel.
• Solution
 need error detection mechanism, e.g. CHECKSUM
 need error correction mechanism, e.g. retransmission
52
 Paradigm
for cooperating processes;
• producer process produces information that is
consumed by a consumer process.
 We
need buffer of items that can be filled by
producer and emptied by consumer.
 Unbounded-buffer places no practical limit on the size of the
buffer. Consumer may wait, producer never waits.
 Bounded-buffer assumes that there is a fixed buffer size.
Consumer waits for new item, producer waits if buffer is full.
 Producer
and Consumer must synchronize.
53
Producer
repeat
…
produce an item in nextp;
…
send(consumer, nextp);
until false;
 Consumer

repeat
receive(producer, nextc);
…
consume item from nextc;
…
until false;
54
 Shared
data
var n;
type item = ….;
var buffer: array[0..n-1] of item;
in, out: 0..n-1;
in =0; out= 0;
/* shared buffer = circular array */
/* Buffer empty if in == out */
/* Buffer full if (in+1) mod n == out */
/* noop means “do nothing” */
55
 Producer
process - creates filled buffers
repeat
…
produce an item in nextp
…
while (( in+1) mod n = = out)
do noop;
buffer[in] = nextp;
in= (in+1 )mod n;
until false;
56
 Consumer
process - Empties filled buffers
repeat
while (in = =out)
do noop;
nextc = buffer[out] ;
out= out+1 mod n;
…
consume the next item in nextc
…
until false
57