Last on TTIT61 The OS consists of A user interface for controlling programs (starting, interrupting) A set of device drivers for accessing the hardware A set of system calls as a program interface to hardware (and not only, we’ll see later) Process management Memory management File system Others TTIT61: Lecture 2 Processes, Schedulers, Threads Alexandru Andrei alean@ida.liu.se phone: 013-322828, room: B 3D:439 A. Andrei, Process programming and operating systems, Processes, schedulers, threads Lecture Plan Outline The concept of “process” Memory layout of a process Operations on processes (create, terminate, etc.) Process state and state transition diagram Process Control Blocks Context switches Threads Motivation, user vs. kernel Multi-threading models, threading issues CPU scheduling Criteria, algorithms 1. What is an operating system? What are its functions? Basics of computer architectures. (Part I of the textbook) 2. Processes, threads, schedulers (Part II , chap. III-V) 3. Synchronization & Deadlock (Part II, chap. VI, VII) 4. Primary memory management. (Part III, chap. VIII, IX) 5. File systems and secondary memory management (Part IV, chap. X, XI, XII) 6. Security (Part V, chap. XIV) A. Andrei, Process programming and operating systems, Processes, schedulers, threads 3 A. Andrei, Process programming and operating systems, Processes, schedulers, threads What Is a Process? In the previous lecture, I’ve used “processes” and “programs” interchangeably hoping you will not notice A program is a passive entity, we use it to refer to the executable file on the disk (or memory stick, etc., from where it is eventually loaded in the main memory) Definition: A process is an active entity, it is an executing program, it is an instance of the program 4 Process and Program User 1 User 2 Process 2 Process 1 Executing instruction here Editor program Executing instruction here Process 1 data We can have several processes of the same program A. Andrei, Process programming and operating systems, Processes, schedulers, threads 2 5 Process 2 data A. Andrei, Process programming and operating systems, Processes, schedulers, threads 6 1 Memory Layout of a Process Memory Text (code) Data Heap Stack A process may contain several data segments: Read-only data: printf(“%d\n”, i) “%d\n” is read-only Initialised data: int a = 20; 20 goes into the executable file on the disk. When the program is loaded into memory, the memory location corresponding to ‘a’ is initialised with 20 Uninitialised data: int b; No space for ‘b’ is reserved for the executable file on the disk. It is just specified in the executable file header that the uninitialised data segment is X bytes long. When the program is loaded into memory, a segment of X bytes is reserved for uninitialised data. The text segment contains the code of the program The data segment contains the global variables The stack segment contains the return addresses of function calls, and in most OS the local variables The heap contains the dynamically allocated memory. It is typically at the other end of the stack segment. A. Andrei, Process programming and operating systems, Processes, schedulers, threads The Data Segment(s) 7 A. Andrei, Process programming and operating systems, Processes, schedulers, threads The Stack Segment int fact(int n) { int xp; 1000: if (n == 0) 1001: return 1; 1002: xp = fact(n – 1); 1003: xp *= n; 1004: return xp; } main() { int rlt; 1005: rlt = fact(3); 1006: printf(“%d\n, rlt); } rlt = 6 3 1006 xp = 6 2 1003 xp = 2 1 1003 xp = 1 0 1003 xp 8 Process Execution A register of the CPU, the program counter (PC), contains the address of the next instruction to execute (it points in the code segment) Another register of the CPU, the stack pointer (SP), contains the address of the of the top of the stack (it points in the stack segment) main fact fact fact fact A. Andrei, Process programming and operating systems, Processes, schedulers, threads 9 A. Andrei, Process programming and operating systems, Processes, schedulers, threads Segment Sharing Can two processes of the same program share the code segment? What would we gain/lose if yes? Segment Sharing User 1 User 2 Process 2 Process 1 Executing instruction here Can two processes, not necessarily of the same program, share the data segment? Why would we (not) want that? Process 1 data Can two processes, not necessarily of the same program, share the stack segment? A. Andrei, Process programming and operating systems, Processes, schedulers, threads 11 10 Editor program (Process1 and 2 code ?) Executing instruction here Process 2 data A. Andrei, Process programming and operating systems, Processes, schedulers, threads 12 2 Outline The concept of “process” Memory layout of a process Operations on processes (create, terminate, etc.) Process state and state transition diagram Process Control Blocks Context switches Threads Motivation, user vs. kernel Multi-threading models, threading issues CPU scheduling Criteria, algorithms A. Andrei, Process programming and operating systems, Processes, schedulers, threads Operations on Processes Creation: system call called fork in Unix Termination: system call called exit Loading of new process image (code, data, stack segments): system call called exec in Unix Waiting for the termination of a child: system call called wait or waitpid in Unix man –s 2 fork/exit/exec/wait/waitpid 13 Fork A. Andrei, Process programming and operating systems, Processes, schedulers, threads 14 A tree of processes on a typical Solaris Fork creates a “clone” of the invoking process. The invoking process will be the parent, and the “clone” will be the child. One child has exactly one parent, a parent may have 0 or more children A. Andrei, Process programming and operating systems, Processes, schedulers, threads 15 A. Andrei, Process programming and operating systems, Processes, schedulers, threads Code Example Fork Fork creates a “clone” of the invoking process. The invoking process will be the parent, and the “clone” will be the child. One child has exactly one parent, a parent may have 0 or more children pid = fork(); if (pid == 0) { printf(“I am the child. My ID is %d and my parent’s ID is %d\n”, getpid(), getppid()); execlp(“/bin/ls”, “ls”, “-l”, “/home/TTIT61”, 0); exit(0); } else { printf(“I am the parent. My child’s ID is %d\n”, pid); waitpid(pid, &status, 0); } A. Andrei, Process programming and operating systems, Processes, schedulers, threads 16 The child inherits the resources of the parent (set of open files, scheduling priority, etc.) 17 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 18 3 CoW However, the child process has its own memory space that contains the same data as the memory space of the parent, just that it is a copy. Parent and child do not share data and stack segments. Each has its own copy that it can modify independently. Should parent and child have different copies of read-only segments? Do they share the code segment? Parent and child processes have separated memory spaces, it is as if they are not aware that the other process exists. Sometimes, we would like to pass data from one process to the other E.g.: gzip –dc nachos-3.4.tar.gz | tar xf – Mail composer + spell checker Actually, in modern Unixes, data segments are allocated in a lazy manner, i.e. only if one of them starts writing, will the data segment copied. This lazy copying technique is called “copy-on-write” (CoW) A. Andrei, Process programming and operating systems, Processes, schedulers, threads Co-operating Processes 19 Inter-Process Communication Mechanisms A. Andrei, Process programming and operating systems, Processes, schedulers, threads 20 Communications Models Pipes (gzip –dc nachos-3.4.tar.gz | tar xf -) Signals (kill -9 pid) Message queues Semaphores, condition variables, locks, etc. Shared memory segments Network sockets (http, ftp, X windows, etc.) A. Andrei, Process programming and operating systems, Processes, schedulers, threads 21 Outline The concept of “process” Memory layout of a process Operations on processes (create, terminate, etc.) Process state and state transition diagram Process Control Blocks Context switches Threads Motivation, user vs. kernel Multi-threading models, threading issues CPU scheduling Criteria, algorithms A. Andrei, Process programming and operating systems, Processes, schedulers, threads A. Andrei, Process programming and operating systems, Processes, schedulers, threads 22 Process States 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 23 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 24 4 Process States (cont.) The concept of “process” Memory layout of a process Operations on processes (create, terminate, etc.) Process state and state transition diagram Process Control Blocks Context switches Threads Motivation, user vs. kernel Multi-threading models, threading issues CPU scheduling Criteria, algorithms New preemption admitted Ready Running dispatch I/O, event completion exit I/O, wait Waiting Terminated A. Andrei, Process programming and operating systems, Processes, schedulers, threads Outline 25 A. Andrei, Process programming and operating systems, Processes, schedulers, threads Process Control Block (PCB) Contents of the PCB Program counter value, stack pointer value, and the value of all other registers Memory management information (base + limit registers, translation tables) CPU scheduling information (process priority, pointers to scheduling queues) Accounting information (CPU time used, real time used, etc) I/O status (devices allocated to the process, list of open files, etc) Is a memory area (data structure) in the OS kernel memory One for each process Contains the data needed by the OS in order to manage the process to which the PCB corresponds It is also called the context of the process When the OS switches the process that runs on the CPU, we say that it performs a context switch A. Andrei, Process programming and operating systems, Processes, schedulers, threads 27 A. Andrei, Process programming and operating systems, Processes, schedulers, threads Context Switch Process A 26 28 Ready Queues Process B A running Save state of A into PCBA Load state of B from PCBB Ready queue CPU Context switch I/O I/O queue I/O request B running Time slice expired Save state of B into PCBB Load state of A from PCBA Interrupt occurs Context switch Wait for interrupt A running A. Andrei, Process programming and operating systems, Processes, schedulers, threads 29 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 30 5 Scheduling A long-term scheduler runs whenever a processes terminates in order to decide which new process to bring in the memory Has to select an appropriate process mix Too many CPU-bound processes ⇒ devices underutilised, process execution times much longer than if the mix was more balanced Too many device-bound processes ⇒ under-utilised CPU Degree of multiprogramming: the number of processes in memory at the same time If this degree is stable ⇒ number of newly created processes over a time interval is roughly equal to the number of processes that terminated in the same interval The scheduler is the routine that selects a process from the ready queue. This is the short-term scheduler. It runs frequently (at least one time every 100ms). A. Andrei, Process programming and operating systems, Processes, schedulers, threads Long-Term Scheduler 31 A. Andrei, Process programming and operating systems, Processes, schedulers, threads Outline The concept of “process” Memory layout of a process Operations on processes (create, terminate, etc.) Process state and state transition diagram Process Control Blocks Context switches Threads Motivation, user vs. kernel Multi-threading models, threading issues CPU scheduling Criteria, algorithms A. Andrei, Process programming and operating systems, Processes, schedulers, threads 32 Context Switch Process A Process B A running Context switch Performance bottleneck B running Context switch A running 33 Threads A. Andrei, Process programming and operating systems, Processes, schedulers, threads 34 Single vs. Multi-Threaded Processes Also known as lightweight processes They do share the data segment Do they share the stack segment? A. Andrei, Process programming and operating systems, Processes, schedulers, threads 35 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 36 6 Advantages of Threads User Threads and Kernel Threads Kernel threads: threads that are visible by the OS They are a scheduling unit Thread creation, scheduling, management is done in kernel space ⇒ slightly slower than user threads If a kernel thread blocks (on I/O, for example), the kernel is able to schedule a different kernel thread or process ⇒ rather efficient Resource sharing (memory segments) Faster creation and destruction (30 times on Solaris 2) ⇒ application is much more responsive Faster context switch (5 times on Solaris 2) User threads: implemented by a thread library at the user level They are not a scheduling unit Creation, scheduling, management is done by the user (library) ⇒ faster than kernel threads If a user thread blocks, all user threads belonging to the scheduling unit (encapsulating process) block A. Andrei, Process programming and operating systems, Processes, schedulers, threads 37 A. Andrei, Process programming and operating systems, Processes, schedulers, threads User Threads Kernel Threads Supported by the Kernel Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads A. Andrei, Process programming and operating systems, Processes, schedulers, threads 38 Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X 39 A. Andrei, Process programming and operating systems, Processes, schedulers, threads Multi-Threading Models Many-to-one 40 Multi-Threading Models One-to-one User threads User threads k A. Andrei, Process programming and operating systems, Processes, schedulers, threads k 41 k k A. Andrei, Process programming and operating systems, Processes, schedulers, threads 42 7 Multi-Threading Models Threading Issues Many-to-many Fork and exec? Should the child process be multi-threaded, or should only the calling thread be cloned in a new process? exec invoked by one thread replaces the entire process Signals? Which thread should get the signal? User threads k k A. Andrei, Process programming and operating systems, Processes, schedulers, threads 43 A. Andrei, Process programming and operating systems, Processes, schedulers, threads Pthreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X) A. Andrei, Process programming and operating systems, Processes, schedulers, threads 44 Windows XP Threads Implements the one-to-one mapping Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block) 45 A. Andrei, Process programming and operating systems, Processes, schedulers, threads Linux Threads 46 Java Threads Java threads are managed by the JVM Java threads may be created by: Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process) A. Andrei, Process programming and operating systems, Processes, schedulers, threads Extending Thread class the Runnable interface Implementing 47 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 48 8 Java Thread States Outline The concept of “process” Memory layout of a process Process state and state transition diagram Process Control Blocks Context switches Operations on processes (create, terminate, etc.) Threads Motivation, user vs. kernel Multi-threading models, threading issues CPU scheduling Criteria, algorithms A. Andrei, Process programming and operating systems, Processes, schedulers, threads 49 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 50 CPU Scheduling Alternating Sequence of CPU And I/O Bursts Why scheduling? For using resources efficiently I/O is very much slower than the CPU (CPUs run at billions of instructions per second, hard disk and network accesses take milliseconds) When a process makes an I/O request, it has to wait. In this time, the CPU would idle if the OS did not schedule a ready process on it. A. Andrei, Process programming and operating systems, Processes, schedulers, threads 51 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 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 time-sharing environment) A. Andrei, Process programming and operating systems, Processes, schedulers, threads 52 Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time 53 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 54 9 Non-Preemptive vs. Preemptive If a scheduling decision is taken only when a process terminates or moves to the waiting state because of the unavailability of a resource, the scheduling is nonpreemptive (Windows 3.1, Apple Macintosh) Non-Preemptive vs. Preemptive Non-preemptive P1 0 If a scheduling decision is taken also when a process becomes ready to execute (moves from waiting or running state to ready state), the scheduling is preemptive 3 7 0 55 P2 2 P3 Non-preemptive scheduling requires no hardware support (timer). The OS is also less complex. 12 16 4 5 P1 P4 7 11 16 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 56 Dispatcher Once the new process to run is selected by the (short time) scheduler, the dispatcher Stops the currently running process (if any) Switches context Switches to user mode Jumps to the proper location in the user program to restart it Preemptive leads to shorter response times. However, operations by the kernel on some data have to be performed atomically (i.e. without being preempted while in the middle of managing that data) in order to avoid data inconsistancies A common Unix solution is preemptive scheduling of processes and non-preemptable system calls. However, the problem persists because of interrupts from the hardware, which may not be ignored. Either disable interrupts or, better, fine-grained locking The time it takes the dispatcher to do that is the dispatch latency 57 A. Andrei, Process programming and operating systems, Processes, schedulers, threads Scheduling Criteria CPU utilisation – keep it as busy as possible The load can be between 0 and 100%. ‘uptime’ in Unix indicates the average number of ready processes in the ready queue. Therefore it may be > 1 Throughput – number of finished processes per time unit Turnaround time – length of the time interval between the process submission time and finishing time of a process Waiting time – length of time spent waiting in the ready queue Response time – length of the time interval between the process submission time and the production of the first results A. Andrei, Process programming and operating systems, Processes, schedulers, threads 8 P2 Non-Preemptive vs. Preemptive A. Andrei, Process programming and operating systems, Processes, schedulers, threads P4 P2 Preemptive P1 A. Andrei, Process programming and operating systems, Processes, schedulers, threads P3 58 First-Come First-Served Simple Non-preemptive Non-minimal waiting times Convoy effect 59 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 60 10 FCFS Scheduling (Cont.) First-Come, First-Served (FCFS) Scheduling Suppose that the processes arrive in the order P2 , P3 , P1 The Gantt chart for the schedule is: Process Burst Time P1 24 P2 3 3 P3 Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: P2 0 P1 P2 0 24 P3 3 6 27 Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case Convoy effect short process behind long process 30 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 61 A. Andrei, Process programming and operating systems, Processes, schedulers, threads Shortest Job First Optimal w.r.t. waiting time Shortest-Job-First (SJF) Scheduling 63 A. Andrei, Process programming and operating systems, Processes, schedulers, threads Example of Non-Preemptive SJF Process Arrival Time P1 0.0 P2 2.0 4.0 P3 P4 5.0 SJF (non-preemptive) 3 62 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 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 0 30 P3 Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 P1 P1 Burst Time 7 4 1 4 P3 7 P2 8 Example of Preemptive SJF Process Arrival Time P1 0.0 P2 2.0 4.0 P3 P4 5.0 SJF (preemptive) P1 P4 12 16 0 Average waiting time = (0 + 6 + 3 + 7)/4 = 4 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 64 P2 P3 2 4 P4 P2 5 Burst Time 7 4 1 4 7 P1 11 16 Average waiting time = (9 + 1 + 0 +2)/4 = 3 65 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 66 11 Shortest Job First Can only estimate the length Can be done by using the length of previous CPU bursts, using exponential averaging 1. t n = actual length of n th CPU burst Optimal w.r.t. waiting time How could we know the length of the next CPU burst? 2. τ n +1 = predicted value for the next CPU burst 3. α , 0 ≤ α ≤ 1 4. Define : τ n +1 = α t n + (1 − α )τ n . Cannot be implemented as the short-term scheduling algorithm. We cannot know the length of the next CPU burst. We can predict it with various methods (see exponential average, textbook, section 6.3.2) A. Andrei, Process programming and operating systems, Processes, schedulers, threads Determining Length of Next CPU Burst 67 A. Andrei, Process programming and operating systems, Processes, schedulers, threads Priority Scheduling Processes are given priorities offline, not by the OS Starvation – low priority processes never get to the CPU Can be countered by aging, i.e. slowly modifying the priorities of processes that waited for a long time 69 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 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 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 Non-preemptive 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 More flexible in the sense that priorities capture aspects such as importance of the job, (financial) reward, etc. A. Andrei, Process programming and operating systems, Processes, schedulers, threads 68 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 70 Example of RR with Time Quantum = 20 Process P1 P2 P3 P4 The Gantt chart is: P1 0 P2 20 37 P3 Burst Time 53 17 68 24 P4 57 P1 77 P3 P4 P1 P3 P3 97 117 121 134 154 162 Typically, higher average turnaround than SJF, but better response 71 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 72 12 Multi-Level Queue Scheduling Time Quantum and Context Switch Time Processes are assigned to different queues, based on some properties (interactive or not, memory size, etc.) There is a scheduling policy between queues, and a scheduling policy for each queue System processes High priority Interactive processes Interactive editing processes Batch processes Student processes A. Andrei, Process programming and operating systems, Processes, schedulers, threads 73 Multilevel Feedback Queues Low priority A. Andrei, Process programming and operating systems, Processes, schedulers, threads 74 Example of Multilevel Feedback Queue Three queues: Q0 – RR with time quantum 8 milliseconds Q1 – RR time quantum 16 milliseconds Q2 – FCFS Scheduling A new job enters queue Q0 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 Q1. At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2. A. Andrei, Process programming and operating systems, Processes, schedulers, threads 75 Multiple-Processor Scheduling 76 Further Reading Operations on processes (man pages, http://www.linuxhq.com/guides/LPG/node7.html) Signals in Unix (man signal, man sigaction) Pthreads (man pthreads) Multi-processor scheduling (section 6.4) Real-time scheduling (section 6.5) 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 A. Andrei, Process programming and operating systems, Processes, schedulers, threads A. Andrei, Process programming and operating systems, Processes, schedulers, threads 77 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 78 13 Summary Processes are executing programs Kernel manages them, their state, associated data (open files, address translation tables, register values, etc.) Threads are lightweight processes, i.e. they share data segments, are cheaper to spawn and terminate Scheduler selects next process to run among the ready ones Various scheduling algorithms and criteria to evaluate them A. Andrei, Process programming and operating systems, Processes, schedulers, threads Reading Silberschatz, Galvin, Gagne, 7th edition, Part II Chapter 3: 3.1-3.5 Chapter 4: 4.1-4.3, 4.5 79 A. Andrei, Process programming and operating systems, Processes, schedulers, threads 80 14