Föreläsning 1 2022-04-25 DT513G Operativsystem för civilingenjörer, 4,5 högskolepoäng ------------------------------------------------------------ (Det här är bara mina egna minnesanteckningar. Även om de visar ungefär vad jag tar upp på föreläsningen, så är de kanske inte särskilt begripliga, och de ska förstås inte ses som en ersättning för föreläsningen, boken eller något annat.) 1. Hej och välkomna! -------------------- Hej och välkomna! Thomas Padron-McCarthy kursbok: olika upplagor, 9:e, 10:e, olika versioner (nämn inte ens pirat-PDF:en!) tenta labbar, projektuppgift Föreläsningsfilmer på Youtube, till exempel från University of Massachusetts Amherst: https://www.youtube.com/playlist?list=PLacuG5pysFbDQU8kKxbUh4K5c1iL5_k7k 2. Bakgrundskunskaper --------------------- Formellt: DT505G Algoritmer, datastrukturer och komplexitet för civilingenjörer 7,5 högskolepoäng Reellt: DT509G Datorteknik för civilingenjörer, 7,5 högskolepoäng (hårdvara, device driver, open...) DT511G Inbyggda system för civilingenjörer, 7,5 högskolepoäng (processer, schedulering, mutex-lås...) 3. Vad är ett operativsystem? ----------------------------- *** A program that acts as an intermediary between a user of a computer and the computer hardware. Erbjuder en virtuell maskin! Operating System Definition...? *** OS is a control program - Controls execution of programs to prevent errors and improper use of the computer *** OS is a resource allocator - Manages all resources - Decides between conflicting requests for efficient and fair resource use *** OS provides uniform, logical view of information storage - Abstracts physical properties to logical storage unit - file - Each medium is controlled by device (i.e., disk drive, tape drive) - Varying properties include access speed, capacity, data- transfer rate, access method (sequential or random) Operating System Definition (Continued) *** No universally accepted definition *** "Everything a vendor ships when you order an operating system" is a good approximation, BUT varies wildly. Note: Internet Explorer! *** "The one program running at all times on the computer" is the kernel. *** Everything else is either - a system program (ships with the operating system) , or - an application program. Summary of Operating System Principles: * OS as juggler/illusionist: providing the illusion of a dedicated machine with infinite memory and CPU. * OS as government: protecting users from each other, allocating resources efficiently and fairly, and providing secure and safe communication. * OS as complex system: keeping OS design and implementation as simple as possible is the key to getting the OS to work. * OS as history teacher: learning from past to predict the future, i.e., OS design tradeoffs change with technology. Protection and Security *** Protection – any mechanism for controlling access of processes or users to resources defined by the OS *** Security – defense of the system against internal and external attacks - Huge range, including denial-of-service, worms, viruses, identity theft, theft of service *** Systems generally first distinguish among users, to determine who can do what - User identities (user IDs, security IDs) include name and associated number, one per user - User ID then associated with all files, processes of that user to determine access control - Group identifier (group ID) allows set of users to be defined and controls managed, then also associated with each process, file - Privilege escalation allows user to change to effective ID with more rights *** Operating system goals: - Execute user programs and make solving user problems easier - Make the computer system convenient to use - Use the computer hardware in an efficient manner What Operating Systems Should Do: *** Depends on the point of view *** Users want convenience, ease of use and good performance - Don’t care about resource utilization *** Some computers have little or no user interface, such as embedded computers in devices and automobiles *** Handheld computers are resource poor, optimized for usability and battery life *** Users of dedicate systems such as workstations have dedicated resources but frequently use shared resources from servers *** But shared computer such as mainframe or minicomputer must keep all users happy 4. Repetition: Computer System Structure ---------------------------------------- Ett "datorsystem" ("en dator") består av fyra delar: * Hardware – provides basic computing resources - CPU, memory, I/O devices * Operating system - Controls and coordinates use of hardware among various applications and users * Application programs – define the ways in which the system resources are used to solve the computing problems of the users - Word processors, compilers, web browsers, database systems, video games * Users - People, machines, other computers Four Components of a Computer System (bild från OSC-10-slides sid 1.6) 5. Repetition(?): Bussar och devices ("enheter") ------------------------------------------------ "Omnibus" = latin "för alla". Både fordonet och datorkomponenten! Computer System Organization and operation * One or more CPUs, device controllers connect through common bus providing access to shared memory * Concurrent execution of CPUs and devices competing for memory cycles (DMA!) (bild från OSC-10-slides sid 1.11) 6. Interrupt ("avbrott") ------------------------ Olika sorter: hårdvara (t ex ett device), timer, mjukvara (ex: 1/0, syscall) Common Functions of Interrupts: *** Interrupt transfers control to the interrupt service routine generally, through the interrupt vector, which contains the addresses of all the service routines *** Interrupt architecture must save the address of the interrupted instruction *** A trap or exception is a software-generated interrupt caused either by an error or a user request *** An operating system is interrupt driven 7. Repetition: Lagring av data ------------------------------ 1. Register: rymmer mindre än 1 KB, accesstid en klockcykel, dvs 0.25-0.5 ns 2. Primärminnescache (flera nivåer): upp till 16 MB (per IC-krets), 0.5-25 ns 3. Primärminne ("main memory"): kanske 32 GB, 80-250 ns the only large storage media that the CPU can access directly - Random access - Typically volatile 4. Sekundärminne ("secondary storage") - extension of main memory that provides large nonvolatile storage capacity - block device, typiskt med filsystem 4A: Hard disks: 1-10 TB, 5-10 ms 4B: Solid-state disks/devices: 0.5-1 TB, 25-50 us (100 ggr snabbare access, 5-10 ggr bandbredd) 5. USB-minnen, externa diskar 6. Servrar, NAS, SAN, moln... 8. Caching ---------- cache (inte cash!) = "litet, undangömt förråd" = härbre = "fickminne" *** Important principle, performed at many levels in a computer (in hardware, operating system, software) *** Information in use copied from slower to faster storage temporarily *** Faster storage (cache) checked first to determine if information is there - If it is, information used directly from the cache (fast) - If not, data copied to cache and used there *** Cache smaller than storage being cached - Cache management important design problem - Cache size and replacement policy Symmetric Multiprocessing Architecture (bild från OSC-10-slides sid 1.25) Migration of data “A" from Disk to Register *** Multitasking environments must be careful to use most recent value, no matter where it is stored in the storage hierarchy *** Multiprocessor environment must provide cache coherency in hardware such that all CPUs have the most recent value in their cache *** Distributed environment situation even more complex - Several copies of a datum can exist - Various solutions covered in Chapter 17 (bild från OSC-10-slides sid 1.40) 9. DMA = Direct Memory Access ----------------------------- *** Used for high-speed I/O devices able to transmit information at close to memory speeds *** Device controller transfers blocks of data from buffer storage directly to main memory without CPU intervention *** Only one interrupt is generated per block, rather than the one interrupt per byte 10. HUR operativsystemet gör vad det nu gör ------------------------------------------- *** Interrupt driven (hardware and software) - Hardware interrupt by one of the devices - Software interrupt (exception or trap): - Software error (e.g., division by zero) - Request for operating system service - Other process problems include infinite loop, processes modifying each other or the operating system *** Dual-mode operation allows OS to protect itself and other system components - User mode and kernel mode - Mode bit provided by hardware - Provides ability to distinguish when system is running user code or kernel code - Some instructions designated as privileged, only executable in kernel mode - System call changes mode to kernel, return from call resets it to user *** Increasingly CPUs support multi-mode operations - i.e. virtual machine manager (VMM) mode for guest VMs Transition from User to Kernel Mode *** Timer to prevent infinite loop / process hogging resources - Timer is set to interrupt the computer after some time period - Keep a counter that is decremented by the physical clock. - Operating system set the counter (privileged instruction) - When counter zero generate an interrupt - Set up before scheduling process to regain control or terminate program that exceeds allotted time (bild från OSC-10-slides sid 1.33) 11. Architectural Features Motivated by OS Services --------------------------------------------------- OS Service <--> Hardware Support Protection Kernel/user mode, protected instructions, base/limit registers Interrupts Interrupt vectors System calls Trap instructions and trap vectors I/O <--> Interrupts and memory mapping Scheduling, error recovery, accounting <--> Timer Synchronization <--> Atomic instructions Virtual memory <--> Translation look-aside buffers 12. Kärnan ("kernel") --------------------- Monolitisk Moduler (fortfarande monolitisk, men man kan ladda in moduler) Microkernel (flytta ut så mycket som möjligt i userspace) Hybridkärna 13. Processer ------------- *** A process is a program in execution. It is a unit of work within the system. Program is a passive entity, process is an active entity. *** Process needs resources to accomplish its task - CPU, memory, I/O, files - Initialization data *** Process termination requires reclaim of any reusable resources *** Single-threaded process has one program counter specifying location of next instruction to execute - Process executes instructions sequentially, one at a time, until completion *** Multi-threaded process has one program counter per thread *** Typically system has many processes, some user, some operating system, running concurrently on one or more CPUs - Concurrency by multiplexing the CPUs among the processes / threads The operating system is responsible for the following activities in connection with process management: * Creating and deleting both user and system processes * Suspending and resuming processes * Providing mechanisms for process synchronization * Providing mechanisms for process communication * Providing mechanisms for deadlock handling Processens minne: *** To execute a program all (or part) of the instructions must be in memory *** All (or part) of the data that is needed by the program must be in memory. *** Memory management determines what is in memory and when - Optimizing CPU utilization and computer response to users *** Memory management activities - Keeping track of which parts of memory are currently being used and by whom - Deciding which processes (or parts thereof) and data to move into and out of memory - Allocating and deallocating memory space as needed 14. Mer i kursen ---------------- API (ungefär men inte riktigt: systemanrop) Linux Kärnan Processer Trådar Primärminne, virtuellt minne, demand paging Sekundärminne: I/O, drivrutiner, filsystem Skydd och säkerhet Virtualisering, virtuella maskiner Labbuppgifter: Linux, API, systemanrop, kärnan, minne, projektuppgift