process concept

40
Process concept

Upload: mindy

Post on 10-Feb-2016

24 views

Category:

Documents


0 download

DESCRIPTION

Process concept. Concept of Multiprogramming. When there is a single program running in the CPU, it leads to the degradation of the CPU utilization. Example: When a running program initiates an I/O operation, the CPU remain idle until the I/O operation is completed. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Process concept

Process concept

Page 2: Process concept

Concept of Multiprogramming

When there is a single program running in the CPU, it leads to the degradation of the CPU utilization.

Example: When a running program initiates an I/O operation, the CPU remain idle until the I/O operation is completed.

Solution to this problem is provided by Multiprogramming.

Page 3: Process concept

A mode of operation that provides for

the interleaved execution of two or more programs by a single processor.

Definition:

Multiprogramming Continued..

Page 4: Process concept

Improving CPU utilization By allowing several programs to reside in main

memory at the “same time” the CPU might be shared,

such that when one program initiates an I/O operation,

another program can be assigned to the CPU, thus the improvement of the CPU utilization.

Multiprogramming Continued..

Page 5: Process concept

The concept of process need to be introduced in order to understand multiprogramming and the concurrent execution of sequential processes in a computer system.

What is a process?

Implementation

Multiprogramming Continued..

Page 6: Process concept

Process

Definition: A program in execution An asynchronous activity The “locus of control” of a procedure in

execution It is manifested by the existence of a

process control block (PCB) in the operating system.

Page 7: Process concept

A state of a process describes the activity that the process is doing at a certain moment in time.

New : A newly created process, not in the ready queue.

Ready : It can use the CPU , if available. Running : If it is currently in the CPU. Waiting : Waiting for some event ex: I/O Abend : Stops executing due to an error.

End : Finished executing properly.Suspended: Stops temporarily while the OS

uses the CPU

Process States

Page 8: Process concept

NEW

READY RUNNING

END

WAITINGI/O

States of processes

ABEND

SUSPENDED

Page 9: Process concept

Causes of state change

When a process executes, it changes states and interrupts cause process to change states.

Current State New state Interrupt Running End EOP ( End of Program)

Running ABEND Trap

(Abnormal end)

Running Blocked System Call (Start I/O) for I/O (SIO)

Page 10: Process concept

RUNNING

END

ABEND

BLOCKED

Depiction of state change

EOP

TRAP

SIO

Page 11: Process concept

The activity of a process is controlled by a data structure called Process Control Block(PCB).

A PCB is created every time a program is loaded to be executed.

So, a process is defined by PCB-Program couple.

Process Continued…

Page 12: Process concept

Structure of PCBPCB contains information about processes, for instance: the current state of a processUnique identification of processProcess priorityContents of some CPU registersInstruction Pointer (IP), also known as PCBase and limit registersTime limits and I/O status information

Page 13: Process concept

PROGRAM

STACK

Base code Limit Code

IP or PC Stack Pointer

Registers

Interrupt FlagsMODE

.….

State

Structure of PCB Contd…

Process name or IDPointer to next PCB

….

…..

ThreadControl Block (TCB)

Page 14: Process concept

A thread is known as “the unit of execution” of a process and it is represented by the Thread Control Block (TCB).

The TCB consists of four fields: PC, stack pointer, the contents of some registers, and the state.

Process Continued…

Page 15: Process concept

We can now observe how each stage of a process takes place by the aid of a state diagrams.

Process creation :

An OS can create one or more processes, via a create-process system call.

During the course of execution an user process may create new processes or threads as well. In this case, the creating process (thread) is called the parent and the created (new) process (thread) is named the child.

Process Continued…

Page 16: Process concept

Process Creation

New

Ready

The process is created and then inserted at the back of the ready queue, it moves to the head of the queue according to a scheduling policy.

Create

Page 17: Process concept

PCB

stack

Program

OS code

Load

Create

Process Creation Contd…

Page 18: Process concept

PCB

Memory

OS

Load

Create

Process Creation (closer view) Cont…

codedata

header

symbol table

Disk

code

data

stack

bssheap

Process working space

Program file

Page 19: Process concept

Process working space (run-time environment)

code

data

stack

bssheap

Process working space

Program text (write protected)Constants / initialized dataGlobal and static variablesDynamically allocated variables

Dynamic link, return address,Local variables, functionParameters, …

x = 4;int y = 7;int z;malloc(n);

Page 20: Process concept

Process working space (run-time environment)

code

data

stack

bssheap

Process working space

x = 4;int y = 7;int z;malloc(n);

Local

Local

Local

Local

Local

Local

Parameter

Parameter

Dynamic Link

Return Address

void sub(float total, int part ) {int List[5];float sum; …

}

sum

bss: means “block started by symbol” and has that name for historical reasons.

Activation record

Page 21: Process concept

Thread working space (run-time environment)

code

data

stack-main

bssheap

Multithreading: Each thread is a unit of execution.

stack-T1

stack-T2

Multithreaded processesneed a stack per thread.

All threads shared the same address space.

Each thread has is own TCB.

Page 22: Process concept

Thread working space (run-time environment)

code

data

stack-main

bssheap

Multithreading: All threads share the CPU-PC and CPU-SP.In this picture thread-T1 is using the CPU.

stack-T1

stack-T2SP

SP

SP PC

PC

PC

PCB

statestatestate

regsregsregs TCB-main

TCB-T2

TCB-T1

.

.

.

.

.

.

Open files Other resources

Process ID

CPU

sp

pc

regs

Page 23: Process concept

1stack

stack

2

PCB1 PCB2

READY RUNNING LOADER INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY

Memory snap shot of two processes in Ready State

The PCB is stored in the OS memory area in a linked list.

NULL

Page 24: Process concept

READY

RUNNING

Dispatcher

When a process reaches the head of the queue and the CPU is available, the process is dispatched which means that the CPU is assigned to the process. This cause a transition from the ready state to the running state.

When the time slice of the running process expires it goes back to the ready state.

Timer Interrupt

Ready to Running

Page 25: Process concept

1stack

stack

2

PCB1 PCB2

READY RUNNING LOADER DISPATCHER INTERRUPT HANDLER RUNTIME LIBRARY

CPU

Accumulator

IPOV MP

INTERRUPT FLAGS MASK

TO BE DEFINED LATER

Ready to Running

PI TI I/O ModeSVC

Page 26: Process concept

1stack

stack

2

PCB1 PCB2

READY RUNNING LOADER INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY

CPU

stack

2

PCB2

Ready to Running

Accumulator

IPOV MP

INTERRUPT FLAGS MASK

TO BE DEFINED LATERPI TI I/O SVC Mode

Page 27: Process concept

stack

1

PCB1

READY RUNNING LOADER INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY

CPU

PCB2

2stack

Running to Ready

Accumulator

IPOV MP

INTERRUPT FLAGS MASK

TO BE DEFINED LATERPI TI I/O SVC Mode

Page 28: Process concept

2stack

1

PCB2 PCB1

READY RUNNING LOADER INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY

CPU

stack

stack

1

PCB1 PCB2

2

stack

Running to Ready

Accumulator

IPOV MP

INTERRUPT FLAGS MASK

TO BE DEFINED LATERPI TI I/O SVC Mode

Page 29: Process concept

As the OS switches the allocation of CPU among processes it uses the PCB to store the CPU information or context, which represents the state of the process.

In the previous example we have seen how the OS performed a context switch between processes P2(from Running to Ready) and P1(from Ready to Running).

When a context switch occurs we need to save the state of the running process in its PCB and load the state of the new process in the CPU.

Process Continued…

Page 30: Process concept

READYRUNNING

Timer Interrupt

Dispatch

Case of Timer interrupt

P1P2

After a timer interrupt, the OS move P1 back to the ready state and the CPUIs assigned to P2.

Page 31: Process concept

Save state into PCB1

Reload state from PCB2

Save state into PCB2

Reload state from PCB1

TIMER

DISPATCH

DISPATCH

TIMER

P1 OS P2EXECUTING

WAITING

EXECUTING

Context switching

WAITING

WAITING

EXECUTING WAITING

WAITINGWAITING

WAITING

Page 32: Process concept

READY

RUNNING

WAITING

I/O Interrupt

Start I/O (System call)

Case of I/O interrupt

Page 33: Process concept

PCB1 PCB2

READY RUNNING LOADER INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY

I/O

SIO

Case of I/O interrupt Contd..

PCB3

I/O DEVICE

Accumulator

IPOV MP

INTERRUPT FLAGS MASK

TO BE DEFINED LATERPI TI I/O SVC

Page 34: Process concept

PCB1

READY RUNNING LOADER INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY

I/OPCB3

PCB2

Case of I/O interrupt Contd..

I/O DEVICE

PCB2

Accumulator

IPOV MP

INTERRUPT FLAGS MASK

TO BE DEFINED LATERPI TI I/O SVC

PCB3

Page 35: Process concept

READY RUNNING LOADER INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY

I/O

PCB2

SUSPEND

OS

Case of I/O interrupt Contd..

PCB1

I/O DEVICE

PCB3

Accumulator

IPOV MP

INTERRUPT FLAGS MASK

TO BE DEFINED LATERPI TI I/O SVC

Page 36: Process concept

READY RUNNING LOADER INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY

I/O

PCB3 PCB2PCB1

Case of I/O interrupt Contd..

I/O DEVICE

SUSPEND PCB3

Accumulator

IPOV MP

INTERRUPT FLAGS MASK

TO BE DEFINED LATERPI TI I/O SVC

OS

Page 37: Process concept

Save state into PCB3

Reload state from PCB2

Suspend P2

Resume P2

P3 OS P2EXECUTING

WAITING IN I/O QUEUE

READY STATE

Context switching

SIO

Put P3 into the I/O queue

DISPATCHIN I/O STATE EXECUTING

I/O INTERRUPT

HANDLE I/OSUSPENDED

DISPATCHEXECUTING

WAITING

READY STATEWAITING

READYEXECUTING

Put P3 in Ready state

READY

Page 38: Process concept

Handling I/O: Here the process waiting in the I/O queue

is moved back to the ready state after the I/O request is completed.

Page 39: Process concept

RUNNING

END

End of Process

EOP

ABEND

TRAP

Page 40: Process concept

OS

PCB

stackprog TRAP

EOP

1. ABORT PROGRAM

2. ERROR MESSAGE

3. FREE RESOURCES

4. DELETE PCB

1. PROGRAM RUNS SUCCESSFULLY

2. FREE RESOURCES

3. DELETE PCB