Elements of an Operating System
Lecture Objective / Overview
In this lecture, we shall see:
uWhat happens when your computer starts uThe role of memory in a computer system uHow an OS manages multitasking
Slide #2 of 25
Starting your Computer …
u A small bootstrap program is loaded when your computer is started or rebooted
n Based on the saying “to pull oneself up by one’s bootstraps” n A self-sustaining process that proceeds without external help
u First coined in 1953 by Computer Scientist Werner Buchholz when talking about the “self-loading procedure” of the IBM Type 701 Computer
https://ieeexplore.ieee.org/document/4051191
Slide #3 of 25
Starting your Computer …
u Stored in Read-Only Memory (ROM) or Electrically Erasable Programmable Read-Only Memory (EEPROM).
nWhy? Can not be easily infected by a virus
uKnown as firmware
uInitializes all aspects of the system: n CPU registers
n Device controllers
n Memory contents
u Loads the operating system kernel into memory
Slide #4 of 25
Once the Firmware is loaded …
u Some services are provided outside of the kernel nThese are loaded at boot time to become system processes
or system daemons
nThese run the entire time the kernel is running
u On UNIX, the first system process is “init”
u Once this is all loaded, the operating systems sits and waits for events to occur…
Slide #5 of 25
Something happens …
u Examples: A user clicks a mouse or a program tries to access a file
uThese events are called interrupts nCan be triggered by hardware or software.
u Hardware may trigger an interrupt at any time by sending a signal to the CPU
u Software triggers an interrupt by executing a special operation
nA system call
Slide #6 of 25
How does the CPU react?
uStops what it is doing
u Immediately transfers execution to a fixed location
u This usually contains the starting address where the service routine for the interrupt is located
uThe interrupt service routine executes
uOn completion, the CPU resumes interrupted computation
Slide #7 of 25
Interrupt Timeline
Slide #8 of 25
Memory
uAn array of bytes, where each byte is addressable
u Read-only Memory (ROM)
u Electrically Erasable Programmable ROM (EEPROM) u ROM cannot be modified
n Suitable for bootstrap programs and game cartridges(!) u EEPROM can only be changed infrequently
n Most smartphones store factory-bundled programs on EEPROM u CPU needs memory that it can read and write to
n Random-Access Memory (RAM)
n Implemented in a semiconductor technology called Dynamic RAM (DRAM)
Slide #9 of 25
Register / Cache Memory
uRegisters are another type of memory
u Main memory goes away when machine is switched off
u Secondary storage: magnetic disks, optical disks, tapes.
uCache Memory:
n Data that has been used a lot is cached into a faster storage
system
n If CPU is looking for information, cache is first checked.
Slide #10 of 25
Multi-tasking in the CPU
uTime-sharing:
nCPU executes multiple jobs by switching between them
nSwitches occur so frequently that the users can interact with each program while it is running
nThe KDF9 could handle up to 4 completely independent programs at once!
Slide #11 of 25
Time Sharing
uTime sharing: Several jobs are kept simultaneously in memory
u CPU Scheduling: Process of deciding which job is brought to memory to be executed when space is an issue
u Reasonable response time is of utmost importance: nProcesses are swapped in and out of main memory to the
disk
nVirtual memory is used. This technique allows the execution of a process that is not completely in memory!
Slide #12 of 25
Time Sharing
u Virtual memory scheme enables users to run programs that are larger than actual physical memory
u It abstracts main memory into a large, uniform array of storage, separating logical memory as viewed by the user from physical memory
uThis frees programmers from concern over memory- storage limitations!
Slide #13 of 25
OS Structure with Services
Slide #14 of 25
Dual Mode
Slide #15 of 25
Kernel Mode
• monitor executes in kernel mode
• privileged instructions may be executed
• protected areas of memory may be accessed
User Mode
• user program executes in user mode
• certain areas of memory are protected from user access
• certain instructions may not be executed
Why do we need Dual Mode?
u MS-DOS: Intel 8088 architecture which has no mode bit u User program can therefore wipe out the whole OS
u Programs are able to write to a device
u In dual mode: Hardware detect errors that violate modes and handle them with the help of OS
Slide #16 of 25
Why do we need Dual Mode?
u Stops user program attempting to execute an illegal instruction or to access memory of other users
uWhen detected:
n OS must terminate the program
n OS gives error message
n Produces memory dumps by writing to a file.
Slide #17 of 25
System Calls
u System calls are the mechanism through which services of the operating systems are sought.
uWhat language?
nTypically C and C++ nSometimes assembly language
nSystem call for reading data from one file and writing to another file:
$ cp file1 file2
Slide #18 of 25
System Calls
uOpen file1
uPossible error (print, abort)
uCreate file2 (if exists, rewrite/rename)
u Start read and write (errors: diskspace, memory stick unplugged)
uRead/write complete
uClose files uAretheseaccesseddirectly? No-viaAPI
Slide #19 of 25
Example of System Calls
System call sequence to copy the contents of one file to another file
Slide #20 of 25
Types of System Calls
q Process control
§ create process, terminate process
§ end, abort
§ load, execute
§ get process attributes, set process attributes
§ wait for time
§ wait event, signal event
§ allocate and free memory
§ Dump memory if error
§ Debugger for determining bugs, single step execution
§ Locks for managing access to shared data between processes
Slide #21 of 25
Types of System Calls
q File management
§ create file, delete file
§ open, close file
§ read, write, reposition
§ get and set file attributes
q Device management
§ request device, release device
§ read, write, reposition
§ get device attributes, set device attributes § logically attach or detach devices
Slide #22 of 25
Types of System Calls
q Information maintenance
§ get time or date, set time or date
§ get system data, set system data
§ get and set process, file, or device attributes
q Communications
§ create, delete communication connection
§ send, receive messages if message passing model to host name or process name
§ From client to server
§ Shared-memory model create and gain access to memory regions
§ transfer status information
§ attach and detach remote devices
Slide #23 of 25
Types of System Calls
q Protection
§ Control access to resources
§ Get and set permissions
§ Allow and deny user access
Slide #24 of 25
Examples of Windows and Unix System Calls
Slide #25 of 25
System Calls : API Wrapper
uApplication Programming Interface (API)
u Specifies a set of functions that are available to an
application programmer
nIncludes the parameters passed to each function
nAlso includes the return values that the programmer can expect
u Programmer accesses API via a library of code provided by the OS
u Example: Windows API for Windows systems
n CreateProcess() invokes the NTCreateProcess() system call in
the Windows kernel. This return value 0 or 1, if error thrown.
Slide #26 of 25
System Calls : API Wrapper
u POSIX API for POSIX-based systems (UNIX, Linux & macOS)
nProgrammer accesses an API via a library of code provided by the OS
uExample: read()
u Input:
nint fd: file descriptor to be read
nvoid *buf: pointer into buffer to be read into nsize_t count: maximum number of bytes to read
u Output:
nnumber of bytes read, if successful n-1 if failed
Slide #27 of 25
System Calls : API Wrapper
Example:
u Java API for programs that run on the Java virtual machine
u getParentFile() uinvoked on a file object u Output:
nReturns the abstract pathname of this file’s parent, or null if this pathname does not name a parent directory.
nJVM uses the OS system calls.
Slide #28 of 25
Why use an API?
u Why not invoke system calls directly?
nProgram portability: program can compile and run on any
system that supports the API
nSystem calls can often be more detailed and difficult to work with
nGive access to high level objects (Java API)
u For example, think of interfaces in Java
u Using system calls is like implementing an interface
Slide #29 of 25
Summary
What elements of the OS have we looked at? uThe Role of Memory
u Multitasking / Time Sharing uSystem calls
u APIs
Slide #30 of 25
References / Links
th u Chapter # 1: Operating System Concepts (9
edition) by Silberschatz, Galvin & Gagne
Slide #31 of 25