Title: Unix Operating Systems
1Unix Operating Systems
2The Unix System - History
- first developed by Ken Thompson (Bell Lab)
- on PDP-7 (16 bit machine)
- Dennis Ritchie joined (MULTICS experience)
- C was developed for Unix
- two ATT organizations
- Bell Labs Research
- 8th edition, 9th edition, Plan 9
- stream IO, RFS,
- USG-gtUSDL-gtATTIS-gtUSO-gtUSL
- System V
- UC Berkeley
- add virtual memory to the 7th edition
- DARPA funding started for a standard Unix
- TCP/IP, The Internet
- influenced SunOS significantly (4.3BSD)
3Commercial Unix
- Sun
- SunOS from BSD
- Solaris from System V
- DEC
- Ultrix, Digital Unix
- OSF/1 (from Mach)
- MS
- Xenix -gt NT
- IBM
- AIX
4Unix Lineage
1st ed.
USG -gt USL
Bell Labs
UCB
6th ed.
1BSD
7th ed.
3BSD
8th ed.
Sys III
4.2BSD
Mach
10 ed.
SunOS 3, 4
SVR 3
4.3BSD
OSF/1
Plan 9
SVR 4
Solaris
4.4BSD
Solaris 2
5Design Principles
- time sharing
- supports multiple processes
- easy process creation
- simple, replaceable user interface
- shell
- file system
- multilevel tree structrure
- IO devices are treated like files
- details are hidden in low level -kernel (device
drivers) - not so much goals in early design
- OS for programmer, not for production
- simple and easy (not performance and
functionality) - interactive (no batch processing)
- C form the original programmers preference
- easy porting -gt wide spread of Unix
- small hardware platform
- entails elegance
6Programming Interface
(the user)
shells and commands compilers and
interpreters system libraries
system call interface to the kernel
signals terminal handling char I/O
system terminal drivers
file system swapping block IO system disk drivers
CPU scheduling page replacement demand
paging virtual memory
kernel interface to the hardware
terminal controllers terminals
device controllers disks and tapes
memory controllers physical memory
7System Calls
- Defines the programmer interface to UNIX
- how to use files
- how to manage processes
- .
- Details of syscall mechanism are hidden from app.
programmers - syscalls are provided as C functions
- classification of syscalls
- process control
- file manipulation
- information manipulation
- device manipulation (similar to files)
8File Manipulation
- file
- a sequence of bytes
- remaining semantics are handled by applications
- directory
- a file containing information on how to find
other files - a path /usr/local/bin/fmaker
- absolute vs relative
- link (ln, link)
- device files
- device interfaces are accesses like files
- /dev/console, /dev/lp0
- character or block device
- file descriptor
- an index into the open file table
9File Related System Calls
- creat, open, read, write, close, lseek
- open(test, O_RDONLY, 0)
- read man for details
- truncate
- need more contol? fcntl(fd, cmd)
- dup fd
- examine and set file parameters
- make succeeding writes append
- need info about file? stat(name, stbuf)
- fills the structure stbuf with informations about
the file name - use fstat() for opened files
10File System Internals
- block size
- if it is big, internal fragmentation
- if it is small, performance degrades
- 4.2BSD has two kinds of block sizes block and
fragment - fragment block is used as the last block of a
file - inodes
- a record that stores most info about a file
- file type plain, directory, c, b, socket
- 12 pointers for direct blocks
- 3 pointers for indirect blocks single -, double
-, triple - - fast for small files while accommodate large
files
11File System Internals
- disk structure
- separation of physical disks from logical file
system - there can be as many file systems as wanted
- a superblock describes a file system
- block allocation
- pre 4.2
- superblock array of inodes data
- free blocks are kept in a linked list
- blocks of a file can be anywhere on the file
system - a copy of superblock is kept in memory for
efficiency - synch between two copies happen every 30 seconds
- cause inconsistecy -gt use synch()
- recovery takes a long time
- needs to check the whole blocks in the file
system
12File System Internals
- Fast File System
- developed at UCB and adopted in 4.2BSD
- a concept of cylinder group
- a group of consecutive cylinders
- efficient when they are accessed at the same(or
near) time - a cylinder group contains
- superblock (it is duplicated all over the
cylinder groups) - fast recovery
- cylinder block
- bit map of free block
- inodes and data blocks
13Buffer Cache
- disk is slow
- cacheing is needed
- The buffer cache (in memory) keeps recently used
blocks - enable the disk manager to sort blocks for better
disk performance - replacement algorithm
- LRU
- consistency problem
- for maximum stable file system, use write-through
- applications may use sync() for safety
- size of the cache
- most influential on performance
- large cache -gt less memory for virtual memory
14Memory Management
- Swapping
- send a whole image of a process to a disk
- used extensively for old Unix (with small memory)
- Paging
- enable more processes run concurrently
- reduces the chance of swapping
- page daemon
- wakes up several times per second
- check if memory is sufficient
- if no, replaces n pages according to the second
chance algorithm until the of free pages
reaches at a certain point - the scheduler decides if swapping is needed
- syscalls
- malloc() - allocate heap space
15Process Control
- fork()
- if ((child_id fork()) 0) / code for
child process / - else / code for parent /
- if you want to load another binary file for the
child process, use - vfork() and
- exec, execve, execv, execp, execl, ..
- if you want parent to meet children use
- exit() and wait() or wait3()
- process synch
- use semaphore
- need to allocate semaphores in shared writable
memory - mmap()
- alarm(), sleep()
- suspend a process for a while
16Signals
- A facility to handle exceptional events
- similar to software interrupts
- there are about 20 signals
- hangup, interrupt, quit, illegal instruction,
sigmentatation fault, bus error, bad args to
syscall, . - 2 user-defined
- can be used for primitive IPC
- usage
- signal (signal_name, handler)
- kill(pid, signal)
- newer Unix has more signals
- 4.3BSD SIGWCH window size has been changed
17IPC
- pipe
- enable two siblings to communicate through
unnamed files - pipe() syscall creates two files
- shared memory
- processes share a region of memory
- shmid shmget(key, size, flag)
- addr shmat(shmid, shamaddr, flag)
- most OS implements the mechanism through VM
- very fast for moving large data
- semaphores
- integer valued object
- semid semget(key, count, flag)
- sema_post(), sema_wait() P, V operations
- socket
- more general interface to communicate with other
processes even on other machines
18Network Layer
user process
system call
return
socket layer
socket
TCP/IP layer
protocol input queue
software interrupt
Network/Interface layer
device (hw) interrupt
ethernet
19Network Programming Using Sockets
main() int sockid, newsockid,
childpid struct sockaddr_in cli_addr,
serv_addr sockid socket(IF_INET, SOCK_STREAM,
0) bzero((char ) serv_addr,
sizeof(serv_addr)) serv_addr.sin-family
AF_INET serv_addr.sin_addr.s_addr
htonl(INADDR_ANY) serv_addr.sin_port
htons(SERVER_PORT) bind(sockid, serv_addr,
sizeof(serv_addr)) listen(sockid, 5) for
( ) / wait for a client connects
/ newsockid accept(sockid, cli_addr,
sizeof(cli_addr)) if ((childpid fork()) 0)
/ child process / close(sockid) serveit(
newsockid) exit(0) close(newsockid)
server side
20Network Programming Using Sockets (2)
main() int sockid struct sockaddr_in
serv_addr bzero((char ) serv_addr,
sizeof(serv_addr)) serv_addr.sin-family
AF_INET serv_addr.sin_addr.s_addr
inet_addr(SERVER_ADDR) serv_addr.sin_port
htons(SERVER_PORT) sockid socket(IF_INET,
SOCK_STREAM, 0) connect(sockid, serv_addr,
sizeof(serv_addr)) askit(sockid) close(sockid)
exit(0)
client
inet_addr() converts IP address (string) to
Internet address (32 bit)
inet_ntoa() reverse of inet_addr() htons()
change byte ordering