Transcript Chapter4

Chapter 4
Process Management
What is a Process


A primary goals of a computer system is to perform work
for the system’s users.
Process is a program in execution


It is a unit of work or an activity
A program stored on disk is NOT a process



A program is a Passive entity
Becomes a process when it is loaded into memory and begins
execution
A PROCESS has:






A State
Text Section
Program counter
Context
Multiple processes can be associated with the same
program– but each process represents a single sequence
of actions.
In Multiprogramming:


Multiple processes are executing
Interleaved execution






To do work each process must execute a program.
In some systems a process may execute a series
of different programs ( steps of a compiler)
Of a program might consists of several distinct
processes, each with a separate sequence of
activities that continue at the same time.
A process is not a data object, but is represented
by a data object, usually called a PCB.
Throughout it’s lifetime a process will use
resources, minimally the CPU and memory, but
sometimes others: files, shared I/O devices.. Etc.
If a shared resource is not available, the process
must wait. A process alternates between periods
of running and waiting, thus a process is
considered to move through a series of distinct
states.
Process Types

Batch:
 Are processes which perform a job submitted
by a user as a whole.
 Batch processes are usually submitted with
good estimates of their resource needs, and
possibly with instructions which explicitly
determine importance and scheduling
requirements .
 These execute with no further interaction with
the user.
Job is executed without interaction
Results

Interactive
 Interactive processes are assumed to perform
work on behalf of users who remain present at
an interactive terminal.
 Usually these processes have short execution
time, and resource needs.
 But display information about progress, and
may interact with the user during execution.
 Interactive processes are rarely initiated with
information about resource use.
 The user requires rapid and consistent
response throughout the processes execution.
Process 1
Process 2
Process 3
•Real time:
•monitor and control external events and must
meet rigid timing constraints.
•designed to interact with external events and
objects such as tracing air traffic, or monitoring
the status of heat sensors.
•resource requirements of real-time processes
are generally known.

Many operating systems are
specialized to recognize only one
type of process:



IBM’s z/OS is primarily batch oriented
Linux is designed for interactive
Some OS’s attempt to support two
or more types of processes, but
usually one type is always
dominant, and the majority of
resources and scheduling effort is
applied to that type of process first.
The PCB – Process Control Block




Process name, name by which the
process may be identified by users or by
other processes.
Process state, a code representing the
current state for this process, or a link to
a queue that represents that state.
Process context (CPU state), a storage
area for register contents and other
information that must be saved when the
process is not running.
Memory use, information about the
memory allocated to this process and the
current location of its program code and
data.




Resource use, information about other
resources currently in use by this process,
and any restrictions or quotas on future
resource use.
Process priority, a value or set of values
to help determine the relative priority to
be given to this process.
Relationships, information about
relationships that may exist between this
process and other processes, such as
those it has created.
Accounting information, records of
time and other resources that have been
used by this process, and who owns the
process, to be used for billing or analysis.
Process 1
Process 2
Process 3
Process 4
Process 5
empty
Fixed Array
of PCB’s
New Process
#6
Linked list of PCB’s
Process 1
Process 2
Process 3
Organization of processes
Process 4


To perform work, a process must be able
use certain resources that must be shared
with other processes.
Physical resources:



represent the permanent components of the
computer such as processor, memory,
secondary storage space, and I/O devices and
are usually controlled by the operating system.
By their nature, physical resources are
reusable.
 They are not destroyed when used, and thus
may be used repeatedly.
 In most cases, a resource can not be used by
two processes for separate purposes.
Reusable resources that can be used by only
one process at a time are called serially
reusable.
Resource Needs

Logical resources:



represent resources formed by
information structures stored within
files or main memory.
These have a shorter lifetime than
physical resources
Some logical resources are
consumable. The are created by one
process and destroyed after use by
another. IE: the blocks of data in a
message.
Resource Allocation

A responsibility of the OS is the control
and allocation of resources.



The OS must keep track of the status of each
resource.
Keep track of processes waiting for a resource
that is currently in the possession on another
process.
 In the form of a queue
 A process enters the queue by linking its PCB
to the queue in some manner.
Have a strategy for deciding which process to
choose when allocating an available resource.
Process State
•Represents the current activity in which the process is
engaged
•An active process is either running or waiting (minimally
for the processor)
•Can be represented in the pcb, or by linking the pcb to a state
queue
PROCESS GAINS
CONTROL OF CPU
RUNNING
WAITING
PROCESS WAITS FOR SOME
RESOURCE OR EVENT
Simplest model of process states
Time quantum expires
READY
CPU assigned
RUNNING
preemption
Waiting for event or
resource
BLOCKED
Basic Process State Diagram
Fig 4-3, page 10
Event occurs or
resource available
INITIAL
New process is ready to run
Time quantum expires
READY
Process swapped
back into memory
CPU assigned
RUNNING
Terminate
when
complete
Event occurs or
resource available
Waiting for
event or
resource
SUSPENDED
READY
Event occurs or
resource available
BLOCKED
Waiting for long term
event
TERMINAL
Abort due to error
Fig 4-4, state diagram with additional states
SUSPENDED
BLOCKED



Processes transition through many
states throughout their lifetime.
The objective of multiprogramming
is to have some process running at
all times, so as to maximize CPU
utilization
Each process will go through many
cycles of alternately running and
waiting.
P1
-----##########===========----#####
====
Ready
-------Running
P1
=====------###################
P1
===========----------##############
OS
===--=====---======---===---==---------
Two issues 1) Idle time, 2) OS overhead
######
Blocked
Scheduling






Throughout it’s lifetime a process spends time
“waiting”, this waiting is done in some scheduling
queue.
Processes waiting to be admitted into the system, are
in a “job” queue.
Processes waiting for the CPU are in the ready queue.
Processes waiting for a device are in a device queue.
A Scheduler determines which process is given the
resource for which it is waiting, and the selection
process is “scheduling”.
When a process is “paused” the need to preserve the
information stored in registers, and status flags while
the processor is in use by the OS or other processes.
This information is called the “context” of the process.
Context Switching
Process1
PCB
process 1
Registers
Memor
y
Process2
PCB
process 2
Operations on processes




In early multi programming OS such as T.H.E. and CDC
Scope processes were NOT dynamic entities.
These systems provided a fixed number of permanent
processes.
Number of processes were fixed at ONE per user.
Static Process Management
Process 1
Process2
New Process
Process3



Operating systems which employ Dynamic
process management allow processes to be
created and destroyed as needed during the
operation of the system!
Minimally, there is one process for each job
admitted into the system. The number of
processes change only as the number of
simultaneous users change.
In newer OS’s (unix), it is possible for one
process to explicitly create another through the
use of system calls. An individual user may
have many processes.

Each interactive user has a logon-session
process, this in turn creates new processes to
execute commands, like running a program or
searching a directory.
The ability of processes to create new ones creates a
process hierarchy.
P1
Child of P1 &
sibling of P3
P2
Parent of P2, & P3
P3
Child of P1, Parent of
P4 & sibling of P2
P4
Child of P3
The creating process is called the parent process, and the
new processes the child of its creator. Children of the
same parent are siblings and other related terms such as
grandparents, ancestor, and descendant may be used
to describe this “tree” of related processes. Ancestor
processes have the ability to control, delete, and change
child processes
Operations needed for dynamic
Process creation

Create or allocate a PCB:


Allocate initial memory space for the
program and data:


A new PCB structure is either allocated from an
existing pool of PCB’s or dynamically created.
Memory is allocated to hold the program
code and data.
Identify and (usually) run the
program to be run:

The program to execute is loaded by the
relocating loader.
Process creation


Assign initial attributes and resource
limits to the process.
Allocate initial resources to the
process (if any).


A child may be allocated resources
directly from the operating system or may
be constrained to a subset of the parent’s
resources.
Establish the starting state for the
process and setup or complete the
PCB

When a process creates a new process,
two possibilities exist in terms of
execution:



The parent process continues to execute
concurrently with its child
The parent process waits until some or all of its
children have terminated.
There are also two possibilities in terms of
the address space of the new process:


The child process is a duplicate of the parent
process
The child process has a program loaded into it.




This can be illustrated using Unix.
A process has a text, data, and
stack segment, which must be in
memory. The text segment contains
the program to be executed.
A process also has a system data
area that contains information
needed by the OS as the process
executes, including storage for
return addresses.
A program’s text segment is
reentrant, and can be shared by
multiple processes.
Real Memory
Text Segment
A&B
Data Segment
Data Segment
Process A
Process B
Stack Segment
Stack Segment
Process A
Process B


If a process that is executing is a
command shell, and receives a command
such as that to execute an application
program, the shell will respond by
creating another process by calling the
“fork” command.
In response, UNIX duplicates the
executing process, creating two identical
copies, both copies initially execute the
same program, share open files and other
key data. The two processes are
distinguished by giving them different
return codes.
#include <stdio.h>
int main (int argc, char *argv[])
{
int pid;
/*fork another process */
printf(“In the parent program forking a
process\n”);
pid = fork();
if (pid <0) {/* the fork failed */
printf("the fork failed\n");
exit(-1);
}
else if(pid == 0) {/*the child process */
printf("the child \n");
execlp("/bin/ps",“ps", NULL);
}
else {/* the parent */
printf("the parent \n");
wait(NULL);
printf("child complete \n");
}
printf("main is done\n");
return 0;
}
Parent
Process
Parent Process
Child Process
#include <stdio.h>
#include <stdio.h>
int main (int argc, char *argv[])
{
int main (int argc, char *argv[])
{
int pid;
int pid;
/*fork another process */
printf(“In the parent program
forking a process\n”);
pid = fork();
/*fork another process */
printf(“In the parent program
forking a process\n”);
pid = fork();
if (pid <0) {/* the fork failed */
printf("the fork failed\n");
exit(-1);
}
else if(pid == 0) {/*the child process
*/
printf("the child \n");
execlp("/bin/ps",“ps", NULL);
}
else {/* the parent */
printf("the parent \n");
wait(NULL);
printf("child complete \n");
}
printf("main is done\n");
return 0;
}
if (pid <0) {/* the fork failed */
printf("the fork failed\n");
exit(-1);
}
else if(pid == 0) {/*the child process
*/
printf("the child \n");
execlp("/bin/ps",“ps", NULL);
}
else {/* the parent */
printf("the parent \n");
wait(NULL);
printf("child complete \n");
}
printf("main is done\n");
return 0;
}
Main
program
/home/hayhurst- a.out
in the parent program forking a process
Child Process
Parent Process
the child
the parent
PID TTY
TIME CMD
23373 pts/3 00:00:00 tcsh
23842 pts/3 00:00:00 emacs
23881 pts/3 00:00:00 a.out
23882 pts/3 00:00:00 ps
child complete
main is done
Process Destruction

The destruction of a process may be invoked by a
“destroy_process” system call either by the
process itself or by another process. The need to
terminate a process can occur for many reasons:






The process itself has requested normal termination
A nonrecoverable error has occurred in the process
execution
An operator command has terminated the process
A parent process has terminated (either normally
or abnormally)
An authorized process has requested termination.
Destruction involves the following steps:




Remove PCB from its current state queue, and other
queues it may be linked to.
Destroy or reassign child processes
Free allocated memory and other resources
Free the PCB