1 CHAPTER 2: PROCESS MANAGEMENT Slides by: Ms. Shree Jaswal
TOPICS TO BE COVERED Process description: Process, Process States, Process Control Block (PCB), Threads, Thread management. Process Scheduling: Types, Comparison of different scheduling policies. 2
PROCESS CONCEPT An operating system executes a variety of programs: Batch system jobs Time-shared systems user programs or tasks Textbook uses the terms job and process almost interchangeably Process a program in execution; process execution must progress in sequential fashion A process includes: program counter stack data section 3
PROCESS IN MEMORY 4
PROCESS STATE As a process executes, it changes state new: The process is being created running: Instructions are being executed waiting: The process is waiting for some event to occur ready: The process is waiting to be assigned to a processor terminated: The process has finished execution 5
DIAGRAM OF PROCESS STATE 6
PROCESS CONTROL BLOCK (PCB) Information associated with each process Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information 7
PROCESS CONTROL BLOCK (PCB) 8
CPU SWITCH FROM PROCESS TO PROCESS 9
PROCESS SCHEDULING QUEUES Job queue (short term queue) set of all processes in the system Ready queue (long term queue) set of all processes residing in main memory, ready and waiting to execute Device queues set of processes waiting for an I/O device Processes migrate among the various queues 10
READY QUEUE AND VARIOUS I/O DEVICE QUEUES 11
REPRESENTATION OF PROCESS SCHEDULING 12
SCHEDULERS Long-term scheduler (or job scheduler) selects which processes should be brought into the ready queue Short-term scheduler (or CPU scheduler) selects which process should be executed next and allocates CPU 13
ADDITION OF MEDIUM TERM SCHEDULING 14
SCHEDULERS (CONT.) Short-term scheduler is invoked very frequently (milliseconds) (must be fast) Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow) The long-term scheduler controls the degree of multiprogramming 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 15
CONTEXT SWITCH 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-switch time is overhead; the system does no useful work while switching Time dependent on hardware support 16
PROCESS CREATION 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 parent s resources Parent and child share no resources Execution Parent and children execute concurrently Parent waits until children terminate 17
PROCESS CREATION (CONT.) 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 18
PROCESS CREATION 19
C PROGRAM FORKING SEPARATE PROCESS int main() { pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf ("Child Complete"); exit(0); } } 20
A TREE OF PROCESSES ON A TYPICAL SOLARIS 21
PROCESS TERMINATION Process executes last statement and asks the operating system to delete it (exit) Output data from child to parent (via wait) Process resources are deallocated by operating system 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 22
COOPERATING PROCESSES Independent process cannot affect or be affected by the execution of another process Cooperating process can affect or be affected by the execution of another process Advantages of process cooperation Information sharing Computation speed-up Modularity Convenience 23
PRODUCER-CONSUMER PROBLEM Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process unbounded-buffer places no practical limit on the size of the buffer bounded-buffer assumes that there is a fixed buffer size 24
PROCESS SCHEDULING Maximum CPU utilization obtained with multiprogramming CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait CPU burst distribution 25
ALTERNATING SEQUENCE OF CPU AND I/O BURSTS 26
HISTOGRAM OF CPU-BURST TIMES 27
CPU SCHEDULER Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state 2. Switches from running to ready state 3. Switches from waiting to ready 4. Terminates Scheduling under 1 and 4 is nonpreemptive All other scheduling is preemptive 28
DISPATCHER Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: switching context switching to user mode jumping to the proper location in the user program to restart that program Dispatch latency time it takes for the dispatcher to stop one process and start another running 29
SCHEDULING CRITERIA CPU utilization keep the CPU as busy as possible Throughput # of processes that complete their execution per time unit Turnaround time amount of time to execute a particular process Waiting time amount of time a process has been waiting in the ready queue Response time amount of time it takes from when a request was submitted until the first response is produced, not output (for timesharing environment) 30
OPTIMIZATION CRITERIA Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time 31
FIRST-COME, FIRST-SERVED (FCFS) SCHEDULING Process Burst Time P 1 24 P 2 3 P 3 3 Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: 0 P 1 P 2 P 3 24 27 30 Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 32
FCFS SCHEDULING (CONT.) Suppose that the processes arrive in the order P 2, P 3, P 1 The Gantt chart for the schedule is: 0 P 2 P 3 Waiting time for P 1 = 6; P 2 = 0 ; P 3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case Convoy effect short process behind long process P 1 3 6 30 33
SHORTEST-JOB-FIRST (SJF) SCHEDULING Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time Two schemes: nonpreemptive once CPU given to the process it cannot be preempted until completes its CPU burst preemptive if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF) SJF is optimal gives minimum average waiting time for a given set of processes 34
EXAMPLE ProcessArrival TimeBurst Time P 1 0.0 7 P 2 2.0 4 P 3 4.0 1 P 4 5.0 4 35
EXAMPLE OF NON-PREEMPTIVE SJF Process Arrival Time Burst Time P 1 0.0 7 P 2 2.0 4 P 3 4.0 1 P 4 5.0 4 SJF (non-preemptive) P 1 P 3 P 2 0 3 7 8 12 16 Average waiting time = (0 + 6 + 3 + 7)/4 = 4 P 4 36
EXAMPLE OF PREEMPTIVE SJF Process SJF (preemptive) 0 Arrival Time Burst Time P 1 0.0 7 P 1 P 2 P 3 P 2 2.0 4 P 3 4.0 1 P 4 5.0 4 P 2 P 4 P 1 2 4 5 7 11 Average waiting time = (9 + 1 + 0 +2)/4 = 3 16 37
PRIORITY SCHEDULING A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer highest priority) Preemptive nonpreemptive SJF is a priority scheduling where priority is the predicted next CPU burst time Problem Starvation low priority processes may never execute Solution Aging as time progresses increase the priority of the process 38
ROUND ROBIN (RR) Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units. Performance q large FIFO q small q must be large with respect to context switch, otherwise overhead is too high 39
EXAMPLE OF RR WITH TIME QUANTUM = 20 Process Burst Time P 1 53 P 2 17 P 3 68 P 4 24 The Gantt chart is: P 1 P 2 P 3 P 4 P 1 P 3 P 4 P 1 P 3 P 3 0 20 37 57 77 97 117 121 134 154 162 Typically, higher average turnaround than SJF, but better response 40
TIME QUANTUM AND CONTEXT SWITCH TIME A rule of thumb is that 80% of CPU bursts should be shorter than the time quantum 41
TURNAROUND TIME VARIES WITH THE TIME QUANTUM 42
MULTILEVEL QUEUE Ready queue is partitioned into separate queues: foreground (interactive) background (batch) Each queue has its own scheduling algorithm foreground RR background FCFS Scheduling must be done between the queues Fixed priority scheduling among queues; (i.e., serve all from foreground then from background). Possibility of starvation. Time slice each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR, 20% to background in FCFS 43
MULTILEVEL QUEUE SCHEDULING 44
MULTILEVEL FEEDBACK QUEUE A process can move between the various queues according to the characteristics of their CPU bursts times. Aging can be implemented to prevent starvation. Multilevel-feedback-queue scheduler defined by the following parameters: number of queues scheduling algorithms for each queue method used to determine when to upgrade a process method used to determine when to demote a process method used to determine which queue a process will enter when that process needs service 45
EXAMPLE OF MULTILEVEL FEEDBACK QUEUE Three queues: Q 0 RR with time quantum 8 milliseconds Q 1 RR time quantum 16 milliseconds Q 2 FCFS Scheduling A new job enters queue Q 0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1. At Q 1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2. 46
MULTILEVEL FEEDBACK QUEUES 47
MULTIPLE-PROCESSOR SCHEDULING CPU scheduling more complex when multiple CPUs are available Homogeneous processors within a multiprocessor Load sharing Asymmetric multiprocessing only one processor accesses the system data structures, alleviating the need for data sharing 48
REAL-TIME SCHEDULING Hard real-time systems required to complete a critical task within a guaranteed amount of time Soft real-time computing requires that critical processes receive priority over less fortunate ones 49
Processes and Threads Process has following 2 characteristics: Resource ownership - process includes a virtual address space to hold the process image Scheduling/execution- follows an execution path that may be interleaved with other processes These two characteristics are treated independently by the operating system 50
Terminology Unit of Dispatching is referred to as a thread or lightweight process Unit of Resource ownership is referred to as a process or task 51
Process = Threads resource grouping (code, data, open files, etc.) + execution (program counter, registers, stack) Thread = a flow of control within a process Multithreading: multiple execution takes place in the same process environment co-operation by sharing resources (address space, open files, etc.) 52
The Thread Model 53
Threads 54
SINGLE AND MULTITHREADED PROCESSES 55
BENEFITS Responsiveness Takes less time to create a new thread than a process Less time to terminate a thread than a process Less time to switch between two threads within the same process 56
BENEFITS Resource Sharing & Economy Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel Scalability: Multithreading on a multiple-cpu machine increases parallelism 57
A word processor with three threads 58
A multithreaded Web server 59
User-Level Threads 60
User-Level Threads All thread management is done by the application The kernel is not aware of the existence of threads Blocking system call! 61
USER THREADS Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads 62
Kernel-Level Threads Windows is an example of this approach Kernel maintains context information for the process and the threads Scheduling is done on a thread basis 63
Kernel-Level Threads 64
KERNEL THREADS Supported by the Kernel Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X 65
Thread States 66
MULTITHREADING MODELS Many-to-One One-to-One Many-to-Many 67
MANY-TO-ONE Many user-level threads mapped to single kernel thread True concurrency is not gained as kernel can schedule only 1 thread at a time Examples: Solaris Green Threads GNU Portable Threads 68
ONE-TO-ONE Each user-level thread maps to kernel thread Allows greater concurrency but developer has to be careful not to create too many threads in an application Examples Windows NT/XP/2000 Linux Solaris 9 and later 69
MANY-TO-MANY MODEL Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris prior to version 9, Windows NT/2000 with the ThreadFiber package 70
DIFFERENCE B/W PROCESS AND PROGRAM: A Program would be executed by specific way/method called process, specific program has specific process. So a program itself is not a process. A program is a passive entity. For example, contents of a file stored on disk. A process is a active entity. For example,a program counter specifying the next instruction to execute and a set of associated resources. Two process may be associated with the same program at a time, they are not considered two separate execution sequences. 71
TYPE OF PROCESSING (TASKING) Tasking refers to the technique involved in creating the process and to assign process attributes. The attributes are current state of the process, program counter, CPU registers etc. There are two types of Tasking: Implicit tasking (System defined) Explicit tasking (programmer defined) 72
TYPE OF PROCESSING (TASKING) Implicit tasking: In this tasking process is defined by the system, it is used in time sharing systems. For example: a separate process may be created for each of the job activities-including compilation, linking and execution of the user program, on same operating system. Explicit tasking: In this type of tasking the programmer explicitly defines each process and some of its attributes, (for explicit control of system activities and high performance). OS and application program are common examples of programmer-defined processes. 73
REASONS FOR EXPLICIT TASKING They are as follows:- It speeds up the execution of an application. It provides user convenience by creating tasks to handle individual actions. It uses multiprocessor system. So, more than one process can execute at a time. It uses distributed computing system. 74
75 THANK YOU!