lecture5

63
1 Process Description and Control

Upload: ali-shah

Post on 18-Jul-2015

68 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: Lecture5

1

Process Description and Control

Page 2: Lecture5

2

Requirements of an Operating System

• Interleave the execution of multiple processes to maximize processor utilization while providing reasonable response time

• Allocate resources to processes

• Support inter process communication and user creation of processes

Page 3: Lecture5

3

Manage Execution of Applications

• Resources made available to multiple applications

• Processor is switched among multiple applications

• The processor and I/O devices can be used efficiently

Page 4: Lecture5

4

Process

• A program in execution– Sometimes also called a task

• 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 instructions

Page 5: Lecture5

5

What is a process?

• Components of a process include

– the program to be executed (i.e. code)– data on which the program will execute– Links to resources used by the program such as

memory and file(s)– information on the process’ execution status

• Is a process the same as a program?

Page 6: Lecture5

6

Processes vs. Programs

• No!, it is both more and less.

• more—a program is just part of a process’ context.

• A single program can be executed by two different users – i.e. the same program can be part of >1 process

• less—a program may actually create several processes.– e.g. Compilation in Unix

Page 7: Lecture5

7

Programming: uni- vs multi-

• Some systems allow the execution of only one process at a time (e.g. early PC's). These are called uniprogramming systems.

• Others allow more than one process to share the CPU over time: multi-programming systems

• NOT multiprocessor systems!– Still only 1 CPU

• In a multiprogramming system, the OS switches the CPU from process to process running each for some “time quantum”

• The CPU is actually running one and only one process at a time.

Page 8: Lecture5

8

• Multitasking can be conveniently described in terms of multiple processes running in (pseudo) parallel

Page 9: Lecture5

9

Execution model

• Operating system designers have developed a model that makes multiple process software relatively easy to understand

• In this model, each runnable piece of software on the computer—even components of the operating system itself—is organized into one or more sequential processes

• Instructions are executed sequentially in each process• Each process can be visualized as a block of code

with a pointer identifying the next instruction to be executed.

Page 10: Lecture5

10

Sharing the CPU

• How can several processes share one CPU?

• The operating system takes care of this by making sure:– each process gets a chance to run—scheduling.– they do not modify each other’s state—protection.

• Note that these are two issues that come up over and over whenever we think about sharing resources…

Page 11: Lecture5

11

• The O/S has to multiplex resources to the processes

• a number of processes have been created• each process during the course of its execution

needs access to system resources: CPU, main memory, I/O devices

Page 12: Lecture5

12

• A process image consists of three components1.an executable program

2.the associated data needed by the program

3.the execution context of the process, which contains all information the O/S needs to manage the process (ID, state, CPU registers, stack, etc.)

Page 13: Lecture5

13

Page 14: Lecture5

14

Process Elements

• Identifier• State• Priority• Program counter• Memory pointers• Context data• I/O status information• Accounting information

Page 15: Lecture5

15

Process Control Block

• Contains the process elements

• Created and manage by the operating system

• Allows support for multiple processes

Page 16: Lecture5

16

The Process Control Block (PCB)• The Process Control Block

(PCB) Typical process image implementation

• is included in the context, along with the stack

• is a “snapshot” that contains all necessary and sufficient data to restart a process where it left off (ID, state, CPU registers, etc.)

• is one entry in the operating system’s process table (array or linked list)

Page 17: Lecture5

17

Process Control Block

Page 18: Lecture5

18

Trace of Process

• Sequence of instruction that execute for a process

• Dispatcher switches the processor from one process to another

• The dispatcher is a routine program in kernel memory space

Page 19: Lecture5

19

Example ExecutionMain MemoryAddress

Dispatcher

Process A

Process B

Process C

Program Counter0

100

5000

8000

8000

12000

Figure 3.2 Snapshot of Example Execution (Figure 3.4)at Instruction Cycle 13

Page 20: Lecture5

20

Trace of Processes

Page 21: Lecture5

21

Page 22: Lecture5

22

A Dispatcher

Page 23: Lecture5

23

Two-State Process Model

• Process may be in one of two states– Running

– Not-running

Page 24: Lecture5

24

Not-Running Process in a Queue• How does the O/S keep track of processes and

states? • By keeping a queue of pointers to the process

control blocks • The queue can be implemented as a linked list if

each PCB contains a pointer to the next PCB

Page 25: Lecture5

25

Process Creation• Some events that lead to process creation

• The system boots– when a system is initialized, several background

processes or “daemons” are started (email, logon, etc.)

• A user requests to run an application– by typing a command in the CLI shell or double-

clicking in the GUI shell, the user can launch a new process

Page 26: Lecture5

26

Process Creation

• An existing process spawns a child process

– for example, a server process (print, file) may create a new process for each request it handles

– The init daemon waits for user login and spawns a shell

• A batch system takes on the next job in line

Page 27: Lecture5

27

Process Hierarchies

• Parent creates a child process, child processes can create their own processes

• Forms a hierarchy– UNIX calls this a "process group"

• Windows has no concept of process hierarchy– all processes are created equal

Page 28: Lecture5

28

Process Creation

Page 29: Lecture5

29

Process Termination• Some events that lead to process termination (exit)• Regular completion, with or without error code

– The process voluntarily executes an exit(err) system call to indicate to the O/S that it has finished

• Fatal error (uncatchable or uncaught)– Service errors: no memory left for allocation, I/O error, etc.– Total time limit exceeded

– Arithmetic error, out-of-bounds memory access, etc.

• Killed by another process via the kernel– The process receives a SIGKILL signal– In some systems the parent takes down its children with it

Page 30: Lecture5

30

Process Pause / Dispatch

• Some events that lead to process pause / dispatch• I/O wait

– a process invokes an I/O system call that blocks waiting for the I/O device: the O/S puts the process in “Not Running” mode and dispatches another process to the CPU

• Preemptive timeout– the process receives a timer interrupt and relinquishes

control back to the O/S dispatcher: the O/S puts the process in “Not Running” mode and dispatches another process to the CPU

– Not to be confused with “total time limit exceeded”, which leads to process termination

Page 31: Lecture5

31

Problem with the two-state model

• Some “Not Running” processes are blocked (waiting for I/O, etc.)

• The O/S wastes time scanning the queue for ready →solution: divide “Not Running” into “Ready” and “Blocked”

Page 32: Lecture5

32

Process States• How does the O/S keep track of three process

states?

• Queuing diagram of a three-state Blocked/Ready process model

• by keeping an extra queue for blocked processes

Page 33: Lecture5

33

• To further reduce scanning, blocked processes can be placed in separate queues depending on the event type

Page 34: Lecture5

34

Representation of Process Scheduling

Page 35: Lecture5

35

Page 36: Lecture5

36

Page 37: Lecture5

37

Page 38: Lecture5

38

Process States

• Two independent concepts ×two values each – Whether a process is waiting on an event (is “Blocked”) or

not– Whether a process has been swapped out of main memory

(is “Suspended”) or not

• =Four combined states– “Ready”: the process is in memory and available for

execution– “Blocked”: the process is in main memory awaiting an event– “Suspended Blocked”: the process is in secondary memory

and awaiting an event– “Suspended Ready”: the process is in secondary memory but

is available for execution as soon as it is loaded into memory

Page 39: Lecture5

39

Process Suspension

• Note: Release of memory by swapping is not the only motivation for suspending processes.

• Various background processes may also be turned off and on, depending on CPU load, suspicion of a problem, some periodical timer or by user request.

Page 40: Lecture5

40

Operating System Control Structures

• The O/S has to multiplex resources to the processes

• To do this, the O/S must keep information about the current status of each process and resource

• Tables are constructed for each entity the operating system manages

Page 41: Lecture5

41

Memory Tables

• Allocation of main memory to processes

• Allocation of secondary memory to processes

• Protection attributes for access to shared memory regions

• Information needed to manage virtual memory

Page 42: Lecture5

42

I/O Tables

• I/O device is available or assigned

• Status of I/O operation

• Location in main memory being used as the source or destination of the I/O transfer

Page 43: Lecture5

43

File Tables

• Existence of files

• Location on secondary memory

• Current Status

• Attributes

• Sometimes this information is maintained by a file management system

Page 44: Lecture5

44

Process Table

• Where process is located

• Attributes in the process control block– Program– Data– Stack

Page 45: Lecture5

45

Memory

Devices

Files

Processes

Process 1

Memory Tables

ProcessImage

Process1

ProcessImage

Processn

I/O Tables

File Tables

Figure 3.11 General Structure of Operating System Control Tables

Primary Process Table

Process 2

Process 3

Process n

Page 46: Lecture5

46

Process Control Block

• Process identification– Identifiers

• Numeric identifiers that may be stored with the process control block include

– Identifier of this process

– Identifier of the process that created this process (parent process)

– User identifier

Page 47: Lecture5

47

Process Control Block

• Processor State Information– User-Visible Registers

• A user-visible register is one that may be referenced by means of the machine language that the processor executes while in user mode. Typically, there are from 8 to 32 of these registers, although some RISC implementations have over 100.

Page 48: Lecture5

48

Process Control Block

• Processor State Information– Control and Status Registers

These are a variety of processor registers that are employed to control the operation of the processor. These include

• Program counter: Contains the address of the next instruction to be fetched

• Condition codes: Result of the most recent arithmetic or logical operation (e.g., sign, zero, carry, equal, overflow)

• Status information: Includes interrupt enabled/disabled flags, execution mode

Page 49: Lecture5

49

Process Control Block

• Processor State Information– Stack Pointers

• Each process has one or more last-in-first-out (LIFO) system stacks associated with it. A stack is used to store parameters and calling addresses for procedure and system calls. The stack pointer points to the top of the stack.

Page 50: Lecture5

50

Process Control Block

• Process Control Information– Scheduling and State Information

This is information that is needed by the operating system to perform its scheduling function. Typical items of information:•Process state: defines the readiness of the process to be scheduled for execution (e.g., running, ready, waiting, halted).•Priority: One or more fields may be used to describe the scheduling priority of the process. In some systems, several values are required (e.g., default, current, highest-allowable)•Scheduling-related information: This will depend on the scheduling algorithm used. Examples are the amount of time that the process has been waiting and the amount of time that the process executed the last time it was running.•Event: Identity of event the process is awaiting before it can be resumed

Page 51: Lecture5

51

Process Control Block

• Process Control Information– Data Structuring

• A process may be linked to other process in a queue, ring, or some other structure. For example, all processes in a waiting state for a particular priority level may be linked in a queue. A process may exhibit a parent-child (creator-created) relationship with another process. The process control block may contain pointers to other processes to support these structures.

Page 52: Lecture5

52

Process Control Block

• Process Control Information– Interprocess Communication

• Various flags, signals, and messages may be associated with communication between two independent processes. Some or all of this information may be maintained in the process control block.

– Process Privileges• Processes are granted privileges in terms of the memory

that may be accessed and the types of instructions that may be executed. In addition, privileges may apply to the use of system utilities and services.

Page 53: Lecture5

53

Process Control Block

• Process Control Information– Memory Management

• This section may include pointers to segment and/or page tables that describe the virtual memory assigned to this process.

– Resource Ownership and Utilization• Resources controlled by the process may be indicated,

such as opened files. A history of utilization of the processor or other resources may also be included; this information may be needed by the scheduler.

Page 54: Lecture5

54

Page 55: Lecture5

55

Page 56: Lecture5

56

Page 57: Lecture5

57

Page 58: Lecture5

58

Page 59: Lecture5

59

Page 60: Lecture5

60

Page 61: Lecture5

61

Page 62: Lecture5

62

Execution of the Operating System

• Non-process Kernel– Execute kernel outside of any process– Operating system code is executed as a separate

entity that operates in privileged mode

• Execution Within User Processes– Operating system software within context of a user

process

– Process executes in privileged mode when executing operating system code

Page 63: Lecture5

63

UNIX Process States