Lecture4

53
© Noha A. Yousri 1 Lecture 4 Process Concepts What is a process? Life Cycle of a process Process Management Process States and Transitions Process Control Block Process Operations Suspend and Resume Context Switching Interrupts Inter Process Communication Unix Processes

Transcript of Lecture4

Page 1: Lecture4

© Noha A. Yousri

1

Lecture 4

Process Concepts

What is a process?

Life Cycle of a process

Process Management

Process States and Transitions

Process Control Block

Process Operations

Suspend and Resume

Context Switching

Interrupts

Inter Process Communication

Unix Processes

Page 2: Lecture4

© Noha A. Yousri

2

What is a Process?

When a processor breathes life into a program it becomes an active entity

�A process Program in execution

A process has:

-Address space consisting of:

Text Region: Stores code to be executed

Data Region: Stores variables and dynamically allocated memory used during execution

Stack region: Stores instructions, local variables for active procedure calls

--Stack grows with issuing nested calls, shrinks when returns

Page 3: Lecture4

© Noha A. Yousri

3

Life Cycle of a process

Think about:

-Can two or more processes execute concurrently? How they can be managed?

-What might happen when a process needs I/O during execution?

Page 4: Lecture4

© Noha A. Yousri

4

Example Execution

William Stallings

Page 5: Lecture4

© Noha A. Yousri

5

Trace of Processes

William Stallings

Page 6: Lecture4

© Noha A. Yousri

6

William Stallings

Page 7: Lecture4

© Noha A. Yousri

7

Two-State Process Model

• Process may be in one of two states

– Running

– Not-running

William Stallings

Page 8: Lecture4

© Noha A. Yousri

8

Life Cycle of a process

-A process goes through discrete states

-Various events can cause process to change states

Process states are:

Running State: Executing on a processor

Ready State: Waiting for a processor

Blocked State: Waiting for an event (e.g. I/O) before it can

proceed with execution

These states can happen even if we have multiprocessors, when processes exceed number of processors

Page 9: Lecture4

© Noha A. Yousri

9

2004 Deitel & Associates, Inc. All rights reserved.

Page 10: Lecture4

© Noha A. Yousri

10

Process States

William Stallings

Page 11: Lecture4

© Noha A. Yousri

11

How processes in ready or blocked states are managed?

-Ready List: processes arranged in priority order

-Blocked list: processes in an unordered list

(But processes waiting for the same event are

given priorities to follow when the event

happens)

Page 12: Lecture4

© Noha A. Yousri

12

Using Two Queues

William Stallings

Page 13: Lecture4

© Noha A. Yousri

13

Multiple Blocked Queues

William Stallings

Page 14: Lecture4

© Noha A. Yousri

14

Process Management

Processes are interrupted and resumedProcesses should be able to communicate with OS to start a

new process or signal the end of a process execution

OS: Create processesDestroy ----Suspend ---Resume ----Change Priority ----Block ---Wake Up ---Dispatch ---Enable IPCManage Process resources e.g. allow multiple processes

to share processor time.

Page 15: Lecture4

© Noha A. Yousri

15

Process States and Transitions

1-Ready to Running:

User runs program

� Process created, and inserted into ready list

--Process moves towards head of list as other processes complete their turns using processor

--When reaching the head of ready list, and processor is available

Ready State ---------� Running State

Dispatch

Dispatching: (by a dispatcher)

-Assigning a processor to first process on

ready list

Page 16: Lecture4

© Noha A. Yousri

16

2- Running to Ready

Interrupting Clock (interval timer)

-OS uses it to allow a process to run for a specific

quantum

-Not to monopolize system

-if a process does not yield processor before quantum expires, the interrupting clock generates an interrupt:

� OS gains control of processor

Running State -----------------------� Ready State

Quantum expires

Page 17: Lecture4

© Noha A. Yousri

17

3-Running to Blocked

-Before quantum expires, a process issues an I/O

-Process waiting for an event (e.g. I/O) will be in blocked list

Running State ------------� Blocked State

Block

Blocked State ------------� Ready StateWakeup

Note:OS initiates : Dispatch, Wakeup, Timer runoutUser process initiates: block

Page 18: Lecture4

© Noha A. Yousri

18

2004 Deitel & Associates, Inc. All rights reserved.

A process is awake or asleep

Page 19: Lecture4

© Noha A. Yousri

19

How an OS manages processes : how it identifies them, stores their related data?

OS identifies a process: PID (process ID)

OS creates Process Control Block (Process Descriptor)

PCB contains:

PID

Process State

Program Counter (which instruction to execute)

Scheduling priority

Credentials

Pointer to parent process (that created it)

Pointer to child processes

Pointer to data

Allocated resources

Page 20: Lecture4

© Noha A. Yousri

20

Also in PCB:

Register contents

to store execution context of processor on whichprocess was running and quit the running state

What happens on a process transition?

OS updates information in PCB

How are PCBs managed?

OS maintains PCBs in a system wide or per-user process table

What happens when a process terminates?

When process terminates, OS frees process’s memory and other resources, and removes it from process table.

Page 21: Lecture4

© Noha A. Yousri

21

2004 Deitel & Associates, Inc. All rights reserved.

Page 22: Lecture4

© Noha A. Yousri

22

Process Operations:

Spawning: A process spawns a new process that becomes its child

Parent process spawns a Child process

Each child is created by only one process

A process can be a parent to many child processes

The child processes can be in a hierarchical structure

Page 23: Lecture4

© Noha A. Yousri

23

2004 Deitel & Associates, Inc. All rights reserved.

Page 24: Lecture4

© Noha A. Yousri

24

Suspend and Resume

-OS allows admin, user or process to suspend a process

-Suspension:

- Indefinitely removing a process from sharing

processor’s time without destroying it

-Allows : - adjustment of system load

-respond to threats of system failure

-Used instead of aborting a process, until a user can

ascertain whether the process functions correctly

-Initiated by a process itself or another process

-In multiprocessing, one running process can

suspend another one running on another processor

Page 25: Lecture4

© Noha A. Yousri

25

suspend

Ready/ Running --------� Suspended Ready/S. Running--------resume

suspendBlocked --------� Suspended Blocked

--------resume

Page 26: Lecture4

© Noha A. Yousri

26

2004 Deitel & Associates, Inc. All rights reserved.

Page 27: Lecture4

© Noha A. Yousri

27

Context Switching

-At stopping a running process and begin a ready process:

kernel must save execution context of running process to PCB

kernel must load the ready process’s execution context

Page 28: Lecture4

© Noha A. Yousri

28

Modes of Execution

• User mode

– Less-privileged mode

– User programs typically execute in this mode

• System mode, control mode, or kernel mode

– More-privileged mode

– Kernel of the operating system

William Stallings

Page 29: Lecture4

© Noha A. Yousri

29

Execution of the Operating System

• Non-process Kernel– Execute kernel outside of any process

– Operating system code is executed as a separate entity that operates in privileged mode

• Execution Within User Processes– Operating system software within context of a

user process

– Process executes in privileged mode when executing operating system code

William Stallings

Page 30: Lecture4

© Noha A. Yousri

30

William Stallings

Page 31: Lecture4

© Noha A. Yousri

31

Execution of the Operating System

• Process-Based Operating System

– Implement operating system as a collection of

system processes

– Useful in multi-processor or multi-computer

environment

William Stallings

Page 32: Lecture4

© Noha A. Yousri

32

Interrupts

• Interrupt the normal sequencing of the processor

• Most I/O devices are slower than the processor

– Processor must pause to wait for device

William Stallings

Page 33: Lecture4

© Noha A. Yousri

33

Classes of Interrupts

William Stallings

Page 34: Lecture4

© Noha A. Yousri

34

Program Flow of Control Without

Interrupts

William Stallings

Page 35: Lecture4

© Noha A. Yousri

35

Program Flow of Control With

Interrupts, Short I/O Wait

William Stallings

Page 36: Lecture4

© Noha A. Yousri

36

Program Flow of Control With

Interrupts; Long I/O Wait

William Stallings

Page 37: Lecture4

© Noha A. Yousri

37

Interrupt Handler

• Program to service a particular I/O device

• Generally part of the operating system

William Stallings

Page 38: Lecture4

© Noha A. Yousri

38

Interrupts

• Suspends the normal sequence of execution

William Stallings

Page 39: Lecture4

© Noha A. Yousri

39

Interrupt Cycle

William Stallings

Page 40: Lecture4

© Noha A. Yousri

40

Interrupt Cycle

• Processor checks for interrupts

• If no interrupts fetch the next instruction for the current program

• If an interrupt is pending, suspend execution of the current program, and execute the interrupt-handler routine

William Stallings

Page 41: Lecture4

© Noha A. Yousri

41

Timing Diagram Based on Short

I/O Wait

William Stallings

Page 42: Lecture4

© Noha A. Yousri

42

Timing Diagram Based on Long I/O

Wait

William Stallings

Page 43: Lecture4

© Noha A. Yousri

43

Multiprogramming

• Processor has more than one program to execute

• The sequence the programs are executed depend on their relative priority and whether they are waiting for I/O

• After an interrupt handler completes, control may not return to the program that was executing at the time of the interrupt

William Stallings

Page 44: Lecture4

© Noha A. Yousri

44

Interrupts

What are the steps taken when an interrupt is received?

Handling interrupts:•After receiving an interrupt, the processor completes execution of the current instruction, then pauses the current process•The processor will then execute one of the kernel’s interrupt-handling functions•The interrupt handler determines how the system should respond•Interrupt handlers are stored in an array of pointers called the interrupt vector•After the interrupt handler completes, the interrupted process is restored and executed or the next process is executed 2004 Deitel & Associates, Inc. All rights reserved.

Page 45: Lecture4

© Noha A. Yousri

45

Inter-Process Communication

1-Signals

-Software interrupts that notify a process that an event has occurred

-do not allow specification of data to be exchanged with other processes

-when a signal occurs, OS determines which process should recieve it, and how that process will respond to it.

Page 46: Lecture4

© Noha A. Yousri

46

Processes may do the following:

Catch signal : processes specifies a routine that an OS calls when delivering a signal

Ignore signal: when a process ignores a signal, it relies on the OS default action to handle signal (default actions as: abort process (exit) or memory dump (exit): stores context information and data from address space)

Mask signal: a process blocks a signal by masking it, when doing so the OS does not deliver signals to the process until mask is cleared (by process). E.g. masking when handling signal of same type

Page 47: Lecture4

© Noha A. Yousri

47

2-Message Passing

Requires a sender process and a receiver process

Can be bidirectional: A process can be both sender and receiver

Implemented using system calls (present in prog. env.):

send (receiver process, message)

receive (sender process, message)

Send can be : for a particular receiver (name is identified)

Broadcast to all processes (name not identified)

Receive: From a particular sender

From any sender ( or any member in a group of

senders)

Page 48: Lecture4

© Noha A. Yousri

48

Send:

Blocking: sender waits till the receiver acknowledgesSynchronous communication

NonBlocking: Sender does not waitAsynchronous communicationRequires bufferingincreases throughput,

Receive:

Blocking: receiver waits till a message is sent

NonBlocking: receiver does not wait, continues with other processes till it next attempts a receive.

Page 49: Lecture4

© Noha A. Yousri

49

Message passing:

Example of implementation : Pipe

Pipe: a buffer in memory is reserved (protected by OS) which processes can use to exchange data.

OS synchronizes access to buffer:

1-After a writer completes writing to buffer (possibly filling it), the OS pauses the writer’s execution and allows reader to read from buffer

2-As a process reads data, data is removed from pipe

3-When reader completes reading, OS pauses execution of reader and allows writer to write

Page 50: Lecture4

© Noha A. Yousri

50

Distributed systems’ issues for message passing:

-Acknowledgement protocol

-Naming mechanism

-Authentication problems

Other IPC techniques:

-Semaphores and Monitors

-Memory sharing

-Remote Procedure Calls

Page 51: Lecture4

© Noha A. Yousri

51

Unix Processes:

-Unix uses a virtual address space (text, data, stack)

-kernel maintains PCB in a protected region of memory

-PCB stores: Contents of processor registers

Process identifier

Program counter

System stack

- Spawning: ‘fork’ system call creates copy of parent process

-child gets copy of parent’s data, stack and

resources

-child shares text segment with parent

-at creation of child, it is identical to parent

Page 52: Lecture4

© Noha A. Yousri

52

Spawning (‘fork’ Cont’d)

-parent and child identical until either determines its

identity

-At execution of fork:

parent receives ID of child,

child receives value of 0

-allows child to recognize it is new

-allows programmers to identify child and specify

new instructions for child to execute

Page 53: Lecture4

© Noha A. Yousri

53

‘exec’ system call:

process calls ‘exec’ to load new program from a file

‘wait’ system call:

parent can call ‘wait’ to be blocked until a child finishes

(for example)

‘exit’ system call:

when a process wants to terminate

‘kill’ system call:

terminates process and its children

‘nice’: changes process scheduling priority

‘signal’: to specify a signal handler for a particular signal type

IPC uses signals and pipes to communicate processes