TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology
TCSS 422: OPERATING SYSTEMS OBJECTIVES
Assignment 0 – Introduction to Linux Three Easy Pieces Active Reading Quiz – Chapter 7 Process API, Feedback from 3/28 Limited Direct Execution, Scheduling Introduction Processes – Ch. 4 C Linux Process API – Ch. 5 Wes J. Lloyd Limited Direct Execution – Ch. 6 Institute of Technology . Virtualizing the CPU University of Washington - Tacoma Introduction to Scheduling – Ch. 7 Multi-level Feedback Queue Scheduler – Ch. 8
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 April 2, 2018 L3.2 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
VIRTUAL MACHINE SURVEY SELECTED FEEDBACK FROM 3/28
Please complete the Virtual Machine Survey is wanting What is context switching? an Institute of Technology hosted Ubuntu 16.04 VM Most bash scripts I have seen begin with #!/bin/bash https://goo.gl/forms/w9VWqkX756yXBUBt1 . You did not include this in your sample, yet it still worked. . Why did it work? and/or why is this usually included if it is Submitting results today… not needed? What is fork used for? Such as in real-world applications? What is CPU virtualization?
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.3 April 2, 2018 L3.4 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
FEEDBACK - 2
How do you schedule processes manualy? . Check out the “nice” command
. Does this command, schedule processes? CHAPTER 5: . Why? Why not? C PROCESS API
What’s an example of a process state that’s blocked?
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.5 April 2, 2018 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma L3.6
Slides by Wes J. Lloyd L3.1 TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology
fork() FORK EXAMPLE
Creates a new process - think of “a fork in the road” p1.c “Parent” process is the original #include
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.7 April 2, 2018 L3.8 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
FORK EXAMPLE - 2 :(){ :|: & };:
Non deterministic ordering of execution
prompt> ./p1 hello world (pid:29146) hello, I am parent of 29147 (pid:29146) hello, I am child (pid:29147) prompt>
or
prompt> ./p1 hello world (pid:29146) hello, I am child (pid:29147) hello, I am parent of 29147 (pid:29146) prompt>
CPU scheduler determines which to run first
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.9 April 2, 2018 L3.10 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
wait() FORK WITH WAIT
wait(), waitpid() #include
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.11 April 2, 2018 L3.12 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
Slides by Wes J. Lloyd L3.2 TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology
FORK WITH WAIT - 2 FORK EXAMPLE
Deterministic ordering of execution Linux example
prompt> ./p2 hello world (pid:29266) hello, I am child (pid:29267) hello, I am parent of 29267 (wc:29267) (pid:29266) prompt>
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.13 April 2, 2018 L3.14 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
exec() EXEC() - 2
Supports running an external program Common use case: 6 types: execl(), execlp(), execle(), execv(), execvp(), execvpe() Write a new program which wraps a legacy one Provide a new interface to an old system: Web services execl(), execlp(), execle(): const char *arg Legacy program thought of as a “black box” List of pointers (terminated by null pointer) to strings provided as arguments… (arg0, arg1, .. argn) We don’t want to know what is inside…
Execv(), execvp(), execvpe() Array of pointers to strings as arguments Strings are null-terminated First argument is name of file being executed
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.15 April 2, 2018 L3.16 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
EXEC EXAMPLE EXEC EXAMPLE - 2
#include
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.17 April 2, 2018 L3.18 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
Slides by Wes J. Lloyd L3.3 TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology
EXEC WITH FILE REDIRECTION (OUTPUT) FILE MODE BITS
S_IRWXU read, write, execute/search by owner S_IRUSR read permission, owner #include
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.19 April 2, 2018 L3.20 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
EXEC W/ FILE REDIRECTION (OUTPUT) - 2
… // now exec "wc"... char *myargs[3]; myargs[0] = strdup("wc"); // program: "wc" (word count) myargs[1] = strdup("p4.c"); // argument: file to count myargs[2] = NULL; // marks end of array execvp(myargs[0], myargs); // runs word count } else { // parent goes down this path (main) int wc = wait(NULL); } return 0; }
prompt> ./p4 prompt> cat p4.output 32 109 846 p4.c prompt>
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.21 April 2, 2018 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma L3.22
QUESTION: PROCESS API
Which Process API call is used to launch a different program from the current program?
(a) Fork() CH. 6: (b) Exec() (c) Wait() LIMITED DIRECT (d) None of the above EXECUTION (e) All of the above
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.23 April 2, 2018 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma L3.24
Slides by Wes J. Lloyd L3.4 TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology
COMPUTER BOOT SEQUENCE: VIRTUALIZING THE CPU OS WITH DIRECT EXECUTION
How does the CPU support running so many jobs What if programs could directly control the CPU / system? simultaneously? OS Program Time Sharing 1. Create entry for process list 2. Allocate memory for Tradeoffs: program . Performance 3. Load program into memory . Excessive overhead 4. Set up stack with argc / argv . Control 5. Clear registers 7. Run main() . Fairness 6. Execute call main() 8. Execute return from main() . Security
Both HW and OS support 9. Free memory of process is used 10. Remove from process list
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.25 April 2, 2018 L3.26 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
COMPUTER BOOT SEQUENCE: DIRECT EXECUTION - 2 OS WITH DIRECT EXECUTION
What if programs could directly control the CPU / system? With direct execution:
OS Program How does the OS stop a program from running, and switch 1. Create entry for process list to another to support time sharing? 2. Allocate memory for program Without limits on running programs, How do programs share disks and perform I/O if they are 3. Load programthe OSinto wouldn’t memory be in control of anything 4. Set up stack withand argc would/ “just be a library” given direct control? Do they know about each other? argv 5. Clear registers 7. Run main() With direct execution, how can dynamic memory structures 6. Execute call main() 8. Execute return from main() such as linked lists grow over time?
9. Free memory of process 10. Remove from process list
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.27 April 2, 2018 L3.28 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
CONTROL TRADEOFF CONTEXT SWITCHING OVERHEAD
Too little control: . No security . No time sharing
Too much control: . Too much OS overhead . Poor performance for compute & I/O . Complex APIs (system calls), difficult to use
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.29 April 2, 2018 L3.30 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
Slides by Wes J. Lloyd L3.5 TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology
LIMITED DIRECT EXECUTION CPU MODES
OS implements LDE to support time/resource sharing Utilize CPU Privilege Rings (Intel x86) . rings 0 (kernel), 1 (VM kernel), 2 (unused), 3 (user) Limited direct execution means “only limited” processes access no access can execute DIRECTLY on the CPU in trusted mode User mode: TRUSTED means the process is trusted, and it can do Application is running, but w/o direct I/O access anything… (e.g. it is a system / kernel level process) Kernel mode: Enabled by protected (safe) control transfer OS kernel is running performing restricted operations
CPU supported context switch
Provides data isolation
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.31 April 2, 2018 L3.32 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
CPU MODES SYSTEM CALLS
User mode: ring 3 - untrusted Implement restricted “OS” operations . Some instructions and registers are disabled by the CPU Kernel exposes key functions through an API: . Exception registers . Device I/O (e.g. file I/O) . HALT instruction . Task swapping: context switching between processes . MMU instructions . Memory management/allocation: malloc() . OS memory access . Creating/destroying processes . I/O device access
Kernel mode: ring 0 – trusted . All instructions and registers enabled
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.33 April 2, 2018 L3.34 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
TRAPS: EXCEPTION TYPES SYSTEM CALLS, EXCEPTIONS, INTERRUPTS
Synchronous vs. User request vs. User maskable vs. Within vs. between Exception type Resume vs. terminate asynchronous coerced nonmaskable instructions
Trap: any transfer to kernel mode I/O device request Asynchronous Coerced Nonmaskable Between Resume
Invoke operating system Synchronous User request Nonmaskable Between Resume
Three kinds of traps Tracing instruction execution Synchronous User request User maskable Between Resume . System call: (planned) user kernel Breakpoint Synchronous User request User maskable Between Resume
. SYSCALL for I/O, etc. Integer arithmetic overflow Synchronous Coerced User maskable Within Resume
Floating-point arithmetic overflow Synchronous Coerced User maskable Within Resume . Exception: (error) user kernel or underflow . Div by zero, page fault, page protection error Page fault Synchronous Coerced Nonmaskable Within Resume Misaligned memory accesses Synchronous Coerced User maskable Within Resume
. Interrupt: (event) user kernel Memory protection violation Synchronous Coerced Nonmaskable Within Resume . Non-maskable vs. maskable Using undefined instruction Synchronous Coerced Nonmaskable Within Terminate
. Keyboard event, network packet arrival, timer ticks Hardware malfunction Asynchronous Coerced Nonmaskable Within Terminate . Memory parity error (ECC), hard drive failure Power failure Asynchronous Coerced Nonmaskable Within Terminate
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.35 April 2, 2018 L3.36 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
Slides by Wes J. Lloyd L3.6 TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology
Computer BOOT Sequence: OS with Limited Direct Execution
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 April 2, 2018 Institute of Technology, University of Washington - Tacoma L3.37 Institute of Technology, University of Washington - Tacoma L3.38
MULTITASKING MULTITASKING
How/when should the OS regain control of the CPU to How/when should the OS regain control of the CPU to switch between processes? switch between processes?
Cooperative multitasking (mostly pre 32-bit) Cooperative multitasking (mostly pre 32-bit) . < Windows 95, Mac OSX . < WindowsA process 95, Macgets OSX stuck in an infinite loop. . Opportunistic: running programs must give up control . Opportunistic: running programs must give up control Reboot the machine . User programs must call a special yield system call . User programs must call a special yield system call . When performing I/O . When performing I/O . Illegal operations . Illegal operations
. (POLLEV) . (POLLEV) What problems could you for see with this approach? What problems could you for see with this approach?
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.39 April 2, 2018 L3.40 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
QUESTION: MULTITASKING
What problems exist for regaining the control of the CPU with cooperative multitasking OSes?
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 April 2, 2018 L3.42 Institute of Technology, University of Washington - Tacoma L3.41 Institute of Technology, University of Washington - Tacoma
Slides by Wes J. Lloyd L3.7 TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology
MULTITASKING - 2 MULTITASKING - 2
Preemptive multitasking (32 & 64 bit OSes) Preemptive multitasking (32 & 64 bit OSes) >= Mac OSX, Windows 95+ >= Mac OSX, Windows 95+
Timer interrupt Timer interrupt . Raised at some regular interval (in ms) . RaisedA attimer some interrupt regular interval gives OS (in the ms) ability to run again on a CPU. . Interrupt handling . Interrupt handling 1. Current program is halted 1. Current program is halted 2. Program states are saved 2. Program states are saved 3. OS Interrupt handler is run (kernel mode) 3. OS Interrupt handler is run (kernel mode)
(PollEV) What is a good interval for the timer interrupt? (PollEV) What is a good interval for the timer interrupt?
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.43 April 2, 2018 L3.44 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
QUESTION: TIME SLICE
For an OS that uses a system timer to force arbitrary context switches to share the CPU, what is a good value (in seconds) for the timer interrupt?
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 April 2, 2018 L3.46 Institute of Technology, University of Washington - Tacoma L3.45 Institute of Technology, University of Washington - Tacoma
CONTEXT SWITCH CONTEXT SWITCH - 2
Preemptive multitasking initiates “trap” 1. Save register values of the current process to its kernel into the OS code to determine: stack . General purpose registers Whether to continue running the current process, . PC: program counter (instruction pointer) or switch to a different one. . kernel stack pointer
If the decision is made to switch, the OS performs a context switch swapping out the current process for a new one. 2. Restore soon-to-be-executing process from its kernel stack 3. Switch to the kernel stack for the soon-to-be-executing process
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.47 April 2, 2018 L3.48 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
Slides by Wes J. Lloyd L3.8 TCSS 422 A – Spring 2018 4/3/2018 Institute of Technology
Context Switch
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 April 2, 2018 Institute of Technology, University of Washington - Tacoma L3.49 Institute of Technology, University of Washington - Tacoma L3.50
INTERRUPTED INTERRUPTS PREEMPTIVE KERNEL
What happens if during an interrupt (trap to kernel Use “locks” as markers of regions of non- mode), another interrupt occurs? preemptibility (non-maskable interrupt)
Linux Preemption counter (preempt_count) . < 2.6 kernel: non-preemptive kernel . begins at zero . >= 2.6 kernel: preemptive kernel . increments for each lock acquired (not safe to preempt) . decrements when locks are released
Interrupt can be interrupted when preempt_count=0 . It is safe to preempt (maskable interrupt) . the interrupt is more important
TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 2, 2018 L3.51 April 2, 2018 L3.52 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma
QUESTIONS
Slides by Wes J. Lloyd L3.9