COM 1032 MOBILE COMPUTING 20190 Lecture 2 Process
COM 1032: MOBILE COMPUTING 2019/0 Lecture 2 Process Description, Control, and Scheduling Dr. Manal Helal
From Introduction to Android™ Application Development, Android Essentials, Fiftth Edition, by Joseph Annuzzi, Jr. , Lauren Darcey, and Shane Conder (ISBN 13: 978 -0 -134 -38945 -5) Copyright © 2016 Pearson Education, Inc. All rights reserved.
Lecture 1 Summary: OS Management of Application Execution § § Resources are made available to multiple applications residing in memory The processor is switched among multiple applications so all will appear to be progressing The processor and I/O devices can be used efficiently OS is structured into monolithic code, kernel, microkernel, hybrid-kernel, and various modular models. © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Learning Outcomes Attributes Developed 001 Process States KC 002 Process Control Block KC 003 Process Privileges KC 004 Process Scheduling KC 005 IPC K Attributes Developed C - Cognitive/analytical K - Subject knowledge T - Transferable skills P - Professional/Practical skills
Outline § § § What is a process? Process states Process control Execution of the operating system Scheduling Types © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. § § Scheduling Criteria Uni-Processor Scheduling Unix/Windows/Android Scheduling Minix Introduction
Process Elements § Two essential elements of a process are: Program code • which may be shared with other processes that are executing the same program A set of data associated with that code • when the processor begins to execute the program code, we refer to this executing entity as a process © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Process Control Block (PCB) Elements: § § § The PCB Contains the process elements It is possible to interrupt a running process and later resume execution as if the interruption had not occurred Created and managed by the operating system Key tool that allows support for multiple processes While the program is executing, this process can be uniquely characterized by a number of elements, shown in figure. © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Outline § § § What is a process? Process states Process control Execution of the operating system Scheduling Types © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. § § Scheduling Criteria Uni-Processor Scheduling Unix/Windows/Android Scheduling Minix Introduction
Process States Trace Dispatcher The behavior of an individual process by listing the sequence of instructions that execute for that process The behavior of the processor can be characterized by showing how the traces of the various processes are interleaved © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. Small program that switches the processor from one process to another
Process Execution © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
© 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Table 3. 1 Reasons for Process Creation © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Process Termination § § There must be a means for a process to indicate its completion A batch job should include a HALT instruction or an explicit OS service call for termination For an interactive application, the action of the user will indicate when the process is completed (e. g. log off, quitting an application) Reasons for Process Termination in Table 3. 2, includes: Normal Completion, Time-out, Memory Unavailable, I/O Failure, among others. (Table is located on page 111 in the textbook) © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Five-State Process Model © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
© 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Table 3. 3 Reasons for Process Suspension © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Outline § § § What is a process? Process states Process control Execution of the operating system Scheduling Types © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. § § Scheduling Criteria Uni-Processor Scheduling Unix/Windows/Android Scheduling Minix Introduction
© 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Table 3. 4 Typical Elements of a Process Image © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Table 3. 5 Typical Elements of a Process Control Block (page 1 of 2) (Table is located on page 125 in the textbook) © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Table 3. 5 Typical Elements of a Process Control Block (page 2 of 2) (Table is located on page 125 in the textbook) © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
© 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
© 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Role of the Process Control Block § § The most important data structure in an OS – Contains all of the information about a process that is needed by the OS – Blocks are read and/or modified by virtually every module in the OS – Defines the state of the OS Difficulty is not access, but protection – A bug in a single routine could damage process control blocks, which could destroy the system’s ability to manage the affected processes – A design change in the structure or semantics of the process control block could affect a number of modules in the OS © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Outline § § § What is a process? Process states Process control Execution of the operating system Scheduling Types © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. § § Scheduling Criteria Uni-Processor Scheduling Unix/Windows/Android Scheduling Minix Introduction
Modes of Execution User Mode • • Less-privileged mode User programs typically execute in this mode © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. System Mode • More-privileged mode • Also referred to as control mode or kernel mode • Kernel of the operating system
Process Creation § Once the OS decides to create a new process it: Assigns a unique process identifier to the new process Allocates space for the process Initializes the process control block Sets the appropriate linkages Creates or expands other data structures © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Table 3. 8 Mechanisms for Interrupting the Execution of a Process © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
System Interrupts Interrupt • Due to some sort of event that is external to and independent of the currently running process – Clock interrupt – I/O interrupt – Memory fault • Time slice – The maximum amount of time that a process can execute before being interrupted © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. Trap • An error or exception condition generated within the currently running process • OS determines if the condition is fatal – Moved to the Exit state and a process switch occurs – Action will depend on the nature of the error the design of the OS
Mode Switching If no interrupts are pending the processor: If an interrupt is pending the processor: Proceeds to the fetch stage and fetches the next instruction of the current program in the current process Sets the program counter to the starting address of an interrupt handler program Switches from user mode to kernel mode so that the interrupt processing code may include privileged instructions © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Change of Process State § The steps in a full process switch are: Save the context of the processor Update the process control block of the process currently in the Running state If the currently running process is to be moved to another state (Ready, Blocked, etc. ), then the OS must make substantial changes in its environment Restore the context of the processor to that which existed at the time the selected process was last switched out © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. Update memory management data structures Move the process control block of this process to the appropriate queue Select another process for execution Update the process control block of the process selected
Execution of the Operating System © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Outline § § § What is a process? Process states Process control Execution of the operating system Scheduling Types © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. § § Scheduling Criteria Uni-Processor Scheduling Unix/Windows/Android Scheduling Minix Introduction
Processor Three Level Scheduling (Batch) Short term scheduling Long term scheduling Intermediate/ Medium Term I/O scheduling
© 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Outline § § § What is a process? Process states Process control Execution of the operating system Scheduling Types © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. § § Scheduling Criteria Uni-Processor Scheduling Unix/Windows/Android Scheduling Minix Introduction
Short Term Scheduling Criteria § Main objective is to allocate processor time to optimize certain aspects of system behavior A set of criteria is needed to evaluate the scheduling policy § • • Performance Related: • Examples: Response time and throughput • Quantitative • Easily Measured Non-performance Related: • • • Examples: Predictability Qualitative Hard to Measure © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. User-oriented criteria • Relate to the behavior of the system as perceived by the individual user or process (such as response time in an interactive system) • Important on virtually all systems System-oriented criteria • Focus is on effective and efficient utilization of the processor (rate at which processes are completed) • Generally of minor importance on single-user systems
Competing performance objectives § CPU utilization. § Minimize the total amount of time spent in the ready queue Keep the CPU as busy as possible § Throughput. Maximize the number of processes completed in a unit of time § Turnaround Time. Minimize the time it takes a process to execute Waiting Time. § Response Time. (for interactive jobs) Minimize the amount of time from the submission of a job to the first response. Table 9. 2 Scheduling Criteria
CPU Scheduling Goals
From Introduction to Android™ Application Development, Android Essentials, Fiftth Edition, by Joseph Annuzzi, Jr. , Lauren Darcey, and Shane Conder (ISBN 13: 978 -0 -134 -38945 -5) Copyright © 2016 Pearson Education, Inc. All rights reserved.
Outline § § § What is a process? Process states Process control Execution of the operating system Scheduling Types © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. § § Scheduling Criteria Uni-Processor Scheduling Unix/Windows/Android Scheduling Minix Introduction
CPU scheduling and process states • When the CPU becomes idle the short-term scheduler is invoked. • It selects a process from the ready queue. • Then the dispatcher assigns the CPU to the chosen process. Its functions include: • context switching • switching from kernel to user mode • branching to the proper place in the user process new terminated job sched interrupt ready running cpu sched I/O or event complete I/O or event waiting
§ Determines which process, among ready processes, is selected next for execution § May be based on priority, resource requirements, or the execution characteristics of the process § If based on execution characteristics, then important quantities are: § w = time spent in system so far, waiting § e = time spent in execution so far § s = total service time required by the process, including e; generally, this quantity must be estimated or supplied by the user © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
§ Specifies the instants in time at which the selection function is exercised § Process created § Process terminated § Process blocked § Process interrupted for I/O § Two categories: § Non-preemptive § Process remains in the CPU until blocked or terminated § Preemptive § The OS moves a running process to the ready state for a higher priority process, time sharing, and interrupt handling Which has more overhead? © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Scheduling algorithm categories § Batch scheduling – – – § Interactive scheduling – – – § First-Come, First-Served (FCFS) Shortest Job First (SJF) Shortest Remaining Time (SRT) Round-Robin Priority Scheduling Multilevel Feedback Queues (MLFQ) Real-Time scheduling – Data must be processed within a given time frame or the system is worthless
Analogy: Waiting to photocopy credit: John Estell, Bluffton College § § § In an office, we have several people and one photocopier. Each person has a variety of items to photocopy - some have one page, others a few pages out of several books, and there also those who want to copy an entire book. How should we allocate access to the photocopier?
An individual represents a process § A variety of processes: – short processes --- copying one page – long processes --- copying an entire book – CPU-bound processes (performing many computations without interruption) • copying a sequence of pages from one book – I/O-bound processes (performing only a few computations before an interruption occurs) • copying a few pages each from several books, or single pages scattered throughout a single book
FCFS (Batch) Whoever arrived first gets to use the machine to make as many copies as desired.
FCFS § § Non-preemptive Processes are assigned CPU in the order in which they request it. § Easy to implement. – The ready queue is FIFO. § What are its weaknesses? – Tends to favor processor-bound processes and over I/O-bound processes – FCFS performs much better for long processes than short ones.
Process P 1 P 2 P 3 P 1 FCFS 0 CPU burst 24 3 3 P 2 P 3 24 27 30 Average waiting Time = (0 + 24 + 27)/3 = 17 msec If the processes arrive in order P 2, P 3, P 1 we have: P 2 0 P 3 3 P 1 6 Average waiting Time = (0 + 3 + 6)/3 = 3 msec 30
SJF (Batch) “ 3” goes next! dispatcher “ 5” “ 32” “ 28” “ 31” “ 26” “ 11” “ 28”
§ § § Non-preemptive Processes are assigned the CPU on the basis of the length of their next CPU bursts – This algorithm should really be called “shortest next burst” Pros: Theoretically optimal when all processes are available at the same time Cons: – Possibility of starvation for longer processes – One difficulty is the need to know, or at least estimate, the required processing time of each process – If the programmer’s estimate is substantially under the actual running time, the system may abort the job Shortest Process Next (SPN) is the interactive version of SJF. – Each command is considered as a process – The necessary processing time for each command is estimated. Shortest Remaining Time (SRT) is the preemptive version of SJF
§ § § Preemptive version of SPN Scheduler always chooses the process that has the shortest expected remaining processing time Risk of starvation of longer processes © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. § Should give superior turnaround time performance to SPN because a short job is given immediate preference to a running longer job
Process P 1 P 2 P 3 P 4 SJF P 4 0 P 1 3 Burst Time 6 8 7 3 P 3 9 P 2 16 Average waiting time = (0 + 3 + 9 + 16)/4 = 7 msec With FCFS scheduling the average waiting time would be 10. 25 milliseconds - try it out! 24
N copies at a time! Dispatcher Round Robin (interactive)
Round-Robin § § § Preemptive FCFS: preemption is based on a clock – A time slice or quantum q, 20 q 50 msec – The next process in the ready queue gets up to q msec of CPU time. • If the CPU burst of the process < q, it voluntarily relinquishes the CPU • If it is > q, a timer goes off, the CPU is interrupted and the process is preempted and put at the end of the ready queue. The next process at the head of the queue is gets the CPU. Pros: Particularly effective in a general-purpose time-sharing system or transaction processing system Cons: One drawback is its relative treatment of processorbound and I/O-bound processes
Process P 1 P 2 P 3 Round Robin Burst Time 24 3 3 Time quantum is 4 P 1 0 P 3 P 2 4 7 P 1 10 P 1 14 P 1 18 Average waiting time = (0 + 4 + 7 + 6)/3 = 5. 66 msec Recall that for these same processes with no preemption the average waiting time was 17 msec P 1 22 P 1 26 30
RR performance depends on the size of the time quantum § § § If it is very large, it is the same as having no preemption (i. e. FCFS) If it is very small, there are many context switches and valuable CPU time is spent swapping processes in and out A rule of thumb is that 80% of the CPU bursts should be shorter than the quantum
§ § Chooses next process with the greatest ratio Attractive because it accounts for the age of the process © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. § While shorter jobs are favored, aging without service increases the ratio so that a longer process will eventually get past competing shorter jobs
Priority (interactive) The“ 2” boss goes “ 5” “ 32” “ 28” “ 3” next! “ 200” Boss “ 31” “ 26” “ 11” “ 28”
Priority Scheduling § § § Some processes are deemed more important than others – So, processes are assigned numbers indicating their relative priority Preemptive or non-preemptive Static or dynamic SJF is a priority scheduling algorithm – the length of the job determines the priority level. How can starvation be avoided?
“ 3” goes next! MLFQ (interactive) max = 10 max = 20 max = 30
MLFQ § Implemented as multiple level Round-Robin queues: – – § § the highest level has smallest quantum size a process enters the ready queue at the highest level; if it does not complete the first time it gets the CPU, it returns to the ready queue one level down I/O bound and interactive processes usually complete execution after one time in the CPU-bound processes that wait too long in lower priority queues may be promoted to prevent starvation
MLFQ with four classes q = 1 q = 2 q = 4 q = 8 System processes Interactive editing processes Batch processes • Each queue has absolute priority over lower queues • no batch process can run unless upper queues are empty • If a higher priority process enters a queue while a batch process is running, the batch process may be preempted.
Table 9. 3 Characteristics of Various Scheduling Policies (Table can be found on page 405 in textbook) © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Outline § § § What is a process? Process states Process control Execution of the operating system Scheduling Types © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. § § Scheduling Criteria Uni-Processor Scheduling Unix/Windows/Android Scheduling Minix Introduction
Windows NT CPU scheduling § § The scheduler runs in the kernel. It is time-sliced (ie, round robin) 20 msec <= quantum <= 200 msec Servers have 6 * quantum of workstations § It is priority based – – § § real time, high, normal, idle priority classes Threads inherit their process priority & also have relative priorities within their process. It is preemptive MLFQ – 32 queues, with absolute priority from top to bottom
Linux CPU scheduling § Threads are implemented at the kernel level – Scheduling is based on threads, not processes § Three classes of threads Priority decreases downward § – SCHED_FIFO: First-in-first-out real-time threads – SCHED_RR: Round-robin real-time threads – SCHED_NORMAL: Other, non-real-time threads Within each class multiple priorities may be used, with priorities in the real-time classes higher than the priorities for the SCHED_NORMAL class
Completely Fair Scheduler (CFS) § § § Used as a result of the drawbacks of the O(1) scheduler Models an ideal multitasking CPU on real hardware that provides fair access to all tasks In order to achieve this goal, the CFS maintains a virtual runtime for each task • • § § The virtual runtime is the amount of time spent executing so far, normalized by the number of runnable processes The smaller a task’s virtual runtime is, the higher is its need for the processor Includes the concept of sleeper fairness to ensure that tasks that are not currently runnable receive a comparable share of the processor when they eventually need it Implemented by the fair_sched_class scheduler class © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved.
Android Scheduling § § An Android system will have a set of unix processes running. Some are native processes but many will be processes that run a Java virtual machine. – Android uses two different scheduling classes (using linux cgroups) bg_non_interactive and default (foreground)” • The configuration is that bg_non_interactive is low priority and can maximum utilize ~5% of the cpu (including all background tasks) • Activities are always foreground or a foreground service utulise ~95% of the CPU. – The binder mechanism also propagates priorities. That is the binder process called will run with the same priority as the caller. JVM processes These processes usually will be multi threaded, All android threads are native pthreads (no green threads) – Thread. set. Priority uses a value from MIN_PRIORITY(19) to MAX_PRIORITY(-8)
Outline § § § What is a process? Process states Process control Execution of the operating system Scheduling Types © 2017 Pearson Education, Inc. , Hoboken, NJ. All rights reserved. § § Scheduling Criteria Uni-Processor Scheduling Unix/Windows/Android Scheduling Minix Introduction
§ The Internal Structure of Minix is structured in four layers, in which Servers run at the same privilege level as user processes but at higher priority. User processes (Init) Server processes (PM, MM, FS) I/O tasks and Device Drivers Kernel, Clock and System Task Q: What is the difference between the privilege and priority? Process management in Minix 73
Processes Management in Minix § § All user processes in the system are descendants of a process called init. The Minix boot program loads into memory: • the kernel • the memory manager • the file system • and init (the first user process) Process management in Minix 74
Interprocess Communication § Processes communicate in Minix via message passing. § Three primivites are provided for sending and receiving messages: – send (dest, &message) – receive(source, &message) – send_rec(src_dst, &message) Process management in Minix 75
Synchronous and Asynchronous Message Passing § § When a process sends a message to a process that is not currently waiting to receive the message, the sender blocks until the destination does RECEIVE. § Q. WHY? Q. What is the difference between synchronous and asynchronous message passing? Process management in Minix 76
Process Scheduling in Minix § § § Minix uses multilevel queue scheduling algorithm. There are three levels corresponding to layers 2, 3 and 4 of the Minix structure Tasks and server processes run until they block (FCFS). User processes use Round Robin with quantum 100 msec. Question: – What are the values of NR_TASK and NR_PROCS? – Look at include/minix/const. h and include/minic/config. h Process management in Minix 77
Summary § § § § Process is a program in execution. Program is a static entity while process is an active entity. Process Control Block (PCB) is a data structure that holds essential process information. Process table is and array or link list of PCBs. Scheduling Criteria Scheduling Algorithms Unix/Windows/Android and Minix Scheduling Process management in Minix 78
- Slides: 78