Chap 3

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 65

Chapter 3

Process Description and Control

Spring 2020 © 2020 by Greg Ozbirn, UTD, for 1


use with Stalling's 9th Ed. OS book
Processes
• When a program is executed, a process is created.
• More than one process can reside in memory at the same
time.
• The CPU may switch between processes as they run.

2
3
Process Execution
Points of View

• Each process has a list of program instructions to be


executed.
• The processor executes instructions from one of the active
processes.
• From the process’ viewpoint, the CPU occasionally
executes its code.
• From the CPU’s viewpoint, it executes some code of one
process, then some code of the next process, etc.

4
Process Trace

5
6
Two-state Process Model
• Two states for a process:
– Running
– Not running

7
Process Queue

Queue of not-running
processes.

8
Process Creation
• When a new process is created, the OS builds the data
structures that are used to manage it, and allocates space in
main memory for it.

9
Process Creation
Four primary reasons a process is created:
1. New batch job submitted.
2. Interactive Logon occurred.
3. OS creates one for some service (like printing).
4. Spawned by an existing process.

10
Spawning a Process
• When one process creates another, the process is said to
spawn the new process.
• A spawned process is said to be a child process, the
process that spawned it is said to be the parent process.

11
Process Termination
• Processes end for many reasons as given on the next slide.
• A batch job might execute a Halt instruction to end.
• An interactive session will end when the user logs off of
the system or closes the application.
• In some OS’s, a process might end when its parent process
ends, or when terminated by its parent.
• Of course, error and fault conditions can also cause a
process to end.
• The next slide lists some specific reasons for process
termination.

12
Process Termination Reasons
1. Normal completion
2. Time limit exceeded
3. Memory unavailable
4. Bounds violation (referenced memory outside its permitted range)
5. Protection error (e.g., write to read-only file)
6. Arithmetic error (e.g., divide by zero)
7. Time overrun (waited event didn’t occur in time)
8. I/O failure (file not found, failure to R/W)
9. Invalid instruction
10. Privileged instruction (reserved for the O.S.)
11. Data misuse (wrong type data)
12. Operating system intervention (example: when deadlock occurs)
13. Parent terminates so child processes terminate (on some systems)
14. Termination at the parent request 13
Five-State Model
• The two-state model uses one queue for all not-running
processes, but this is inadequate when some not-running
processes are ready to run while others are blocked and
waiting on some event, such as I/O completion.
• The two-state model would require the dispatcher to search
the not-running queue, looking for a process that is not
blocked.

14
Five-State Model
A better solution splits the not-running state into two states:
1. Ready: process is ready to execute.
2. Blocked: process is waiting for an event.

15
Five-State Model
• Besides splitting the not-running state into two states, two
more states are added for processes that are new and
processes that are exiting.

• This leads to a five-state model:

– Running: process is executing


– Ready: process is ready to execute
– Blocked: process is waiting for an event
– New: process created but not yet loaded
– Exit: process is exiting
16
17
New and Exit States
New:
• A program in the new state has just been defined. It is not
loaded into memory yet.
• Some systems limit the number of processes in the system.
Exit:
• When a program terminates it is no longer eligible to
execute.
• The data structures for a terminated process remain long
enough for support and accounting programs to extract
information.
• Afterwards, the process is deleted from the system.

18
State Changes
• Null->New: process creation.
• New->Ready: when OS is ready for a new process.
• Ready->Running: based on scheduling algorithm.
• Running->Exit: on termination.
• Running->Ready: time-out, preemption, yield.
• Running->Blocked: service call, I/O request.
• Blocked->Ready: waited event occurs.
• Ready->Exit: terminated due to parent.
• Blocked->Exit: same as Ready->Exit.

19
20
Queue Movements
• A new process is moved to the ready queue.
• A process is picked to run from the ready queue.
• A running process moves either to the blocked queue or
ready queue, or terminates.
• When a blocking event occurs, all processes that are
blocked and waiting on the event are moved to the ready
queue. In large systems, there might be separate event
queues to avoid searching the blocked queue.
• There also may be separate ready queues for different
process priorities.

21
Queues

22
23
Suspended Processes
• Even with multiprogramming of processes in memory, a
CPU could go idle, simply because I/O’s take so much
time compared to the CPU’s speed.
• But adding more main memory is expensive.
• A better solution is swapping processes from main
memory to disk.
• We therefore need a new state, which we will call a
suspend state.
• Notice that swapping is itself an I/O activity, but is usually
very fast compared to other I/O activity.

24
One Suspend State

25
Suspended Processes
• Actually two suspend states are needed:
– Blocked/Suspend: suspended and awaiting an event.
– Ready/Suspend: suspended and ready to run.

26
Two Suspend States
Dashed lines are possible but
not required transitions.

27
New Suspend Transitions
• Blocked -> Blocked/Suspend: to free memory.
• Blocked/Suspend -> Ready/Suspend: if event occurs
while Blocked/Suspend.
• Ready/Suspend -> Ready: if no ready processes, or if
suspended process is higher priority than ready queue
processes.
• Ready -> Ready/Suspend: if need to free up memory and
there are no blocked processes, or a ready process is of
lower priority than the blocked processes.

28
Other Transistions
• New->Ready/Suspend and New->Ready: could add new process to
either queue. Might add to the Ready/Suspend queue if no room in
memory or if desired to let OS setup the new process before loading it.

• Blocked/Suspend -> Blocked: if suspended blocked process has


higher priority than any suspended ready process.

• Running -> Ready/Suspend: rather than returning to the ready queue,


could be sent to ready/suspend to free up memory for some higher
priority process.

• Various -> Exit: if terminates due to normal completion or error, or by


parent, or on some systems if parent terminates.

29
Suspend State
The suspend state may be used for other occasions besides
swapping a process out of memory.

In general, a suspended process has these characteristics:


• It is not immediately available for execution.
• It may or may not be waiting on an event. (Blocking is not
the same as suspending).
• The process was suspended by an agent (itself, a parent,
the OS).
• The process stays suspended until the agent activates it.

30
Reasons for Suspension
• Swapping: for freeing main memory.
• OS Reason: if OS thinks process is causing a problem (for
example, deadlock).
• User request: to debug, the user may suspend execution,
examine its activity, and resume execution.
• Timing: if infrequently run (utility or accounting
program), operator may suspend it between runs.
• Parent request: to examine a child process or its activity.

31
Process Description
• The operating system controls events within the computer.
• It does so by managing processes and resources.
• The next slide illustrates processes and resources in a
computer system.

32
Processes and Resources

P1 running, P2 blocked, Pn suspended

33
OS Control Structures
The OS maintains tables about each entity it is managing.
4 categories of information:
• Memory tables
• I/O tables
• File tables
• Process tables

34
Memory Tables

Memory tables keep track of:


• Allocation of main memory to processes.
• Allocation of secondary memory to processes.
• Protection attributes of blocks of main or virtual memory
(for shared blocks for example).
• Information needed to manage virtual memory.

35
I/O Tables
• I/O tables are used to manage the I/O devices.
• Keeps track of such things as:
– availability of a device
– device assignment to a process
– device status
– operation status
– memory being used for a data transfer

36
File Tables
• File tables keep track of information about files such as:
– their location in secondary storage
– their status
– other file attributes.
• Some OS’s manage this information, others use a separate
File Management System to manage it.

37
Process Tables
• The process tables may link to or cross-reference the other
tables.
• Process tables must keep track of where the process is
located, and the attributes of the process.

38
39
Process Control Structures
• The OS must manage processes.
• In order to do this, it must maintain information about each
process.
• This information is maintained in a data structure called a
Process Control Block (PCB).

40
Process Image
• When a program is loaded into memory, it is not just the
executable code that is loaded.
• The program will also need memory to store its data and
its stack.
• The program will also be associated with a PCB.
• All of these together are referred to as the Process Image:
– User Program: program to execute
– User Data: data for the program
– System Stack: for parameter passing
– Process Control Block (PCB): process attributes

41
Process Attributes
Process Control Block information has three main parts:
• Process Identification
• Processor State Information
• Process Control Information

42
Process Identification
Numeric IDs for:
• The process
• The parent of the process
• The user of the process

• The process ID may be used as the means of cross-


referencing other tables.
• It might simply be an index into the process table.

43
Processor State Information
Processor state includes the following:
• User-visible Registers
• Control and Status Registers (PC, Condition Codes, Status
information, PSW)
• Stack Pointers

44
45
Process Control Information
• Scheduling and State Information:
– Process state: ready, running, waiting, …
– Priority: high, low, numeric value, etc.
– Scheduling-related information: for example, time
process has been waiting
– Event: ID of event being waited for.
• Data Structuring: pointers to other processes, such as
parent-child, queuing, or other structure.
• Interprocess Communication: flags, signals, messages
• Process Privileges: for resources or memory
• Memory Mgmt: for segmented or virtual memory
• Resources Ownership and Utilization: resources currently
owned or history information for the scheduler.
46
For code or data
shared between
Process images processes
47
Queues implemented
as linked lists of PCBs.

48
Role of PCB
• PCB is most important data structure in the OS.
• It contains all of the information the OS needs to know
about the process.
• It is used by almost every module in the OS.
• The set of PCBs defines the state of the OS.
• This means the PCBs must be handled carefully, since if
any routine corrupts them then all is lost.
• Likewise, any change of the structure of the PCB would
affect a large number of modules.
• Therefore, all routines are required to use a PCB handler
routine that is responsible for all PCB access.

49
Process Control
Most OS’s support at least two modes of execution:
• Kernel mode: privileged mode having complete control of
system, can execute privileged instructions, all registers,
and all memory. (also called system mode or control
mode).
• User mode: less privileged mode for running user
programs.

Having these two modes keeps user programs from


interferring with system functions.

50
Changing Modes
• The PSW has a mode bit to indicate the current mode.
• Upon a system call or interrupt, the processor switches to
kernel mode and the bit is set.
• For example:
– The Intel Itanium 64-bit processor has a 2 bit cpl field,
representing four privilege levels.
– When an interrupt occurs, the processor clears the field,
making the mode the most privileged level.
– When the interrupt ends with an “irt” instruction, the
cpl field is restored to the level of the interrupted
program.

51
Process Creation
• Creating a new process is a relatively lengthy operation.
• The steps involved are:
1. Assign an ID and add to process table.
2. Allocate space for all parts of the process image.
3. Initialize the PCB.
4. Set appropriate linkages (queues or links to other
processes).
5. Create other data structures (accounting, billing,
performance, etc.)

52
Process Switching
• A process switch may occur anytime the OS gains control
again from the current process.
• This might happen for:
– an interrupt: if time expired, I/O complete, memory
fault (page not in memory)
– a trap: if current instruction creates an error
– a supervisor call: such as an I/O request

53
Switching
Two kinds of switching:
• Mode switch: switch away from process, for example,
to run an interrupt handler.
• Process switch: switch from one process to another
process.

54
Mode Switching
Mode switch:
• Save context (PC, registers, PSW, stack pointers)
• Set PC to interrupt handler
• Switch from user mode to kernel mode.
• Run interrupt handler

Upon return:
• Switch from kernel mode back to user mode
• Set PC to program
• Restore context

55
Process Switch
A process switch involves these actions:
• Save context.
• Update PCB of current process to a non-running state.
• Move PCB of process to appropriate queue for the state.
• Select another process to run (runs scheduler routine)
• Update PCB of the selected process to the running state.
• Update memory-management data structures.
• Restore state of newly selected process.

Note: more work than a mode switch.


56
Execution of the OS
• If the OS is just a program like any other, is it also
executed by a process?
• If so, then who controls it?
• The next few slides will look at a few general approaches
to answer this question.

57
Nonprocess Kernel
• This approach executes the kernel outside of any process.
• The OS has its own region of memory and its own stack.
• The OS is a separate entity which manages user processes.
• So, the idea of a process applies only to user programs.
• This approach was common on older operating systems.

58
Execution within User Processes
• OS software runs within context of a user process.
• Kernel’s program, data & stacks are added to the image of
the calling process.
• Process executes in privileged mode when executing the
part that has the operating system code.
• Key benefit that interrupts are handled within the same
process (mode switch instead of process switch).
• Unix SVR4 follows this model.

59
Process-Based OS
• OS supports two kinds of processes, user processes and
system processes.
• All processes can then be managed according to priority
with some OS functions being low priority and others
being high.
• Benefits also seen in multiprocessor environments where
OS processes may exploit parallelism.

60
61
62
63
Special Unix Processes
• Unix has two special processes that get created when the
system first boots.
• Process 0: the swapper process (the scheduler)
• Process 1: the init process

• All other processes in the system have process 1 as an


ancestor.

64
End of Slides

based on Stalling's official slides 65

You might also like