44
3.1/44 Part two: Process management Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory, files, and I/O devices to accomplish its task. These resources are allocated to the process either when it is created or while it is executing. All these processes may execute concurrently. The operating system is responsible for the following activities in connection with process and thread management: the creation and deletion of both user and system processes; the scheduling of processes; and the provision of mechanisms for synchronization, communication, and deadlock handling for processes. P79

3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

Embed Size (px)

Citation preview

Page 1: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.1/44

Part two: Process managementPart two: Process management

A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory, files, and I/O devices to accomplish its task. These resources are allocated to the process either when it is created or while it is executing.

All these processes may execute concurrently.

The operating system is responsible for the following activities in connection with process and thread management: the creation and deletion of both user and system processes; the scheduling of processes; and the provision of mechanisms for synchronization, communication, and deadlock handling for processes.

P79

Page 2: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

Chapter 3: ProcessesChapter 3: Processes

Page 3: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.3/44

Chapter 3: ProcessesChapter 3: Processes

Process Concept

Process Scheduling

Operations on Processes

Cooperating Processes

Interprocess Communication

Page 4: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.4/44

ObjectivesObjectives

To introduce the notion of a process - a program in execution, which forms the basis of all computation.

To describe the various features of processes, including scheduling, creation and termination, and communication.

To describe communication in client-sewer systems.

Page 5: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.5/44

Process ConceptProcess Concept

A question that arises in discussing operating systems involves what to call all the CPU activities.

在多道程序环境中,程序的执行是断断续续的,具备动态特征,程序和程序的执行不再一一对应,有必要引入一个新的概念

进程的定义:可并发执行的程序在一个数据集合上的运行过程。

P81

Page 6: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.6/44

Process ConceptProcess Concept

a process is a program in execution. A process is more than the program code, which is

sometimes known as the text section. It also includes the current activity, as represented

by the value of the program counter and the contents of the processor's registers.

A process generally also includes the process stack, which contains temporary data (such as function parameters, return addresses, and local variables), and a data section, which contains global variables. A process may also include a heap, which is memory that is dynamically allocated during process run time.

Page 7: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.7/44

Process in MemoryProcess in Memory

Page 8: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.8/44

Program vs ProcessProgram vs Process

a program by itself is not a process; a program is a passive entity, such as a file containing a list of instructions stored on disk (often called an executable file), whereas a process is an active entity, with a program counter specifying the next instruction to execute and a set of associated resources.

A program becomes a process when an executable file is loaded into memory.

Although two processes may be associated with the same program, they are nevertheless considered two separate execution sequences.

Page 9: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.9/44

Process StateProcess State

As a process executes, it changes state. The state of a process is defined in part by the current activity of that process. Each process may be in one of the following states:

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 execution

only one process can be running on any processor at any instant. Many processes may be ready and waiting.

P83

Page 10: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.10/44

Diagram of Process StateDiagram of Process State

Page 11: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.11/44

Process Control Block (PCB)Process Control Block (PCB)

Each process is represented in the operating system by a process control block

It contains many pieces of information associated with a specific process, including these:

Process state

Program counter

CPU registers

CPU scheduling information

Memory-management information

Accounting information

I/O status information

P84

Page 12: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.12/44

Process Control Block (PCB)Process Control Block (PCB)

PCB的作用 : 反映进程的动态特征,描述进程的执行情况,使操作系统能控制进程的运行。

Page 13: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.13/44

CPU Switch From Process to ProcessCPU Switch From Process to Process

Page 14: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.14/44

Characters of processCharacters of process

动态性:进程有一定的生命周期,它由创建而产生,由调度而执行,因得不到资源而暂停执行,由撤消而消亡。

并发性:多个进程实体,同存于内存中,能在一段时间内同时运行。

独立性:进程是独立运行的基本单位,是系统中独立获得资源和独立调度的基本单位。

异步性:进程按各自独立的、不可预知的速度向前推进。

结构特征:进程 = 程序 + 数据 + 进程控制块

Page 15: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.15/44

Process SchedulingProcess Scheduling

The objective of multiprogramming is to have some process running at all times, to maximize CPU utilization.

The objective of time sharing is to switch the CPU among processes so frequently that users can interact with each program while it is running.

To meet these objectives, the process scheduler selects an available process (possibly from a set of several available processes) for program execution on the CPU.

For a single-processor system, there will never be more than one running process. If there are more processes, the rest will have to wait until the CPU is free and can be rescheduled.

P85

Page 16: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.16/44

Process Scheduling QueuesProcess Scheduling Queues

Job queue – set of all processes in the system

Only one

Ready queue – set of all processes residing in main memory, ready and waiting to execute

Only one

Device queues – set of processes waiting for an I/O device

Each device has its own device queue

Processes migrate among the various queues

Page 17: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.17/44

Ready Queue And Various I/O Device QueuesReady Queue And Various I/O Device Queues

Page 18: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.18/44

Representation of Process SchedulingRepresentation of Process Scheduling

P88

Page 19: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.19/44

SchedulersSchedulers

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

Medium-term scheduler -- to remove processes from memory (and from active contention for the CPU) and thus the degree of multiprogramming. Later, the process can be reintroduced into memory, and its execution can be continued where it left off.

P88

Page 20: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.20/44

Addition of Medium Term SchedulingAddition of Medium Term Scheduling

Page 21: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.21/44

Schedulers Schedulers

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

Page 22: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.22/44

Schedulers Schedulers

It is important that the long-term scheduler select a good process mix of I/O-bound and CPU-bound processes.

If all processes are I/O bound, the ready queue will almost always be empty, and the short-term scheduler will have little to do. If all processes are CPU bound, the I/O waiting queue will almost always be empty, devices will go unused, and again the system will be unbalanced.

The system with the best performance will thus have a combination of CPU-bound and I/O-bound processes.

Page 23: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.23/44

long-term schedulerlong-term scheduler

the long-term scheduler may be absent or minimal.

time-sharing systems such as UNIX and Microsoft Windows systems often have no long-term scheduler but simply put every new process in memory for the short-term scheduler.

The stability of these systems depends either on a physical limitation (such as the number of available terminals) or on the self-adjusting nature of human users. If the performance declines to unacceptable levels on a multiuser system, some users will simply quit.

Page 24: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.24/44

Context SwitchContext Switch

interrupts cause the operating system to change a CPU from its current task and to run a kernel routine.

When an interrupt occurs, the system needs to save the current context of the process currently running on the CPU so that it can restore that context when its processing is done, essentially suspending the process and then resuming it.

The context is represented in the PCB of the process; it includes the value of the CPU registers, the process state, and memory-management information.

Generically, we perform a state save of the current state of the CPU, be it in kernel or user mode, and then a state restore to resume operations.

P89

Page 25: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.25/44

Context SwitchContext Switch

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

Context-switch time is pure overhead; the system does no useful work while switching

Time dependent on hardware support

the memory speed, the number of registers that must be copied, and the existence of special instructions (such as a single instruction to load or store all registers).

Page 26: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.26/44

Process CreationProcess Creation

Parent process create children processes, which, in turn create other processes, forming a tree of processes

Page 27: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.27/44

Process CreationProcess Creation

Resource sharing

Sub-process may be able to obtain its resources directly from the operating system,

or it may be constrained to a subset of the resources of the parent process.

The parent may have to partition its resources among its children, or it may be able to share some resources (such as memory or files) among several of its children.

Restricting a child process to a subset of the parent's resources prevents any process from overloading the system by creating too many sub-processes.

P90

Page 28: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.28/44

Process CreationProcess Creation

execution

The parent continues to execute concurrently with its children.

The parent waits until some or all of its children have terminated.

address space

The child process is a duplicate of the parent process (it has the same program and data as the parent).

The child process has a new program loaded into it.

Page 29: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.29/44

Process Creation Process Creation

Address space

Child duplicate of parent

Child has a program loaded into it

UNIX examples

fork system call creates new process

exec system call used after a fork to replace the process’ memory space with a new program

Page 30: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.30/44

C Program Forking Separate ProcessC Program Forking Separate Processint main(){pid_t pid;

/* fork another process */pid = fork();if (pid < 0) { /* error occurred */

fprintf(stderr, "Fork Failed");exit(-1);

}else if (pid == 0) { /* child process */

execlp("/bin/ls", "ls", NULL);}else { /* parent process */

/* parent will wait for the child to complete */wait (NULL);printf ("Child Complete");exit(0);

}}

Page 31: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.31/44

Process TerminationProcess Termination

Process executes last statement and asks the operating system to delete it (exit)

Output data from child to parent (via wait)

Process’ resources are deallocated by operating system

Parent may terminate execution of children processes (abort)

Child has exceeded allocated resources

Task assigned to child is no longer required

If parent is exiting

Some operating system do not allow child to continue if its parent terminates

– All children terminated - cascading termination

Users could arbitrarily kill some jobs (kill).P95

Page 32: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.32/44

Cooperating ProcessesCooperating 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

Information sharing

Computation speed-up

Modularity

Convenience

P96

Page 33: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.33/44

models of inter-process communicationmodels of inter-process communication

shared memory

a region of memory that is shared by cooperating processes is established. Processes can then exchange information by reading and writing data to the shared region.

message passing

communication takes place by means of messages exchanged between the cooperating processes.

Page 34: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.34/44

models of inter-process communicationmodels of inter-process communication

Both of the models just discussed are common in operating systems, and many systems implement both.

Message passing is useful for exchanging smaller amounts of data, because no conflicts need be avoided.

Message passing is also easier to implement than is shared memory for inter-computer communication.

Shared memory allows maximum speed and convenience of communication, as it can be done at memory speeds when within a computer.

Shared memory is faster than message passing.

Page 35: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.35/44

Communications Models Communications Models

Figure 3.13 Communications models. (a) Message passing. (b) Shared memory.

Page 36: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.36/44

Inter-process Communication (IPC)Inter-process Communication (IPC) 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)P99

Page 37: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.37/44

Implementation QuestionsImplementation 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?

Page 38: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.38/44

Direct CommunicationDirect 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

P100

Page 39: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.39/44

Indirect CommunicationIndirect Communication

Messages are directed and received from mailboxes (also referred to as 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

Link may be unidirectional or bi-directionalP101

Page 40: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.40/44

Indirect CommunicationIndirect Communication

Operations

create a new mailbox

send and receive messages through mailbox

destroy a mailbox

Primitives are defined as:

send(A, message) – send a message to mailbox A

receive(A, message) – receive a message from mailbox A

Page 41: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.41/44

Indirect CommunicationIndirect Communication

Mailbox sharing

P1, P2, and P3 share mailbox A

P1, sends; P2 and P3 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.

Page 42: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.42/44

SynchronizationSynchronization

Message passing may be either blocking or non-blocking

Blocking is considered synchronous

Blocking send has the sender block until the message is received

Blocking receive has the receiver block until a message is available

Non-blocking is considered asynchronous

Non-blocking send has the sender send the message and continue

Non-blocking receive has the receiver receive a valid message or null

P102

Page 43: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

3.43/44

BufferingBuffering

Queue of messages attached to the link; implemented in one of three ways

1.Zero capacity – 0 messagesSender must wait for receiver (rendezvous)

2.Bounded capacity – finite length of n messagesSender must wait if link full

3.Unbounded capacity – infinite length Sender never waits

P102

Page 44: 3.1/44 Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory,

Homework: 1Homework: 1 、、 22

End of Chapter 3End of Chapter 3