what is an operating system?facstaff.bloomu.edu/dcoles/386/downloads/whatis.pdf · kernel the...

34
What is an Operating System? COMPSCI 386

Upload: phungcong

Post on 12-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

What is an Operating System?

COMPSCI 386

Preliminaries

● What is a process?

processes

registersprogramcounter

runtimestack threads

Preliminaries

● Registers– small and extremely fast storage units within CPU

– dedicated (accumulator, PC, IR) and general purpose

RAMCPUclock

registers

control unit

ALU cache

Side Note

● All modern CPUs have multi-level caches.

● Usually split into instruction and data caches.

● In multicore systems, each CPU has its own cache and there may be a shared cache.

● Cache coherency and other complex problems must be addressed, but these are hardware issues.

● Similarly, the distinction between multicore and multiprocessor systems is transparent to the OS.

Preliminaries

● Runtime stack (call stack)– used to implement subroutines

– stack frames (activation records)

– stack pointer

int main() {

a = f(x, y);

}int f(int c, int d) {

g(c + d);

}

int g(int x) {

}

maincontext

fcontext

gcontext

Preliminaries

● Process: a program in execution; own address space.● Thread: a subsequence of instructions within a process

(a flow of control) that can be executed concurrently with other subsequences – as if on a dedicated CPU.– concurrent vs. parallel computation

– threads of a process share code, data, resources

– each thread has its own register set and call stack

– web browsers, servlets, databases, GUIs, operating systems.

What is an OS?

● An interface between users/applications and the hardware.

– Provides a high-level view of I/O devices.

– Provides useful abstractions: processes, memory, files...

● Resource allocator.

– Mediates conflicting requests for system resources as fairly and efficiently as possible.

– CPU, main memory, disk storage, network sockets, etc.

● Control program.

– Provides an environment for safe execution of user applications.

– For example, prevents a process from accessing memory outside its address space or writing to the boot sector.

System and Application Programs

Operating System as an Interface

Operating System

Hardware

USER

compiler text editor ⋯web browser database

USER USER ⋯ USER

OS Responsibilities

● Process management– creating and deleting processes

– scheduling threads and processes

– suspending and resuming processes

– synchronizing threads and processes

– enabling interprocess communication

– preventing deadlock

OS Responsibilities

● Main memory management

– allocating memory for processes

– keeping track of allocated memory

– paging and virtual memory

– reclaiming memory from completed processes

OS Responsibilities

● Mass-storage management– file system

● creating and deleting files and directories● mapping files onto secondary storage● primitives for manipulating files and directories

– disk system● free space management● storage allocation● disk scheduling

OS Responsibilities

● Protection– controling access to system resources

– distinguish among users or sets of users

● Security– defend system from internal and external attacks

– viruses, worms, DoS attacks, theft of service, etc.

– some elements handled by independent software

I/O Devices

● device controller– hardware interface to a device

– has its own dedicated processor

– moves data to and from device

● device driver– software interface between OS and device controller

The Kernel

kernel

systemprograms

interrupts

traps

signalsuser mode

kernelmode

privilegedinstructions

dual modeoperation

mode bit

systemcalls

System Programs

● For system maintenance, monitoring, optimization – disk defragmenters, task managers, virus scanners

● For development and execution of programs– text editors, compilers, assemblers, linkers

– debuggers, profilers, loaders

● No clear boundary between system programs and the operating system

Kernel

● The only process that never terminates

● Loaded into protected area of memory when system boots and remains there until system shuts down

● Provides essential services to the rest of the OS– process scheduling

– memory management

Interrupts

● Message to OS from a device: something happened.– completion of an I/O operation

– keystroke or mouse movement

– counter/timer event

● Context switch: OS suspends current process and then transfers control to service routine via interrupt vector.

● Trap: a software-generated interrupt– division by zero

– invalid memory access

Signals (UNIX)

● Asynchronous message from OS to a process– terminate (ctrl-c)

– suspend (ctrl-z)

– process tries to write to a closed pipe

● Process can register signal handlers

● Upcall: lower-level system (e.g., OS) invokes code belonging to a higher-level system (e.g., process)

Privileged Instructions

● CPUs support at least two modes of operation:– User mode is limited to instructions that cannot cause

damage to the system or to other processes.

– Kernel mode is assumed to be executed trusted code. Instructions that can only be executed in kernel mode called privileged.

● access I/O devices, disable interrupts, modify registers or counters used for CPU scheduling.

● If user code tries to execute a privileged instruction, an interrupt is triggered and the OS takes over.

Kernel

● The kernel can be defined as the part of the OS that runs in kernel mode.

● Windows provided kernel-level support for IE.– So the browser runs faster.

– But the trusted codebase becomes much larger, which makes the kernel less secure and less scalable.

● How does the OS maintain control over the system? What prevents a process from running forever? – Hardware support is needed to handle this.

System Calls

● Interface between kernel space and user space

● Asks kernel to execute a privileged instruction

● Usually written in C or C++, or maybe assembly if direct hardware access required.

● Typically traps to a location in the interrupt vector.

System Calls

● File management– creating/deleting, opening/closing, reading/writing

– repositioning the file pointer

– getting/setting attributes (permissions, time stamps, etc.)

● System information– current date/time

– available memory

– available disk space

System Calls

● IPC– message passing

– shared memory

● Device management– UNIX device operations are abstracted away as

operations on files. Single set of system calls for all device operations.

– Other operating systems have distinct system calls for files and devices, but the concepts are merged at the UI level.

System Calls

● You can make a system call from a C program.– syscall function in unistd.h

● But Java was designed for architecture neutrality, so of course it has no system call interface.

● Except it does.– JNI

– native methods

System Calls

● In some cases, thousands of system calls per second. ● Just copying a file involves system calls to:

– prompt user for file names– read user input – open files– read bytes from source file– write bytes to destination file– close files

● APIs raise the level of abstraction away from system calls.

Mode Bit

● Dual-mode operation is realized in hardware by a mode bit that is automatically set to indicate kernel mode whenever an interrupt is triggered.

USER PROCESS

SYSTEMCALL

SERVICEROUTINE

trap

mode bitset to zero

SET MODEBIT TO ONE

Mode Bit

● Dual-mode operation is realized in hardware by a mode bit that is automatically set to indicate kernel mode whenever an interrupt is triggered.

● Enables the OS to executed privileged instructions.

● Before returning control to user space, the OS sets the mode bit to indicate user mode.

● Instruction to set the mode bit is privileged.

Dinosaur Days

● Computers were massive and extremely expensive.

● Each machine had its own OS (or did not have one at all – OS functionality had to be built into programs).

● Not portable: a program that ran on one machine could not run on another.

Batch Processing

● Concept of an OS born in the 1950s.

● Batch processing– Programs ran without human interaction.

– When one process finished, the next was loaded and executed.

● CPU time was very expensive.

● When a process was waiting for I/O, CPU was idle.

Multiprogramming

● Multiple processes execute concurrently.

● When currently executing process needs to wait (e.g., for I/O), it is preempted and control is given to another process.

● Maximizes CPU utilization.

Time-Sharing

● Logical extension of multiprogramming.

● CPU is switched (multiplexed) among multiple processes so rapidly as to provide the illusion to each process of a dedicated CPU.

● Makes interactive applications possible.

● Also called multitasking. Some people distinguish the terms by saying time-sharing when referring to multi-user systems.

Microkernels

● Bare minimum of OS functionality in the kernel– CPU scheduling, memory management, IPC

– Everything else runs in user space

● Easier to extend and port, more secure and reliable.● Slow (communication overhead)● Dynamically-loadable kernel modules in Linux

The macOS Kernel

● Mach microkernel– memory management

– remote procedure calls

– interprocess communication

– thread scheduling

● BSD component– command interpreter

– networking support

– file system support

Hypervisor Mode

HARDWARE

CPURAM DEVICES

HOST OPERATING SYSTEM

Application Application Application Application

VIRTUAL MACHINE

Guest OS

virtual CPUvirtual memoryvirtual devices

Some CPUs support more than two modes of execution.

Code running in hypervisor mode is trusted more than user applications but less than the kernel.

Guest OS

virtual CPUvirtual memoryvirtual devices

Guest OS

virtual CPUvirtual memoryvirtual devices