Operating Systems Processes Operating System Concepts 8 th

  • Slides: 40
Download presentation
Operating Systems Processes Operating System Concepts – 8 th Edition, Silberschatz, Galvin and Gagne

Operating Systems Processes Operating System Concepts – 8 th Edition, Silberschatz, Galvin and Gagne © 2009

What is a process? n An instance of an application execution n Process is

What is a process? n An instance of an application execution n Process is the most basic abstractions provided by OS l An isolated computation context for each application n Computation context l CPU state + address space + environment Operating System Concepts – 8 th Edition 3. 2 Silberschatz, Galvin and Gagne © 2009

What’s “in” a process? n A process consists of (at least): l l An

What’s “in” a process? n A process consists of (at least): l l An address space, containing 4 the code (instructions) for the running program 4 the data for the running program (static data, heap data, stack) CPU state, consisting of 4 The program counter (PC), indicating the next instruction 4 The stack pointer 4 Other l general purpose register values A set of OS resources 4 open files, network connections, sound channels, … n In other words, it’s all the stuff you need to run the program l or to re-start it, if it’s interrupted at some point Operating System Concepts – 8 th Edition 3. 3 Silberschatz, Galvin and Gagne © 2009

The OS’s process namespace n (Like most things, the particulars depend on the specific

The OS’s process namespace n (Like most things, the particulars depend on the specific OS, but the principles are general) n The name for a process is called a process ID (PID) l An integer n The PID namespace is global to the system l Only one process at a time has a particular PID n Operations that create processes return a PID l E. g. , fork() n Operations on processes take PIDs as an argument l E. g. , kill(), wait(), nice() Operating System Concepts – 8 th Edition 3. 4 Silberschatz, Galvin and Gagne © 2009

Process State n As a process executes, it changes state l new: The process

Process State n As a process executes, it changes state l new: The process is being created l running: Instructions are being executed l waiting: The process is waiting for some event to occur l ready: The process is waiting to be assigned to a processor l terminated: The process has finished execution Operating System Concepts – 8 th Edition 3. 5 Silberschatz, Galvin and Gagne © 2009

Diagram of Process State Operating System Concepts – 8 th Edition 3. 6 Silberschatz,

Diagram of Process State Operating System Concepts – 8 th Edition 3. 6 Silberschatz, Galvin and Gagne © 2009

Process States terminated running schedule wait for event preempt created ready blocked event done

Process States terminated running schedule wait for event preempt created ready blocked event done Operating System Concepts – 8 th Edition 3. 7 Silberschatz, Galvin and Gagne © 2009

State queues n The OS maintains a collection of queues that represent the state

State queues n The OS maintains a collection of queues that represent the state of all processes in the system l typically one queue for each state 4 e. g. , ready, waiting, … l each PCB is queued onto a state queue according to the current state of the process it represents l as a process changes state, its PCB is unlinked from one queue, and linked onto another n Once again, this is just as straightforward as it sounds! The PCBs are moved between queues, which are represented as linked lists. There is no magic! Operating System Concepts – 8 th Edition 3. 8 Silberschatz, Galvin and Gagne © 2009

Process Creation n Parent process create children processes, which, in turn create other processes,

Process Creation n Parent process create children processes, which, in turn create other processes, forming a tree of processes n Generally, process identified and managed via a process identifier (pid) n Resource sharing l Parent and children share all resources l Children share subset of parent’s resources l Parent and child share no resources n Execution l Parent and children execute concurrently l Parent waits until children terminate Operating System Concepts – 8 th Edition 3. 9 Silberschatz, Galvin and Gagne © 2009

A tree of processes on a typical Solaris Operating System Concepts – 8 th

A tree of processes on a typical Solaris Operating System Concepts – 8 th Edition 3. 10 Silberschatz, Galvin and Gagne © 2009

Process Termination n Process executes last statement and asks the operating system to delete

Process Termination n Process executes last statement and asks the operating system to delete it (exit) l Process’ resources are de-allocated by operating system n Parent may terminate execution of children processes (abort) l Child has exceeded allocated resources l Task assigned to child is no longer required l If parent is exiting 4 Some operating system do not allow child to continue if its parent terminates – All children terminated - cascading termination Operating System Concepts – 8 th Edition 3. 11 Silberschatz, Galvin and Gagne © 2009

CPU state=Register contents n Process Status Word (PSW) l exec. mode, last op. outcome,

CPU state=Register contents n Process Status Word (PSW) l exec. mode, last op. outcome, interrupt level n Instruction Register (IR) l Current instruction being executed n Program counter (PC) n Stack pointer (SP) n General purpose registers Operating System Concepts – 8 th Edition 3. 12 Silberschatz, Galvin and Gagne © 2009

The PCB n The PCB is a data structure with many, many fields: l

The PCB n The PCB is a data structure with many, many fields: l process ID (PID) l parent process ID l execution state l program counter, stack pointer, registers l address space info l UNIX user id, group id l scheduling priority l accounting info l pointers for state queues n In Linux: l defined in task_struct (include/linux/sched. h) l over 95 fields!!! Operating System Concepts – 8 th Edition 3. 13 Silberschatz, Galvin and Gagne © 2009

Address space n Text l Program code n Data l Predefined data (known in

Address space n Text l Program code n Data l Predefined data (known in compile time) n Heap l Dynamically allocated data n Stack l Supporting Operating System Concepts – 8 th Edition function calls 3. 14 Silberschatz, Galvin and Gagne © 2009

Process Control Block (PCB) Information associated with each process n Process state n Program

Process Control Block (PCB) Information associated with each process n Process state n Program counter n CPU registers n CPU scheduling information n Memory-management information n Accounting information n I/O status information Operating System Concepts – 8 th Edition 3. 15 Silberschatz, Galvin and Gagne © 2009

Process Scheduling Queues n Job queue – set of all processes in the system

Process Scheduling Queues n Job queue – set of all processes in the system n Ready queue – set of all processes residing in main memory, ready and waiting to execute n Device queues – set of processes waiting for an I/O device n Processes migrate among the various queues Operating System Concepts – 8 th Edition 3. 16 Silberschatz, Galvin and Gagne © 2009

Ready Queue And Various I/O Device Queues Operating System Concepts – 8 th Edition

Ready Queue And Various I/O Device Queues Operating System Concepts – 8 th Edition 3. 17 Silberschatz, Galvin and Gagne © 2009

Representation of Process Scheduling Operating System Concepts – 8 th Edition 3. 18 Silberschatz,

Representation of Process Scheduling Operating System Concepts – 8 th Edition 3. 18 Silberschatz, Galvin and Gagne © 2009

Schedulers n Long-term scheduler (or job scheduler) – selects which processes should be brought

Schedulers n Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue (seconds, minutes) (may be slow) n Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU (milliseconds) (must be fast) Operating System Concepts – 8 th Edition 3. 19 Silberschatz, Galvin and Gagne © 2009

Context Switch n When CPU switches to another process, the system must save the

Context Switch n 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 via a context switch n Context of a process represented in the PCB n Context-switch time is overhead; the system does no useful work while switching n Time dependent on hardware support Operating System Concepts – 8 th Edition 3. 20 Silberschatz, Galvin and Gagne © 2009

CPU Switch From Process to Process Operating System Concepts – 8 th Edition 3.

CPU Switch From Process to Process Operating System Concepts – 8 th Edition 3. 21 Silberschatz, Galvin and Gagne © 2009

CPU Scheduler n Selects from among the processes in memory that are ready to

CPU Scheduler n Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them n 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 n Scheduling under 1 and 4 is nonpreemptive n All other scheduling is preemptive Operating System Concepts – 8 th Edition 3. 22 Silberschatz, Galvin and Gagne © 2009

When to assign? n Pre-emptive vs. non-preemptive schedulers l Non-preemptive 4 l once you

When to assign? n Pre-emptive vs. non-preemptive schedulers l Non-preemptive 4 l once you give somebody the green light, they’ve got it until they relinquish it – an I/O operation – allocation of memory in a system without swapping Preemptive 4 you can re-visit a decision – – 4 setting the timer allows you to preempt the CPU from a thread even if it doesn’t relinquish it voluntarily in any modern system, if you mark a program as non-runnable, its memory resources will eventually be re-allocated to others Re-assignment always involves some overhead – Overhead doesn’t contribute to the goal of any scheduler n We’ll assume “work conserving” policies l Never leave a resource idle when someone wants it 4 Why even mention this? When might it be useful to do something else? Operating System Concepts – 8 th Edition 3. 23 Silberschatz, Galvin and Gagne © 2009

Dispatcher n Dispatcher module gives control of the CPU to the process selected by

Dispatcher n Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: l switching context l switching to user mode l jumping to the proper location in the user program to restart that program n Dispatch latency – time it takes for the dispatcher to stop one process and start another running Operating System Concepts – 8 th Edition 3. 24 Silberschatz, Galvin and Gagne © 2009

Scheduling Criteria n CPU utilization – keep the CPU as busy as possible n

Scheduling Criteria n CPU utilization – keep the CPU as busy as possible n Throughput – # of processes that complete their execution per time unit n Turnaround time – amount of time to execute a particular process n Waiting time – amount of time a process has been waiting in the ready queue n Response time – amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment) Operating System Concepts – 8 th Edition 3. 25 Silberschatz, Galvin and Gagne © 2009

First-Come, First-Served (FCFS) Scheduling Process Burst Time P 1 24 P 2 3 P

First-Come, First-Served (FCFS) Scheduling Process Burst Time P 1 24 P 2 3 P 3 3 n Suppose that the processes arrive in the order: P 1 , P 2 , P 3 The Gantt Chart for the schedule is: P 1 P 2 0 24 P 3 27 30 n Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 n Average waiting time: (0 + 24 + 27)/3 = 17 Operating System Concepts – 8 th Edition 3. 26 Silberschatz, Galvin and Gagne © 2009

Shortest-Job-First (SJF) Scheduling n Associate with each process the length of its next CPU

Shortest-Job-First (SJF) Scheduling n Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time n SJF is optimal – gives minimum average waiting time for a given set of processes l The difficulty is knowing the length of the next CPU request Operating System Concepts – 8 th Edition 3. 27 Silberschatz, Galvin and Gagne © 2009

Example of SJF Process Arrival Time Burst Time P 1 0. 0 6 P

Example of SJF Process Arrival Time Burst Time P 1 0. 0 6 P 2 0. 0 8 P 3 0. 0 7 P 4 0. 0 3 n SJF scheduling chart P 4 0 P 3 P 1 3 9 P 2 16 24 n Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 Operating System Concepts – 8 th Edition 3. 28 Silberschatz, Galvin and Gagne © 2009

Round Robin (RR) n Each process gets a small unit of CPU time (time

Round Robin (RR) n 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. n 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. n Performance l q large FIFO l q small q must be large with respect to context switch, otherwise overhead is too high Operating System Concepts – 8 th Edition 3. 29 Silberschatz, Galvin and Gagne © 2009

Example of RR with Time Quantum = 4 Process Burst Time P 1 P

Example of RR with Time Quantum = 4 Process Burst Time P 1 P 2 P 3 24 3 3 n The Gantt chart is: P 1 0 P 2 4 P 3 7 P 1 10 P 1 14 P 1 18 22 P 1 26 P 1 30 n Typically, higher average turnaround than SJF, but better response Operating System Concepts – 8 th Edition 3. 30 Silberschatz, Galvin and Gagne © 2009

Time Quantum and Context Switch Time Operating System Concepts – 8 th Edition 3.

Time Quantum and Context Switch Time Operating System Concepts – 8 th Edition 3. 31 Silberschatz, Galvin and Gagne © 2009

CPU SCHEDULING EXAMPLE DATA: Process 1 2 3 4 Operating System Concepts – 8

CPU SCHEDULING EXAMPLE DATA: Process 1 2 3 4 Operating System Concepts – 8 th Edition Arrival Time 0 1 2 3 3. 32 Service Time 8 4 9 5 Silberschatz, Galvin and Gagne © 2009

Example of RR with Time Quantum = 20 n Example: l Process P 1

Example of RR with Time Quantum = 20 n Example: l Process P 1 P 2 P 3 P 4 Burst Time 53 8 68 24 Remaining Time 53 8 68 24 The Gantt chart is: Operating System Concepts – 8 th Edition 3. 33 Silberschatz, Galvin and Gagne © 2009

Multilevel Queue n Ready queue is partitioned into separate queues: foreground (interactive) background (batch)

Multilevel Queue n Ready queue is partitioned into separate queues: foreground (interactive) background (batch) n Each queue has its own scheduling algorithm l foreground – RR l background – FCFS n Scheduling must be done between the queues l Fixed priority scheduling; (i. e. , serve all from foreground then from background). Possibility of starvation. l 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 l 20% to background in FCFS Operating System Concepts – 8 th Edition 3. 34 Silberschatz, Galvin and Gagne © 2009

Multilevel Queue Scheduling Operating System Concepts – 8 th Edition 3. 35 Silberschatz, Galvin

Multilevel Queue Scheduling Operating System Concepts – 8 th Edition 3. 35 Silberschatz, Galvin and Gagne © 2009

Example of Multilevel Feedback Queue n Three queues: l Q 0 – RR with

Example of Multilevel Feedback Queue n Three queues: l Q 0 – RR with time quantum 8 milliseconds l Q 1 – RR time quantum 16 milliseconds l Q 2 – FCFS n Scheduling l 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. l 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. Operating System Concepts – 8 th Edition 3. 36 Silberschatz, Galvin and Gagne © 2009

Multilevel Feedback Queues Operating System Concepts – 8 th Edition 3. 37 Silberschatz, Galvin

Multilevel Feedback Queues Operating System Concepts – 8 th Edition 3. 37 Silberschatz, Galvin and Gagne © 2009

Multiple-Processor Scheduling n CPU scheduling more complex when multiple CPUs are available n Homogeneous

Multiple-Processor Scheduling n CPU scheduling more complex when multiple CPUs are available n Homogeneous processors within a multiprocessor n Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing n Symmetric multiprocessing (SMP) – each processor is self-scheduling, all processes in common ready queue, or each has its own private queue of ready processes n Processor affinity – process has affinity for processor on which it is currently running l soft affinity l hard affinity Operating System Concepts – 8 th Edition 3. 38 Silberschatz, Galvin and Gagne © 2009

Scheduling Metrics n Waiting Time: time the job is waiting in the ready queue

Scheduling Metrics n Waiting Time: time the job is waiting in the ready queue l Time between job’s arrival in the ready queue and launching the job n Service (Execution) Time: time the job is running n Response (Completion) Time: l l Time between job’s arrival in the ready queue and job’s completion Response time is what the user sees: 4 Time to echo a keystroke in editor 4 Time to compile a program Response Time = Waiting Time + Service Time n Throughput: number of jobs completed per unit of time l Throughput related to response time, but not same thing: 4 Minimizing response time will lead to more context switching than if you only maximized throughput Operating System Concepts – 8 th Edition 3. 39 Silberschatz, Galvin and Gagne © 2009

Scheduling Policy Goals/Criteria n Minimize Response Time l Minimize elapsed time to do an

Scheduling Policy Goals/Criteria n Minimize Response Time l Minimize elapsed time to do an operation (or job) n Maximize Throughput l Two parts to maximizing throughput 4 Minimize overhead (for example, context-switching) 4 Efficient use of resources (CPU, disk, memory, etc) n Fairness Share CPU among users in some equitable way l Fairness is not minimizing average response time: l 4 Better average response time by making system less fair Operating System Concepts – 8 th Edition 3. 40 Silberschatz, Galvin and Gagne © 2009