CSCI 4061 Introduction to Operating Systems
Instructor:
Today
OS Overview
OS Structure and APIs Unix Overview
2
3
What is an Operating System?
User’s View: Extended Machine
Programmer’s View: Resource Manager System View: Control Program
Applications
Operating System
Hardware
4
User’s View
Extended Machine
Simple abstraction of hardware resources
CPU -> Processes, Threads
Memory -> Virtual Memory
Disks -> Files
Network interfaces -> Sockets
Goal: Simple, easy to use
1
5
Programmer’s View
Resource Manager
Efficient division of resources among multiple users, programs
Multiple processes on same CPU
Multiple files on the same disk
Multiple connections on same network link
Arbitrate conflicting demands
Goal: Maximize system performance
6
System View
Control Program
Handle different events, user inputs, etc.
User typing commands on keyboard
Bytes being read from the disk
Packets arriving on the network interface
Multiple concurrent and asynchronous events Goal: Ensure correctness and fairness
7
What services does the OS provide?
Allows different applications to execute concurrently
Processes, Memory management
Allows access to multiple files, user input, display
File system, File I/O
Allows parallelism and data sharing
Threads and synchronization
Enables communication across machines
Networking and sockets
8
Course Road Map
Understand different OS components Processes
File System
Memory Management Threads
Concurrency and Communication Thread Synchronization
Networking and IPC
Signals
2
General Operating System Structure
Hardware-OS interactions OS-Application interactions
Applications
Operating System
Hardware
9
Kernel
Core of the Operating System
Provides the main functionality:
10
Process and Thread Management Memory Management
File System and I/O
Inter-Process Communication
Hardware-OS interactions
We would mainly worry about hardware abstractions
E.g.: Processes, files, virtual memory, sockets Control allocation and management of CPU, disks, devices, memory, network interfaces
Portability across variety of hardware
Asynchronous events and concurrency. E.g.: interrupts, I/O events
11
OS-Application interactions
Shells and User interfaces
Allow users to interact with the OS
Libraries
Allow programs to use common services
System calls
Direct conduit into the OS
Signals
OS interacting with user programs
12
3
Key Questions
Q.1: How does the OS protect the hardware from (unruly or malicious) applications?
Q.2: How do applications get access to desired resources (CPU, memory, disk, etc.)?
13
Kernel Mode
User Mode
Kernel Mode
OS runs in kernel mode: hardware-enabled Higher privileges than user mode
Access to hardware resources Access to protected memory Access to OS data structures
Tighter control, security of system resources 14
Applications
Operating System
User Mode
User Mode
Kernel Mode
Applications, utilities, shell run in user mode Restricted access to
System resources
Kernel data structures Protection boundaries
15
Applications
Operating System
Application-OS Interaction: Example Scenario
Suppose an application needs to read data from a file
How does the application tell the OS to read the data for it?
16
4
System Calls
User Mode
System Call Result Kernel Mode
Kernel API: well-defined, small set of operations Entry points into the kernel
Provide restricted access to the kernel
17
Applications
Operating System
System Call Operation
User program executes a TRAP instruction Switches to kernel mode
Passes parameters, system call no.
Kernel looks up system call table System call handler is invoked
Results returned to user program
18
User Mode Kernel Mode
Parameters, Syscall no.
Result
Trap Syscall Table Dispatcher
Syscall Call Example: read
E.g.: read 16 bytes from file F into buffer Parameters: (F, buffer, 16)
User program passes:
Parameters, read syscall no. (3)
User program executes TRAP
Kernel looks up code for syscall no. 3
Kernel executes code for read system call
Look up F (File system)
Transfer 16 bytes from disk to buffer (I/O) System call returns control to user space
19
System Call Implementation
Requires performing TRAP instruction Requires passing parameters to kernel
In registers or in buffers
Requires getting back control from kernel May be implemented in assembly language
How to use in programs easily?
20
5
Libraries
Pre-written and pre-tested functions
Programmers can call library functions in their
programs Two types:
21
Wrappers for system calls. E.g.: read library call
User-level utility functions. E.g.: String operations (strcpy, strcmp, strlen, …)
Unix Overview
22
What is Unix?
Highly Popular OS
Programming Platform/Toolbox Many variants exist
Multiple implementations, similar functionality Principles:
Multiprogramming Flexibility
Extensibility
23
Unix Evolution
MULTICS (1965): MULTiplexed Information and Computing Service
UNIX (1969): Developed at Bell Labs Developed in C
Architecture-independent
Several Evolution Paths:
AT&T System V (1983) -> SCO Unix
BSD (1980) -> FreeBSD, NetBSD
Linux (1991-) -> Android
Other variants: Solaris, HP/UX, IBM AIX, S/X
24
6
Common Features and Variations
Common:
Similar APIs and basic tools POSIX Standards
Variations:
Underlying implementation
Shells, tools, and user interfaces
25
Operating System Structure
Applications
Operating System
Hardware
26
Unix Structure
Applications, Utilities, and User Programs
Shell
Libraries
System Call Interface
Kernel
CPU, Memory, Disks, Devices
AUI (Application User Interface)
API (Application Programmer Interface)
Operating System
Hardware
27
Shell
Basic interface to the OS
Allows users to interact with the OS
Command interpreter
Parses and executes several commands
Many Unix Shells
Bourne sh, csh, tcsh, ksh, bash
Shell is just another user-level program No special privileges
28
7
Shell as Command Interpreter
Read-eval-print
Read user input
Evaluate the input and execute command(s) Print output
Example: ls –l
User types above command
Shell reads and parses the input
Shell invokes “ls” program with argument “-l” Shell prints the output produced by the program
29
Standard I/O
Standard input (stdin): source of input data for a command or program
Default: Keyboard
Standard output (stdout): destination of output
from a command or program Default: Display
Standard error (stderr): destination of error messages
Default: Display
30
Shell Redirection
Redirect the input or output of a command to a file
Input redirection (<): Takes input from a file instead of keyboard
sort < file
Output redirection (>): Send output to a file instead of display
ls -l > file
cat file1 >> file2
31
Shell Plumbing
Pipes: prog1 | prog2
Allow multiple commands to be linked together
Connects output of prog1 to input of prog2
Examples:
ls -l | more
cat foo | sort | head
How will you do the above using only redirection?
Most UNIX commands consume and produce plain text data
32
8
Unix File System
Tree-based hierarchy
File system has a root (/)
Each user has a home directory ($HOME) There are several standard directories:
E.g.: /bin (binary files), /dev: device files, /lib: libraries, …
33
Unix Directory Hierarchy
/
bin dev etc home lib usr tmp var
cp ls
34
chandra
bin myprog
cc
bin include lib
which libc.so libm.so
9