0% found this document useful (0 votes)
20 views

Oslecture3 4

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Oslecture3 4

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 47

Outline

• Process Concept
• Process State
• Process State-transitions
• Process Control Block
• Context switch
• Operations on Processes
Process Concept
• Process - a program in execution
• process execution proceeds in a sequential fashion
• A process is more than the program code, which is
sometimes known as the text section.
• A process includes the current activity, as
represented by the value of the program
counter and the contents of the processors
registrar,Stack which contains temporary
data,and a data section which contains global
variables.
Contd..
• A process is an active entity, with a
program counter specifying the next
instruction to execute and a set of
associated resources.
Difference Between Program and
Process
Program Process
•A set of instructions the •Program in execution
user/programmer has •An active/Dynamic
written.
•Compete for resources
•Passive/static
•Has code section, data
•Can not compete for section, stack and program
resources counter
•Has code section
Structure of a Process in
memory
stack

heap

data

text
Process State
• A process changes state as it executes.

new admitted
new exit terminated

interrupt

running
ready

Scheduler
I/O or dispatch
event I/O or
completion event wait

waiting
Process States
• New - The process is being created.
• Running - Instructions are being executed.
• Waiting - Waiting for some event to occur.
• Ready - Waiting to be assigned to a
processor.
• Terminated - Process has finished
execution.
Process Transition
• Null->New: A new process is created to
execute a program.
• New->Ready: The Operating system will
move a process from the new state to the ready
state. When it is prepared to take on an
additional process.
• Ready->Running: It is a time to select a new
process to run,the O.S. chooses one of the
processes in the ready state.
Cont..
• Running->Exit: The currently running process
terminated by the Operating System.If the
process indicates that it has completed or if it
abort.
• Running -> Ready: The most common
reason for this transition is that the running
process has reached the maximum allowable
time for uninterrupted execution.
Cont…
• Running -> Waiting: A process is put in
the wait state if it request something for
which it must wait. A request to the
operating system is usually in the form of
a system service call.
Process Control Block(PCB)
• Contains information associated with each
process
– Process State - e.g. new, ready, running etc.
– Program Counter - address of next instruction to be
executed
– CPU registers - general purpose registers, stack pointer
etc.
– CPU scheduling information - process priority, pointer
– Memory Management information - base/limit information
– Accounting information - time limits, process number
– I/O Status information - list of I/O devices allocated
Process Control Block

Pointer Process
State

Process Number

Program Counter

registrars

Memory limits

List of open files


Context Switch
• Switching the CPU to another process requires
saving the state of the old process and loading
the saved state for the new process known as
context switch.
• It is represented in the PCB of a process.
• It includes the value of the CPU registers, the
process state and memory management
information.
Cont..
• When a context switch occurs,the kernel
saves the context of the old process in its
PCB and loads the saved context of the
new process scheduled to run.
• In context switch , times are highly
dependent on hardware support.
Cont..
• For eg. Some processors provide multiple
sets of registers, A context switch simply
includes changing the pointer to the
current register sets, the system resorts
to copying register data to and from
memory as before.
Process Scheduling Queues
• Job Queue - set of all processes in the system
• Ready Queue - set of all processes residing in
main memory, ready and waiting to execute.
• Device Queues - set of processes waiting for an
I/O device.
• Process migration between the various queues.
• Queue Structures - typically linked list, circular
list etc.
Process Scheduling Queues
Contd..
• A common representation of process scheduling
is a queueing diagram.
• Each rectangular box represents a queue.
• Two types of queue:
- The ready queue
- A set of devices queues
• The circle represents the resources that serve
the queues.
• Arrows indicates the flow of processes in the
system.
Contd..
• A new process is initially put in the ready queue.
• It waits in the ready queue until it is selected for
execution.
• Once the process is assigned to the CPU & is
executing, One of several events could occur:
- The process could issue an I/O request & then be
placed in an I/O queue.
- The process could create a new subprocess & wait for
its termination.
Contd..
- The process could be removed forcibly from the CPU, as
a result of an interrupt, & be put back in the ready queue.
• In the first two cases, the process eventually
switches from waiting state to the ready state &
is then put back in the ready queue.
• A process continues this cycle until it terminates,
at which time it is removed from all queues &
has its PCB & resources deallocated.
Operations on Processes
Process Creation:
•Processes are created and deleted dynamically
•Process which creates another process is called a
parent process; the created process is called a
child process.
•Result is a tree of processes
• e.g. UNIX - processes have dependencies and
form a hierarchy.
•Resources required when creating process
• CPU time, files, memory, I/O devices etc.
Cont..
• Resource sharing
– Parent and children share all resources.
– Children share subset of parent’s resources - prevents
many processes from overloading the system.
– Parent and child execute concurrently.
– Parent waits until child has terminated.

• Address Space
– Child process is duplicate of parent process.
– Child process has a program loaded into it.
UNIX Process Hierarchy
UNIX Process Creation
• Fork system call creates new processes

• execute system call is used after a fork to


replace the processes memory space with
a new program.
Suspend/Resume Process
-Suspension of process is limited with
resumption and vice versa.
-A suspended process cannot processed
until it is resumed by another process.
-The suspension of a process is depend on
many things.
1. If a system is functioning poorly and may
fail, it is advisable to suspend a process
and then resume once the problem is
solved.
Contd..

2. If user suspect about partial result of


process , he may suspend it until he
ascertain the process is functioning
correctly.
3. Some process may be suspended in
response to short term fluctuation in system
load, then it can be resumed once the load
settles back to normal levels.
Process Termination
• A process terminates when it finishes
executing its last statement and asks the
operating system to delete it by using the
exit system call.
• At the point,the process may return Output
data from child to parent process.
• All the resources of the process including
physical and virtual memory ,open files,
I/O buffer are deallocated by operating
system.
Contd..
- A process can cause the termination of
another process via an appropriate system
call, otherwise users could arbitrarily kill
each other's job.
- A Parent therefore needs to know the
identities of its children.
- OS does not allow child to continue if parent
terminates
Interupt Processing

- On a computer system, an interrupt is an event


that alters the sequence in which a processor
executes instructions.
- Generated by the hardware of the computer
system.
- When an interrupt occurs,
- The OS gains control
- It saves the state of the interrupted process.
Contd..
- OS analyzes the interrupt and passes
control to the appropriate routine to
handle the interrupt.
- The state of the interrupted process is
restored.
- The interrupted process executes.
Threads
• Processes do not share resources well
– high context switching overhead

• A thread ,sometimes called a lightweight


process,is basic unit of CPU utilization.

• it consists of:
-thread ID,program counter, register set
and stack space
Contd..

-A thread shares the following with peer


threads:
code section, data section and OS
resources,such as open files and signals.
- Eg. MS DoS
cont.
• Thread context switch still requires a
register set switch, but no memory
management related work!!
• Thread states -
• ready, blocked, running, terminated
• Threads share CPU and only one thread
can run at a time.
• No protection among threads.
Benefits
• It takes less time to create new thread.
• It takes less time to terminate a thread
than process.
• It takes less time to switch between two
thread within the same process.
• It communicates in same process without
invoke the kernel.
User Threads
• Thread management done by user-level threads
library
• Supported the kernel, via a set of library calls at
the user level.
• The library supports thread creation, scheduling
and management with no support from the
kernel,because the kernel is unaware of user
level threads.
Contd..
- All threads creation and scheduling are done in
user space.
- User level threads are fast to create and manage.
- Disadv: If kernel is single threaded, system call from any
thread can block the entire task.
-Example thread libraries:
- POSIX P threads
- Win32 threads
- Java threads
Kernel Threads
• Supported by the OS
• Performs thread creation , scheduling and
management in kernel space.
• Slower to create and manage.
• Examples
• Windows XP/2000 , Solaris 2
• Linux , Mach, OS/2
Multithreading

- It refers to the ability of an OS to support


multiple threads of execution within a single
process.

- UNIX , OS/2,Windows 2000,Solaris, Linux,


Mach
Single and Multithreaded
Processes
Multithreading Models

• Many-to-One model

• One-to-One model

• Many-to-Many model
Many-to-One
• Many user-level threads mapped to single
kernel thread
• Thread management is done in user
space
• It is efficient.
• The entire process will block if a thread
makes a blocking system call,because
only one thread can access the kernel at a
time.
Contd..

- Multiple thread are unable to run in parallel on


multiprocessor.
- User level thread libraries implemented on OS do
not support kernel threads use the many -to –
one model
- Examples:
Solaris Green Threads
GNU Portable Threads
Many-to-One Model
One-to-One
• Each user-level thread maps to kernel
thread
• It allows multiple threads to run in parallel
on multiprocessors.
• Examples
– Windows NT/XP/2000
– Linux
– Solaris 9 and later
One-to-one Model
Many-to-Many Model
• Allows many user level threads to
be mapped to many kernel threads
• Allows the operating system to
create a sufficient number of kernel
threads
• Solaris prior to version 9
• Windows NT/2000 with the
ThreadFiber package
Many-to-Many Model

You might also like