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
1 © Noha A. Yousri

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
2

© Noha A. Yousri

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?

3 © Noha A. Yousri

Example Execution

4

William Stallings

© Noha A. Yousri

Trace of Processes

5

William Stallings

© Noha A. Yousri

6 William Stallings © Noha A. Yousri .

Yousri .Two-State Process Model • Process may be in one of two states – Running – Not-running 7 William Stallings © Noha A.

I/O) before it can proceed with execution These states can happen even if we have multiprocessors.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. Yousri . when processes exceed number of processors 8 © Noha A.g.

Inc. Yousri . 9 © Noha A. All rights reserved.© 2004 Deitel & Associates.

Process States 10 William Stallings © Noha A. Yousri .

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) 11 © Noha A. Yousri .

Using Two Queues 12 William Stallings © Noha A. Yousri .

Yousri .Multiple Blocked Queues 13 William Stallings © Noha A.

g. Yousri . © Noha A. allow multiple processes 14 to share processor time.Process Management Processes are interrupted and resumed Processes should be able to communicate with OS to start a new process or signal the end of a process execution OS: Create processes Destroy ---Suspend --Resume ---Change Priority ---Block --Wake Up --Dispatch --Enable IPC Manage Process resources e.

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 --------Dispatch Dispatching: (by a dispatcher) -Assigning a processor to first process on 15 Running State ready list © Noha A.Process States and Transitions 1-Ready to Running: User runs program Process created. Yousri .

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.2. the interrupting clock generates an interrupt: OS gains control of processor Running State ----------------------Quantum expires Ready State 16 © Noha A. Yousri .

Timer runout User process initiates: block 17 © Noha A. I/O) will be in blocked list Running State -----------Block Blocked State -----------. Yousri .Ready State Wakeup Blocked State Note: OS initiates : Dispatch.3-Running to Blocked -Before quantum expires. Wakeup. a process issues an I/O -Process waiting for an event (e.g.

All rights reserved. Inc. A process is awake or asleep 18 © Noha A. Yousri .© 2004 Deitel & Associates.

Yousri .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 19 © Noha A.

Also in PCB: Register contents to store execution context of processor on which process 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. 20 © Noha A. and removes it from process table. Yousri . OS frees process’s memory and other resources.

21 © Noha A.© 2004 Deitel & Associates. All rights reserved. Yousri . Inc.

Yousri .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 22 © Noha A.

© 2004 Deitel & Associates. Inc. All rights reserved. Yousri . 23 © Noha A.

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.Suspend and Resume -OS allows admin. Yousri . 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 processor24 © Noha A. user or process to suspend a process -Suspension: .

suspend Ready/ Running -------. Running -------resume suspend Blocked -------. Yousri .Suspended Ready/S.Suspended Blocked -------resume 25 © Noha A.

All rights reserved. Inc. 26 © Noha A.© 2004 Deitel & Associates. Yousri .

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 27 © Noha A. Yousri .

control mode. Yousri . or kernel mode – More-privileged mode – Kernel of the operating system 28 William Stallings © Noha A.Modes of Execution • User mode – Less-privileged mode – User programs typically execute in this mode • System mode.

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 29 William Stallings © Noha A. Yousri .

Yousri .30 William Stallings © Noha A.

Yousri .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 31 William Stallings © Noha A.

Interrupts • Interrupt the normal sequencing of the processor • Most I/O devices are slower than the processor – Processor must pause to wait for device 32 William Stallings © Noha A. Yousri .

Yousri .Classes of Interrupts 33 William Stallings © Noha A.

Yousri .Program Flow of Control Without Interrupts 34 William Stallings © Noha A.

Program Flow of Control With Interrupts. Yousri . Short I/O Wait 35 William Stallings © Noha A.

Yousri .Program Flow of Control With Interrupts. Long I/O Wait 36 William Stallings © Noha A.

Yousri .Interrupt Handler • Program to service a particular I/O device • Generally part of the operating system 37 William Stallings © Noha A.

Interrupts • Suspends the normal sequence of execution 38 William Stallings © Noha A. Yousri .

Yousri .Interrupt Cycle 39 William Stallings © Noha A.

Interrupt Cycle • Processor checks for interrupts • If no interrupts fetch the next instruction for the current program • If an interrupt is pending. Yousri . suspend execution of the current program. and execute the interrupt-handler routine 40 William Stallings © Noha A.

Timing Diagram Based on Short I/O Wait 41 William Stallings © Noha A. Yousri .

Yousri .Timing Diagram Based on Long I/O Wait 42 William Stallings © Noha A.

control may not return to the program that was executing at the time of the interrupt 43 William Stallings © Noha A.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. Yousri .

Inc. the processor completes execution of the current instruction.Interrupts What are the steps taken when an interrupt is received? Handling interrupts: •After receiving an interrupt. the interrupted process is restored and executed or the next process is executed © 2004 Deitel & Associates. Yousri . All rights reserved. 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. 44 © Noha A.

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. 45 © Noha A. and how that process will respond to it. Yousri .

when doing so the OS does not deliver signals to the process until mask is cleared (by process). 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. E.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. masking when handling signal of same type 46 © Noha A. Yousri .g.

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) 47 © Noha A. Yousri .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.): send (receiver process. env. message) receive (sender process.

48 © Noha A. Receive: Blocking: receiver waits till a message is sent NonBlocking: receiver does not wait. Yousri . continues with other processes till it next attempts a receive.Send: Blocking: sender waits till the receiver acknowledges Synchronous communication NonBlocking: Sender does not wait Asynchronous communication Requires buffering increases throughput.

OS pauses execution of reader and allows writer to write 49 © Noha A. the OS pauses the writer’s execution and allows reader to read from buffer 2-As a process reads data. Yousri . OS synchronizes access to buffer: 1-After a writer completes writing to buffer (possibly filling it).Message passing: Example of implementation : Pipe Pipe: a buffer in memory is reserved (protected by OS) which processes can use to exchange data. data is removed from pipe 3-When reader completes reading.

Yousri .Distributed systems’ issues for message passing: -Acknowledgement protocol -Naming mechanism -Authentication problems Other IPC techniques: -Semaphores and Monitors -Memory sharing -Remote Procedure Calls 50 © Noha A.

Yousri . stack and resources -child shares text segment with parent -at creation of child. stack) -kernel maintains PCB in a protected region of memory -PCB stores: Contents of processor registers Process identifier Program counter System stack .Unix Processes: -Unix uses a virtual address space (text. it is identical to parent 51 © Noha A.Spawning: ‘fork’ system call creates copy of parent process -child gets copy of parent’s data. data.

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 52 © Noha A. Yousri .

‘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 53 © Noha A. Yousri .

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.