0% found this document useful (0 votes)
35 views93 pages

Process Management in Operating Systems

Uploaded by

hgull8490
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views93 pages

Process Management in Operating Systems

Uploaded by

hgull8490
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Operating Systems:

Internals and Design Principles, 6/E


William Stallings

Chapter 3
Process Description and Control

Dave Bremer
Otago Polytechnic, N.Z.
©2008, Prentice Hall
Objectives
Roadmap
– How are processes represented and
controlled by the OS.
– Process states which characterize the
behaviour of processes.
– Data structures used to manage processes.
– Ways in which the OS uses these data
structures to control process execution.
– Discuss process management in UNIX
SVR4 Will skip it.
Requirements of an
Operating System
• Fundamental Task: Process Management
• The Operating System must
– Interleave the execution of multiple processes
– Allocate resources to processes, and protect
the resources of each process from other
processes,
– Enable processes to share and exchange
information,
– Enable synchronization among processes.
Concepts
• From earlier chapters we know that:
– Computer platforms consists of a collection of
hardware resources

– Computer applications are developed to


perform some task

– It is inefficient for applications to be written


directly for a given hardware platform
Concepts cont…
– OS provides an interface for applications to
use

– OS provides a representation of resources


that can be requested and accessed by
application
The OS Manages
Execution of Applications
• Resources are made available to multiple
applications
• The processor is switched among multiple
application
• The processor and I/O devices can be
used efficiently
What is a “process”?
• 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 instructions
Process Elements
• A process is comprised of:
– Program code (possibly shared)
– A set of data
– A number of attributes describing the state of
the process
Process Elements
• While the process is running it has a
number of elements including
– Identifier
– State
– Priority
– Program counter
– Memory pointers
– Context data
– I/O status information
– Accounting information
Process Control Block
• Contains the process
elements
• Created and manage by the
operating system
• Allows support for multiple
processes
• Process scheduling state –The state of the process in
terms of "ready", "suspended", etc., and other
scheduling information as well, such as priority value, the
amount of time elapsed since the process gained control
of the CPU or since it was suspended. Also, in case of a
suspended process, event identification data must be
recorded for the event the process is waiting for.
• Process structuring information – the process's children id's, or
the id's of other processes related to the current one in some
functional way, which may be represented as a queue, a ring or
other data structures
• Pointer – It is a stack pointer which is required to be saved when
the process is switched from one state to another to retain the
• current position of the process.
• Process number – Every process is assigned with a unique id
known as process ID or PID which stores the process identifier.
• Program counter – It stores the counter which contains the address of
the next instruction that is to be executed for the process.
• Register – These are the CPU registers which includes:
accumulator, base, registers and general-purpose registers.
• Memory Management Information – This field contains the
information about memory management system used by operating
system. This may include the page tables, segment tables etc.
• Open files list – This information includes the list of files opened
for a process.
• Inter process communication information – flags, signals and
messages associated with the communication among independent
processes
• Process State – new, ready, running, waiting, dead
Trace of the Process
• The behavior of an individual process is
shown by listing the sequence of
instructions that are executed
• This list is called a Trace
• Dispatcher is a small program which
switches the processor from one process
to another
Process Execution
• Consider three
processes being
executed
• All are in memory
(plus the dispatcher)
• Lets ignore virtual
memory for this.
Trace from the
processes point of view:
• Each process runs to completion
Trace from Processors
point of view

Timeout
I/O
Roadmap
– How are processes represented and
controlled by the OS.
– Process states which characterize the
behaviour of processes.
– Data structures used to manage processes.
– Ways in which the OS uses these data
structures to control process execution.
– Discuss process management in UNIX SVR4.
Two-State Process Model
• Process may be in one of two states
– Running
– Not-running
Queuing Diagram

processes moved by the dispatcher of the OS to the CPU then back to the
queue until the task is competed
Process Creation
• The OS builds a data structure to manage
the process
• Traditionally, the OS created all processes
– But it can be useful to let a running process
create another
• This action is called process spawning
– Parent Process is the original, creating,
process
– Child Process is the new process
Reasons of Process Creation
Process Termination
• There must be some way that a process
can indicate completion.
• This indication may be:
– A HALT instruction generating an interrupt alert
to the OS.
– A user action (e.g. log off, quitting an
application)
– A fault or error
– Parent process terminating
Reasons for Process Termination
Reasons for Process Termination(1)
Reasons for Process Termination(2)
Five-State
Process Model
Using Two Queues
Multiple Blocked Queues
Suspended Processes
• Processor is faster than I/O so all
processes could be waiting for I/O
– Swap these processes to disk to free up more
memory and use processor on more
processes
• Blocked state becomes suspend state
when swapped to disk
• Two new states
– Blocked/Suspend
– Ready/Suspend
Reason for Process
Suspension
Reason Comment
Swapping The OS needs to release sufficient main memory to
bring in a process that is ready to execute.
Other OS Reason OS suspects process of causing a problem.
Interactive User e.g. debugging or in connection with the use of a
Request resource.
Timing A process may be executed periodically (e.g., an
accounting or system monitoring process) and may
be suspended while waiting for the next time.
Parent Process A parent process may wish to suspend execution of
Request a descendent to examine or modify the suspended
process, or to coordinate the activity of various
descendants.

Table 3.3 Reasons for Process Suspension


Six-State Model (with One Suspend State)
Seven-State Model (with Two Suspend States)
Seven States Model
• Two suspend states allow all processes which are not
actually running to be swapped.
• Ready: The process is in main memory and available for
execution.
• Blocked: The process is in main memory and awaiting
an event.
• Blocked/Suspend: The process is in secondary
memory and awaiting an event.
• Ready/Suspend: The process is in secondary memory
but is available for execution as soon as it is loaded into
main memory.
Transitions
• Blocked  Blocked/Suspend:
– If no room for a new process and there is no ready process, make this
transition.
– This transition can be made even if there are ready processes available,
if the OS determines that the currently running process or a ready
process that it would like to dispatch requires more main memory to
maintain adequate performance.
• Blocked/Suspend  Ready/Suspend:
– A process in the Blocked/Suspend state is moved to the
Ready/Suspend state when the event for which it has been waiting
occurs.
• Ready/Suspend  Ready:
– When there are no ready processes in main memory, or if a suspended
process has a higher priority, the OS will need to bring one in to
continue execution.
Transitions
• Ready  Ready/Suspend:
– Normally, the OS would prefer to suspend a blocked process rather than
a ready one- WHY?
– However, it may be necessary to suspend a ready process if that is the
only way to free up a sufficiently large block of main memory.

• New  Ready/Suspend and New Ready:


– When a new process is created, it can either be added to the Ready
queue or the Ready/Suspend queue.
– In either case, the OS must create a process control block and allocate
an address space to the process.
– When there would often be insufficient room in main memory for a new
process; hence the use of the (New  Ready/Suspend) transition.
Transitions
• Blocked/Suspend  Blocked:
– Inclusion of this transition may seem to be poor design.
– After all, if a process is not ready to execute and is not already in
main memory, what is the point of bringing it in?
– For the rational of this transition consider the following scenario:
• A process terminates, freeing up some main memory.
• There is a process in the (Blocked/Suspend) queue with a
higher priority than any of the processes in the (Ready/Suspend)
queue and
• the OS has reason to believe that the blocking event for that
process will occur soon.
• Under these circumstances, it would seem reasonable to bring
a blocked process into main memory in preference to a ready
process.
Transitions
• Running  Ready/Suspend:
– Normally, a running process is moved to the Ready state when
its time allocation expires.
– If, however, the OS is pre-empting the process because a
higher-priority process on the Blocked/Suspend queue has just
become unblocked, the OS could move the running process
directly to the (Ready/Suspend) queue and free some main
memory.
• Any State  Exit:
– completed or because of some fatal fault condition.
– However, in some operating systems, a process may be
terminated by the parent process or when the parent process is
itself terminated
– If this is allowed, then a process in any state can be moved to
the Exit state.
Process Creation
• Parent process creates children processes, which, in turn
create other processes, forming a tree of processes

• Generally, process is identified and managed via a process


identifier (pid)

• Resource sharing options


– Parent and children share all resources
– Children share subset of parent’s resources
– Parent and child share no resources

• Execution options
– Parent and children execute concurrently
– Parent waits until children terminate
Process Creation (Cont.)
• 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
A Tree of Processes in Linux
Forking Separate Process
 Processes are created with the fork() system call
 Also called forking or spawning a process
 The child process, created by the parent process by the use of fork
system call, is an exact copy of the original parent process, except that
it has its own process ID
 After forking a child process, both the parent and child processes
continue to execute normally.
 If you want your program to wait for a child process to finish executing
before continuing
 Explicitly call wait() or waitpid() after the fork()
 A newly forked child process continues to execute the same program
as its parent process, at the point where the fork call returns.
 You can use the return value from fork to tell whether the program is
running in the parent process or the child process.
Forking Separate Process…
contd
• When a child process terminates, its death is communicated to its
parent so that the parent may take some appropriate action
• If a parent dies before its child, the child (orphan process) is
automatically adopted by the original “init” process whose PID is 1.
• If the operation is successful, there are then both parent and child
processes and both see fork return, but with different values:
– It returns a value of 0 in the child process and returns the child's
process ID in the parent process.
– If process creation failed, fork returns a value of -1 in the parent
process and no child is created.
Programming Practice
Note: Try this on a UNIX/Linux machine with the gcc
compiler installed.
• Save the program with the name
process_creation.c
• Compile it (while you are in the same directory
where the program is saved) with the command:
$ gcc process_creation.c -o process_creation.o
• Run the binaries with the command:
$ ./process_creation.o and enter
Programming Practice(1)
#include <stdio.h>
#include <unistd.h> /* contains fork system call prototype */
int main(void)
{
printf("Hello Batch 17\n");
fork( );
printf(”To be or not to be, that’s the question\n"); /* pay
attention to this statement */
}
Programming Practice(2)
• When the program on the previous slide is executed, it
first prints Hello Batch 17.
• When the fork is executed, an identical child process is
created.
• After that, both the parent and the child process begin
execution at the next statement, i.e., printf(”To be or not
to be, that’s the question\n");
• When a fork is executed, everything in the parent
process is copied to the child process. This includes
variable values, code, and file descriptors.
• Following the fork, the child and parent processes are
completely independent.
Programming Practice(3)
• There is no guarantee which process will first execute the
statement printf(”To be or not to be\n");.
• The child process begins execution at the statement
immediately after the fork, not at the beginning of the
program.
• A parent process can be distinguished from the child
process by examining the return value of the fork call. fork
returns a zero to the child process and the process id of the
child process to the parent.
• A process can execute as many forks as desired. However,
be wary of infinite loops of forks (there is a maximum
number of processes allowed for a single user).
Programming Practice(4)
• Modify the previous program ,compile and run it with the same procedure as
outlined in the earlier slide.
#include <stdio.h>
#include <unistd.h> /* contains fork prototype */
int main(void)
{
int pid;
printf("Hello Batch 17!!!!\n");
printf("I am the parent process and my pid is : %d .\n",getpid());
printf("Here, i am before use of forking\n");
pid = fork(); /* pay attention to this statement and the assignment , fork returned valued assigned to pid variable*/
printf("Here I am just after forking, dont know who executed me first\n");
if (pid == 0) /* Here goes the real test*/
printf("I am the child process and my pid is :%d.\n",getpid());
else
printf("I am the parent process and pid is: %d .\n",getpid());
}

//getpid() is the system call to get the current process “process id”.
Process 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 systems do not allow child to continue
if its parent terminates
– All children terminated - cascading termination
Roadmap
– How are processes represented and
controlled by the OS.
– Process states which characterize the
behaviour of processes.
– Data structures used to manage processes.
– Ways in which the OS uses these data
structures to control process execution.
– Discuss process management in UNIX SVR4.
Process Description
• The OS controls events within the computer system
– Schedules and dispatches processes for execution by
the processor
– Allocates resources to processes
– Responds to requests by user processes for basic
services

• OS as that entity that manages the use of system


resources by processes
Processes and Resources

• There are a number of processes (P1, . . ., Pn,) that have been


created and exist in virtual memory.
• Each process, during the course of its execution, needs access to
certain system resources, including the processor, I/O devices, and
main memory.
• In the figure, process P1 is running; at least part of the process is in
main memory, and it has control of two I/O devices.
• Process P2 is also in main memory but is blocked waiting for an I/O
device allocated to P1
• Process Pn has been swapped out and is therefore suspended.
Operating System
Control Structures
• For the OS is to manage processes and
resources, it must have information about
the current status of each process and
resource.
• Universal approach: OS construct tables
for each entity the operating system
manages
– Memory, I/O, File and Process Tables
OS Control Tables
Memory Tables
• Memory tables are used to keep track of
both main and secondary memory (Virtual
memory).
• Must include this information:
– Allocation of main memory to processes
– Allocation of secondary/virtual memory to
processes
– Protection attributes for access to shared
memory regions
– Information needed to manage virtual memory
I/O Tables
• Used by the OS to manage the I/O
devices and channels of the computer.
• The OS needs to know
– Whether a certian I/O device is available or
assigned
– The status of I/O operation
– The location in main memory being used as
the source or destination of the I/O transfer
File Tables
• These tables provide information about:
– Existence of files
– Location on secondary memory
– Current Status
– other attributes.
• Sometimes this information is maintained
by a file management system
Process Tables
• To manage processes the OS needs to
know details of the processes
– Current state
– Process ID
– Location in memory
– etc
• Process control block
– Process image is the collection of program.
Data, stack, and attributes
Process Attributes
• We can group the process control block
information into three general categories:
– Process identification
– Processor state information
– Process control information
Process Identification
• Each process is assigned a unique
numeric identifier.
• Many of the other tables controlled by the
OS may use process identifiers to cross-
reference process tables
Processor State
Information
• This consists of the contents of processor
registers.
– User-visible registers
– Control and status registers
– Stack pointers
• Program status word (PSW)
– contains status information
– Example: the EFLAGS register on Pentium
processors
Pentium II
EFLAGS Register

Also see Table 3.6


Process Control
Information
• This is the additional information needed
by the OS to control and coordinate the
various active processes.
– See table 3.5 for scope of information
Structure of Process
Images in Virtual Memory
Role of the
Process Control Block
• The most important data structure in an
OS
– It defines the state of the OS
• Process Control Block requires protection
– A faulty routine could cause damage to the
block destroying the OS’s ability to manage
the process
– Any design change to the block could affect
many modules of the OS
Roadmap
– How are processes represented and
controlled by the OS.
– Process states which characterize the
behaviour of processes.
– Data structures used to manage processes.
– Ways in which the OS uses these data
structures to control process execution.
– Discuss process management in UNIX SVR4.
Modes of Execution
• Most processors support at least two
modes of execution
• User mode
– Less-privileged mode
– User programs typically execute in this mode
• System mode
– More-privileged mode
– Kernel of the operating system
Process Creation
• Once the OS decides to create a new
process it:
– Assigns a unique process identifier
– Allocates space for the process
– Initializes process control block
– Sets up appropriate linkages
– Creates or expand other data structures
Switching Processes
• Several design issues are raised regarding
process switching
– What events trigger a process switch?
– We must distinguish between mode switching
and process switching.
– What must the OS do to the various data
structures under its control to achieve a
process switch?
When to switch processes
A process switch may occur any time that the OS has gained control from the
currently running process. Possible events giving OS control are:

Mechanism Cause Use


Interrupt External to the execution of Reaction to an asynchronous
the current instruction external event

Trap Associated with the execution Handling of an error or an


of the current instruction exception condition
Supervisor call Explicit request Call to an operating system
function

Table 3.8 Mechanisms for Interrupting the Execution of a Process


Change of
Process State …
• The steps in a process switch are:
1. Save context of processor including program
counter and other registers
2. Update the process control block of the
process that is currently in the Running state
3. Move process control block to appropriate
queue – ready; blocked; ready/suspend
Change of
Process State cont…
4. Select another process for execution
5. Update the process control block of the
process selected
6. Update memory-management data
structures
7. Restore context of the selected process
Is the OS a Process?
• If the OS is just a collection of programs
and if it is executed by the processor just
like any other program, is the OS a
process?
• If so, how is it controlled?
– Who (what) controls it?
Execution of the
Operating System
Non-process Kernel
• Execute kernel outside of any process
• The concept of process is considered to
apply only to user programs
– Operating system code is executed as a
separate entity that operates in privileged mode
Execution Within
User Processes
• Execution Within User
Processes
– Operating system software within
context of a user process
– No need for Process Switch to
run OS routine
Process-based
Operating System
• Process-based operating system
– Implement the OS as a collection of system
process
Security Issues
• An OS associates a set of privileges with
each process.
– Highest level being administrator, supervisor,
or root, access.
• A key security issue in the design of any
OS is to prevent anything (user or
process) from gaining unauthorized
privileges on the system
– Especially - from gaining root access.
System access threats
• Intruders
– Masquerader (outsider)
– Misfeasor (insider)
– Clandestine user (outside or insider)
• Malicious software (malware)
Countermeasures:
Intrusion Detection
• Intrusion detection systems are typically
designed to detect human intruder and
malicious software behaviour.
• May be host or network based
• Intrusion detection systems (IDS) typically
comprise
– Sensors
– Analyzers
– User Interface
Countermeasures:
Authentication
• Two Stages:
– Identification
– Verification
• Four Factors:
– Something the individual knows
– Something the individual possesses
– Something the individual is (static biometrics)
– Something the individual does (dynamic
biometrics)
Countermeasures:
Access Control
• A policy governing access to resources
• A security administrator maintains an
authorization database
– The access control function consults this to
determine whether to grant access.
• An auditing function monitors and keeps a
record of user accesses to system
resources.
Countermeasures:
Firewalls
• Traditionally, a firewall is a dedicated
computer that:
– interfaces with computers outside a network
– has special security precautions built into it to
protect sensitive files on computers within the
network.
Roadmap
– How are processes represented and
controlled by the OS.
– Process states which characterize the
behaviour of processes.
– Data structures used to manage processes.
– Ways in which the OS uses these data
structures to control process execution.
– Discuss process management in UNIX SVR4.
Unix SVR4
System V Release 4
• Uses the model of fig3.15b where most of
the OS executes in the user process
• System Processes - Kernel mode only
• User Processes
– User mode to execute user programs and
utilities
– Kernel mode to execute instructions that belong
to the kernel.
UNIX Process State
Transition Diagram
UNIX Process States
A Unix Process
• A process in UNIX is a set of data
structures that provide the OS with all of
the information necessary to manage and
dispatch processes.
• See Table 3.10 which organizes the
elements into three parts:
– user-level context,
– register context, and
– system-level context.
Process Creation
• Process creation is by means of the kernel
system call,fork( ).
• This causes the OS, in Kernel Mode, to:
1. Allocate a slot in the process table for the
new process.
2. Assign a unique process ID to the child
process.
3. Copy of process image of the parent, with
the exception of any shared memory.
Process Creation
cont…
4. Increment the counters for any files owned
by the parent, to reflect that an additional
process now also owns those files.
5. Assign the child process to the Ready to
Run state.
6. Returns the ID number of the child to the
parent process, and a 0 value to the child
process.
After Creation
• After creating the process the Kernel can
do one of the following, as part of the
dispatcher routine:
– Stay in the parent process.
– Transfer control to the child process
– Transfer control to another process.

You might also like