Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 1 Process Description and Control Chapter 3.

Similar presentations

Presentation on theme: "Chapter 3 1 Process Description and Control Chapter 3."— Presentation transcript:

1 Chapter 3 1 Process Description and Control Chapter 3

2 2 Processes - Tasks n Recall: a process is a program in execution u why must we distinguish between process and program? u because a single program can be executed by several processes... n The word process sometimes applies also to a program that is capable of executing independently and simultaneously with others n Task is an equivalent term

3 Chapter 3 3 OS Requirements for Processes n OS must interleave the execution of several processes to maximize CPU usage while providing reasonable response time n OS must allocate resources to processes while avoiding deadlock and starvation n OS must support inter process communication and user creation of processes

4 Chapter 3 4 Dispatcher (short-term scheduler, CPU scheduler) n Is an OS program that assigns the CPU to one process or to another n It decides who goes next according to a scheduling algorithm (chap 9) n The CPU will execute instructions from the dispatcher to switch from process A to process B

5 Chapter 3 5 When does a process get created? n Submission of a job n User logs on n Created by OS to provide a service to a user (ex: printing a file) n Spawned by an existing process u a user program can dictate the creation of a number of processes

6 Chapter 3 6 When does a process get terminated? n User logs off n Process executes a system call to terminate n Error or fault conditions, timeout

7 Chapter 3 7 Reasons for Process Termination n Normal completion n Time limit exceeded n Memory unavailable n Memory bounds violation n Protection error u example: write to read-only file n Arithmetic error n Time overrun u process waited longer than a specified maximum for an event

8 Chapter 3 8 Reasons for Process Termination n I/O failure n Invalid instruction u happens when try to execute data n Privileged instruction n Operating system intervention u such as when deadlock occurs n Parent request to terminate one offspring n Parent terminates so child processes terminate

9 Chapter 3 9 Process States n Let us start with these states: u The Running state F The process(es) that is (are) executing on the CPU are in Running state u The Blocked state F A process that is waiting for something (e.g. I/O) to complete is in Blocked state u The Ready state F A process that is ready to be executed is in Ready state [due to limited number of CPUs]

10 Chapter 3 10 Other Useful States n The New state u OS has performed the necessary actions to create the process F has created a process identifier F has created tables needed to manage the process u but has not yet committed to execute the process (not yet admitted) F because resources are limited u Long-term scheduler takes this decision

11 Chapter 3 11 Other Useful States n The Exit state u Termination moves the process to this state u It is no longer eligible for execution u Tables and other info are temporarily preserved for auxiliary program F Ex: accounting program that cumulates resource usage for billing the users n The process (and its tables) gets deleted when the data is no longer needed

12 Chapter 3 12 A Five-state Process Model

13 Chapter 3 13 Process Transitions n New -> Admit: u long-term scheduler decides that the process can be executed

14 Chapter 3 14 Process Transitions n Ready --> Running u The dispatcher selects a new process to run (to be discussed in Chapter 4). n Running --> Ready u the running process is timed out u the running process gets interrupted because a higher priority process is in the ready state (needs to run)

15 Chapter 3 15 Process Transitions n Running --> Blocked u When a process requests something for which it must wait F a service of the OS that requires a wait F an access to a resource not yet available F initiates I/O and must wait for the result F waiting for a process to provide input (IPC) n Blocked --> Ready u When the event for which it was waiting occurs

16 Chapter 3 16 Process Transitions Release: we discussed the reasons earlier

17 Chapter 3 17 A Queuing Discipline n Queue Ready: processes waiting to get executed on CPU n When event n occurs, the process that was waiting for it is moved into the Ready queue

18 Chapter 3 18 Variations the basic model n The basic model we have discussed is very widely used. n It has a great number of variations, since no two OS are alike n An example follows: adding swapping states

19 Chapter 3 19 The need for swapping n So far, all the processes had to be (at least partly) in main memory n Even with virtual memory, keeping too many processes in main memory will deteriorate the systems performance u ex: coexistence of several processes that do not use enough the CPU (e.g. are all blocked) n The OS may need to suspend some processes to bring in others, ie: to swap them out to auxiliary memory (disk). We add 2 new states, which double Blocked and Ready: u Blocked Suspend: blocked processes which have been swapped out to disk u Ready Suspend: ready processes which have been swapped out to disk

20 Chapter 3 20 New state transitions (medium-term scheduling) n Blocked --> Blocked Suspend u When all processes are blocked, the OS may remove a blocked process to bring a ready process in memory n Blocked Suspend --> Ready Suspend u When the event for which process has been waiting occurs n Ready Suspend --> Ready u When no ready processes in main memory u Normally, this transition is paired with Blocked --> Blocked suspend for another process (memory space must be found) n Ready--> Ready Suspend u When there are no blocked processes and must free memory for adequate performance

21 Chapter 3 21 A Seven-state Process Model

22 Chapter 3 22 Operating System Control Structures n An OS maintains the following tables for managing processes and resources: u Memory occupation tables (see later) u I/O tables (see later) u File tables (see later) u Process tables (this chapter) n See detailed discussion in texbook

23 Chapter 3 23

24 Chapter 3 24 Process Image n The information that the OS requires in order to manage the process u User program u User data u System and user stack(s) F for calls, returns and parameter passing u Process Control Block (execution context) F Data needed (process attributes) by the OS to suspend/resume the process. This includes: Process identification information Processor state information Process control information

25 Chapter 3 25 Process images

26 Chapter 3 26 Location of the Process Image n The location of each process image is pointed to by an entry in the Primary Process Table

27 Chapter 3 27

28 Chapter 3 28 What needs to be saved and when n Mode switching: process is suspended (e.g. to process interrupt) but immediately after this it resumes execution on the same CPU: u only information necessary to resume execution of the same process needs to be saved (e.g. program counter) n Process switching: process is suspended and another process will take the CPU: u more may need to be saved: context, data... u also process state must be updated.

29 Chapter 3 29 Process Identification (in the PCB) n A few numeric identifiers may be used u Unique process identifier (always) u User identifier F the user who is responsible for the process u Identifier of the process that created this process

30 Chapter 3 30 Processor State Information (in PCB) n Contents of processor registers u User-visible registers u Control and status registers: program counter... u Stack pointers n Program status word (PSW) u contains status information u Example: the EFLAGS register on Pentium machines

31 Chapter 3 31 Process Control Information (in PCB) n scheduling and state information u Process state (ie: running, ready, blocked...) u Priority of the process u Event for which the process is waiting (if blocked) n data structuring information u may hold pointers to other PCBs for process queues, parent-child relationships and other structures

32 Chapter 3 32 Queues as linked lists of PCBs

33 Chapter 3 33 Process Control Information (in PCB) n interprocess communication u may hold flags and signals for IPC n process privileges u Ex: access to certain memory locations... n memory management u pointers to segment/page tables assigned to this process n resource ownership and utilization u resource in use: open files, I/O devices... u history of usage (of CPU time, I/O...)

34 Chapter 3 34 Modes of Execution n To provide protection to PCBs (and other OS data) most processors support at least 2 execution modes: u Privileged mode (a.k.a. system mode, kernel mode, supervisor mode, control mode, master mode... ) F manipulating control registers, primitive I/O instructions, memory management... u User mode (a.k.a. slave mode) n For this the CPU provides a (or a few) mode bit which may only be set by an interrupt or trap or OS call

35 Chapter 3 35 Process Creation n Assign a unique process identifier n Allocate space for the process image n Initialize process control block u default values are used (ex: state is New, no I/O devices or files...) n Set up appropriate linkages u Ex: add new process to linked list used for the scheduling queue n Establish or enlarge some data structures u accounting info

36 Chapter 3 36 When to Switch a Process ? n A process switch may occur whenever the OS has gained control of CPU. ie when: u Supervisor Call F explicit request by the program (ex: I/O). The process will probably be blocked u Trap F An error resulted from the last instruction. It may cause the process to be moved to exit state u Interrupt F the cause is external to the execution of the current instruction. Control is transferred to IH

37 Chapter 3 37 Examples of interruptions n Timers u process has used up its time - transfer to ready state n I/O completion u transfer process to ready state u CPU scheduler will decide when to resume it n Memory fault, page fault u In virtual memory systems, process requests a page that is not in memory u block process, read page in memory

38 Chapter 3 38 Mode Switching n It is possible that an interrupt does not produce a process switch n Control can immediately return to the interrupted program after executing the IH n Then only the processor state information needs to be saved (ref. Chap 1) n This is called mode switching (user to kernel mode when going into IH) n Less overhead: no need to use the PCB as for process switching

39 Chapter 3 39 Steps in Process (Context) Switching n Save context of CPU including program counter and other registers n Update the PCB of the running process with its new state and other associate info n Move PCB to appropriate queue - ready, blocked n Select another process for execution n Update PCB of the selected process n Restore CPU context from PCB of the selected process

40 Chapter 3 40 Simple Interrupt Processing Save Process Control Block Restore Process Control Block

41 Chapter 3 41 Execution of the Operating System n Up to now, by process we were referring to user process n If the OS is just like any other collection of programs, is the OS a process? n If so, how it is controlled? n The answer depends on the OS design.

42 Chapter 3 42 Nonprocess Kernel (old) n The concept of process applies only to user programs n OS code is executed as a separate entity in privilege mode n OS code never gets executed within a process

43 Chapter 3 43 Execution within User Processes n OS operates mostly as it was a regular user, in fact OS function appear as functions of the users that invoke them: shared address space n Virtually all OS code gets executed within the context of a user process n On Interrupts, Traps, System calls: CPU switches to kernel mode to execute OS routine within the context of user process (mode switch) n Control passes to process switching functions (outside processes) only when needed (eg: interprocess synchronization, process scheduling…)

44 Chapter 3 44 Execution within User Processes n OS code and data are in the shared address space and are shared by all user processes n Separate kernel stack for calls/returns when the process is in kernel mode n Within a user process, both user and OS programs may execute (more than 1)

45 Chapter 3 45 Process-based Operating System n The OS is a collection of system processes, outside of users address space n Major kernel functions are separate processes n Process switching functions are executed outside of any process

46 Chapter 3 46 Important concepts of Chapter 3 n Process State n Waiting queues of professes n Process Image n Process Control Block n Process switching n Mode switching n Kernel and distribution of functionalities between users and kernel

47 Chapter 3 47 UNIX SVR4 Process management n Most of OS executes within user processes n Uses two categories of processes: u System processes F run in kernel mode for housekeeping functions (memory allocation, process swapping...) u User processes F run in user mode for user programs F run in kernel modes for system calls, traps, and interrupts

48 Chapter 3 48 UNIX SVR4 Process States n Similar to our 7 state model n 2 running states: User and Kernel u transitions to other states (blocked, ready) must come from kernel running n Sleeping states (in memory, or swapped) correspond to our blocking states n A preempted state is distinguished from the ready state (but they form 1 queue) n Preemption can occur only when a process is about to move from kernel to user mode

49 Chapter 3 49 UNIX Process State Diagram

50 Chapter 3 50 UNIX Process Creation n Every process, except process 0, is created by the fork() system call u fork() allocates entry in process table and assigns a unique PID to the child process u child gets a copy of process image of parent: both child and parent are executing the same code following fork() u but fork() returns the PID of the child to the parent process and returns 0 to the child process

51 Chapter 3 51 UNIX System Processes n Process 0 is created at boot time and becomes the swapper after forking process 1 (the INIT process) n When a user logs in: process 1 creates a process for that user

52 Chapter 3 52 UNIX Process Image n User-level context u Process Text (ie: code: read-only) u Process Data u User Stack (calls/returns in user mode) u Shared memory (for IPC) F only one physical copy exists but, with virtual memory, it appears as it is in the processs address space n Register context

53 Chapter 3 53 UNIX Process Image n System-level context u Process table entry F the actual entry concerning this process in the Process Table maintained by OS Process state, UID, PID, priority, event awaiting, signals sent, pointers to memory holding text, data... u U (user) area F additional process info needed by the kernel when executing in the context of this process effective UID, timers, limit fields, files in use... u Kernel stack (calls/returns in kernel mode) u Per Process Region Table (used by memory manager)

Download ppt "Chapter 3 1 Process Description and Control Chapter 3."

Similar presentations

Ads by Google