Вы находитесь на странице: 1из 59

1

SPRING 2011 - 102

Outline
Output Device Processor

How are processes represented and controlled by the OS


Process states which characterize the behaviour of processes.

Input Device

SPRING 2011 - 102

SPRING 2011 - 102

Idea Program Result

Idea
Program Result

Idea

Program

Result
SPRING 2011 - 102

Idea Program Idea Abstract Machine Result

Program

Abstract Machine

Physical Machine

Result

Idea

Program

Abstract Machine

SPRING 2011 - 102

Result

Process: A sequential program in execution Resource: Any abstract resource that a process can request, and which may can cause the process to be blocked if the resource is unavailable.

File: A special case of a resource. A linearlyaddressed sequence of bytes. A byte stream.

SPRING 2011 - 102

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.

SPRING 2011 - 102

SPRING 2011 - 102

Idea Execution Engine

Algorithm

Status Stack
R U N

Files Files Files

Source Program

Binary Program

Data

Other Resources

Process
SPRING 2011 - 102

10

SPRING 2011 - 102

11

Information associated with each process

Process state

Program counter
CPU registers CPU scheduling information Memory-management information Accounting information

I/O status information

SPRING 2011 - 102

12

Consider three processes being executed All are in memory (plus the dispatcher) Lets ignore virtual memory for this.

SPRING 2011 - 102

13

Each process runs to completion

SPRING 2011 - 102

14

Timeout I/O

SPRING 2011 - 102

15

When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process - > Context switching
Context-switch time is overhead; the system does no useful work while switching It is the job of Dispatcher. The time it takes for a dispatcher to stop one process and start another is known as dispatch latency. Interrupt, Trap and signal triggers a process switch.

SPRING 2011 - 102

16

SPRING 2011 - 102

17

SPRING 2011 - 102

18

Process may be in one of two states


Running Not-running

SPRING 2011 - 102

19

Etc processes moved by the dispatcher of the OS to the CPU then back to the queue until the task is competed

SPRING 2011 - 102

20

New Running

Exit

Not Running

As a process executes, it changes state


new: The process is being created running: Instructions are being executed Not Running

Not Running

Ready

waiting: The process is waiting for some event ready: The process is waiting to be run

Blocked

terminated: The process has finished execution


SPRING 2011 - 102

21

User submits job (batch/interactive)

Job accepted

Put on NEW and placed in queue

Job state changes from NEW to READY

Indicates job waiting for CPU

Job state changes from READY to RUNNING

When selected for CPU and processing

Job state changes from RUNNING to READY

Time out

Job state changes from RUNNING to WAITING

Requires unavailable resources/ wait for event

Job state changes from WAITING to RUNNING

Event happened

Job state changes to FINISHED

Job completed (successfully or unsuccessfully)


SPRING 2011 - 102

22

Limitations
If all processes in the queue are always ready to execute only then the above queuing discipline will work. But it may also be possible that some processes in the queue are ready to execute, while some are waiting for an I/O operation to complete. So the dispatcher has to scan the list of processes looking for the process that is not blocked and is there in the queue for the longest SPRING 2011 - 102

23

In the simplest solution, model require an queue for the blocked processes. But when an event occurs the dispatcher would have to cycle through the entire queue to see which process is waiting for the event. This can cause huge overhead when their may be 100s or 1000s of processes
SPRING 2011 - 102

24

Job Queue

Job queue set of all processes in the system

Ready queue set of all processes residing in main memory, ready and waiting to execute
SPRING 2011 - 102

25

Job Queue

Process not running PCB is in some scheduler queue


Separate queue for each device/signal/condition

SPRING 2011 - 102

26

SPRING 2011 - 102

27

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 Interrupt Cause External to the execution of the current instruction Use Reaction to an asynchronous external event

Trap Supervisor call

Associated with the execution of Handling of an error or an the current instruction exception condition Explicit request Call to an operating system function

SPRING 2011 - 102

28

The steps in a process switch are:

Save context of processor including program counter and other registers

Update the process control block of the process that is currently in the Running state
Move process control block to appropriate queue ready; blocked; ready/suspend

Select another process for execution Update the process control block of the process selected Restore context of the selected process
SPRING 2011 - 102

29

SPRING 2011 - 102

30

A process migrates between various scheduling queues throughout its life cycle. OS must select processes from these queues in some predefined fashion. This selection is done by an appropriate scheduler

Objective of Multiprogramming

Objective of Time Sharing

Process Scheduler
SPRING 2011 - 102

31

Job Queue

Short-term scheduler (or CPU scheduler) selects which process should be executed next and allocates CPU
SPRING 2011 - 102

32

Long-term scheduler (or job scheduler) selects processes from the job pool (processes spooled on hard disk) to be brought into the ready queue (inside main memory).

Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow) The long-term scheduler controls the degree of multiprogramming

SPRING 2011 - 102

33

Short-term scheduler (or CPU scheduler) selects which


process should be executed next and allocates it the CPU through the dispatcher.

Short-term scheduler is invoked very frequently


(milliseconds) (must be fast).

Invoked when following events occur

CPU slice of the current process finishes Current process needs to wait for an event Clock interrupt I/O interrupt System call Signal
SPRING 2011 - 102

34

Processes can be described as either:

I/O-bound process spends more time doing I/O than computations, many short CPU bursts

CPU-bound process spends more time doing computations; few very long CPU bursts

If the degree of multi programming is stable, then the average rate of process creation must be equal to the average departure rate of process leaving the system

The degree of multiprogramming describes the maximum number of processes that a singleprocessor system can accommodate efficiently SPRING 2011 - 102

35

Also known as swapper It can suspend one process by putting it in the suspended states and transferring it to disk. This is called a swap-out operation. Now the main memory has space to bring in a new process. The swapped out process can be swapped-in later on Swapping decision is based on several factors Arrival of a higher priority process but no memory available To improve process mix. Requiring memory to be freed up because memory requirement of a process cannot be met.
SPRING 2011 - 102

36

SPRING 2011 - 102

37

SPRING 2011 - 102

38

Parent process create children processes, which, in turn create other processes, forming a tree of processes Resource sharing

Parent and children share all resources Children share subset of parents resources

Parent and child share no resources

Execution

Parent and children execute concurrently Parent waits until children terminate
SPRING 2011 - 102

39

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

SPRING 2011 - 102

40

SPRING 2011 - 102

41

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 system do not allow child to continue if its parent terminates
All children terminated

- cascading termination

SPRING 2011 - 102

42

SPRING 2011 - 102

43

Process
(Executing Concurrently)

Cooperating
It can affect OR Can be affected by any other process.

Independent
It cannot affect OR Can not be affected by any other process.

SPRING 2011 - 102

44

Independent

process is a

Cooperating

process is a

process that cannot affect or cannot be affected by the execution of another process. A process that does not share data with another process is independent

process that can affect or can be affected by the execution of another process. A process that share data with other process is cooperating process.

SPRING 2011 - 102

45

Message passing

Shared Communication Memory

Inter Process

SPRING 2011 - 102

46

IPC is a mechanism for processes to communicate and to synchronize their actions. There are two fundamental models of IPC:

Shared Memory A region of memory that is shared by cooperating processes is established. Processes can then exchange information by reading and writing data to the shared region. Messaging system Communication takes place by means of messages exchanged between the cooperating processes. Processes communicate with each other without resorting to shared variables.
SPRING 2011 - 102

47

Process 1 Process 2

Process - 1

Shared
Process - 2

Kernel

Kernel

Message Passing

Shared Memory

SPRING 2011 - 102

48

Useful for Exchanging smaller amount of data. Much easier to implement than shared Memory in inter-computer communications. Implemented by using System calls Requires More time (Kernel Intervention)

Shared Memory allows max Speed. Generally convenient for IPC, as done at memory speed. Faster in case of one computer. Kernel intervention is only required t beginning.

Message Passing

Shared Memory
SPRING 2011 - 102

49

SPRING 2011 - 102

50

Lets take an Example of Producer Consumer Model. Producer Produces information which is consumed by Consumer.

Producer Compiler

Consumer
Assembly Code

Assembler Producer

Object Code

Loader Consumer

SPRING 2011 - 102

51

To allow producer and consumer run concurrently we must have a buffer that can be filled by the producer and emptied by the consumer. Buffer can be bounded or unbounded.

SPRING 2011 - 102

52

Message passing provides a mechanism to allow processes to communicate and to synchronize their actions without sharing the same address space. It is particularly useful in a distributed environment. If P and Q wish to communicate, they need to:

Establish a communication between them link them.

Exchange messages via send Message & Receive Message

SPRING 2011 - 102

53

Following are the methods for logical implementation of communication link.

SPRING 2011 - 102

54

This scheme is either symmetric in addressing or asymmetric in addressing


send (P, message) send a message to process P receive(id, message) receive a message from any process

send (P, message) send a message to process P receive(Q, message) receive a message from process Q

Properties of communication link in

A Link is established automatically. A link is associated with exactly one pair of communicating processes. S 2011 - 102 Between each pair there exists exactly one link
PRING

55

Messages are directed and received from mailboxes (ports).


Each mailbox has a unique id. Processes can communicate only if they share a mailbox. It is an abstract object into which a message can be placed in or removed from. Link may be unidirectional or bi-directional

Properties of communication link in

Link established only if processes share a common mailbox. A link may be associated with many processes Each pair of processes may share several communication S 2011 - 102 links.
PRING

Operations create a new mailbox send and receive messages through mailbox destroy a mailbox
Mailbox sharing P1, P2, and P3 share mailbox A. P1, sends; P2 and P3 receive. Who gets the message?

16 MARCH 2011

Solutions

Allow a link to be associated with at most two processes. Allow only one process at a time to execute a receive operation. Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.

COPYRIGHT VIRTUAL UNIVERSITY OF PAKISTAN

16 MARCH 2011

58

Message passing may be either blocking or non-blocking Blocking is considered synchronous


Blocking send has the sender block until the message is received Blocking receive has the receiver block until a message is available

Non-blocking is considered asynchronous


Non-blocking send has the sender send the message and continue Non-blocking receive has the receiver receive a valid message or null
SPRING 2011 - 102

59

Queue of messages attached to the link; implemented in one of three ways


1. 2. 3. Zero capacity 0 messages Sender must wait for receiver Bounded capacity finite length of n messages Sender must wait if link full Unbounded capacity infinite length Sender never waits

SPRING 2011 - 102

Вам также может понравиться