Title: UNIX
1UNIX
1 History of UNIX 2 Overview of UNIX 3
Processes in UNIX 4 Memory management in UNIX 5
The UNIX file system 6 Input/output in UNIX
2History of UNIX
- MULTICS and UNICS (Thompson)
- PDP-11 UNIX (B C, Richie Tompson)
- Portable UNIX
- Berkeley UNIX
- Standard UNIX
- Linux (Linus Torvald)
3UNIX Goals
- An interactive system designed to handle multiple
pro-cesses and multiple (sophisticated) users at
the same time - Group of experienced users ! personal computer
model - Experienced users want
- simplicity, elegancy, consistency
- power and flexibility
- avoid useless redundancy
4Interfaces to UNIX
UserInterface
- The layers of a UNIX system.
5UNIX Shell (F?????)
- Shell a command line interface ! X Windows
- Faster to use, more powerful, easily extensible
- When the shell starts up initializes it self,
types a prompt character. User types a command
line. Shell extract first word -gt program to
execute. Shell is just another program - Commands may take arguments, e.g. cp a1.txt
b1.txt - Other aspects flags, wildcards e.g. 2 , /?
- Standard input, output, error. Redirection ( lt,
gt). - e.g. sort lt in gt tmp head 30 lt temp rm temp
- Output of a program is input to another. Pipes
() - e.g. sort lt in head 30
- Multiple commands e.g. sort lt in gt tmp . Shell
scripts.
6UNIX Utility Programs
- A few of the more common UNIX utility programs
required by POSIX
7Processes in UNIX
- Processes are created using the fork system call
- Parent and child processes have their own,
private memory images. Open files are shared
between the two processes - How do the processes know which one should run
the parent code and which one should run the
child code?
8POSIX
- Processes communicate with each other
- using a form of message passing (pipes) and with
signals
- The signals required by POSIX.
9System Calls for Process Management
- s is an error code
- pid is a process ID
- residual is the remaining time from the previous
alarm
10POSIX Shell
- A highly simplified shell
11UNIX Processes Implementation
- Two data structures process table and user
structure - Process table (keeps information for all
processes) - Scheduling parameters (process priority, amount
of CPU time consumed recently, sleeping time
recently) - Memory image (pointers to text, data and stack
segments) - Signals (masks showing treatment of signals)
- Miscellaneous (process state, event being waited
for, PID, etc.) - User structure (only if process in memory and
runnable) - Machine registers
- System call state
- File descriptor table (used to locate the
i-nodes) - Accounting (CPU used so far)
12The ls Command
- Steps in executing the command ls type to the
shell
13UNIX Scheduler
- Designed to provide good response to interactive
processes - Two-level algorithm
- Low-level picks the process to run next from the
runnable ones - High level moves processes between memory and
disk - Low level algorithm
- Uses multiple queues, each with a range of
priorities values - Processes in user mode have positive values,
kernel mode negative - A process run for a quantum (usually 100msec) or
until it blocks and then it is put on the end of
the queue - Every second priority CPU_usage nice base
14UNIX Scheduler
- The UNIX scheduler is based on a multilevel queue
structure
15Booting UNIX
The boot program located in the MBR runs first
and loads the O.S.
cp
- The sequences of processes used to boot some
systems
16Handling Memory
Process A
Process B
- Process A's virtual address space (text,data and
stack) - Physical memory
- Process B's virtual address space (text, data and
stack)
17System Calls for Memory Management
- s is an error code
- b and addr are memory addresses
- len is a length
- prot controls protection
- flags are miscellaneous bits
- fd is a file descriptor
- offset is a file offset
18Implementation of Paging in UNIX
- Earlier UNIX systems used swapping (low-level
scheduling)
- On-demand
- No prepaging
- Page daemon
- OS, core map and page frames
The core map has an entry for each page
19Page Replacement in UNIX
- When a page fault occurs and the there is no
available page, process is suspended until page
daemon has freed some. - Page replacement algorithm is executed by page
daemon every 250 msec it checks if free pages gt
lotsfree parameter - If not, it transfers pages to disk so free pages
gt lotsfree - Modified version of the clock algorithm global
replacement - Two-hand clock algorithm the page daemon
maintains two pointers into the core map. When it
runs, it first clears the usage bit at the front
end and then checks the usage bit at the back
end, after which it advances both hands.
20The UNIX File System
- Some important directories found in most UNIX
systems
21The UNIX File System
- Before linking.
- After linking.
(a) Before linking. (b) After linking
22The UNIX File System
- Separate file systems
- After mounting
(a)
(b)
(a) Before mounting. (b) After mounting
23Locking Files
- (a) File with one lock
- (b) Addition of a second lock
- (c) A third lock
- Exclusive and Shared Locks similar to read and
write locks
24System Calls for File Management
- s is an error code
- fd is a file descriptor
- position is a file offset
25The lstat System Call
- Fields returned by the lstat system call.
26System Calls for Directory Management
- s is an error code
- dir identifies a directory stream
- dirent is a directory entry
27UNIX File System
- Disk layout in classical UNIX systems
28UNIX File System
Structure of the i-node
29UNIX File System
- Example n read (fd, buffer, nbytes)
- When kernel gets control, all it has is just 3
parameters. It has to locate the i-node. Also
store the file position. - One of the internal tables file descriptor array
- One possibility have a pointer to the i-node at
the file descriptor and keep file position in the
i-node. FAILS (?) - Another possibility keep file position in the
file descriptor array. What happens if a child
process modifies the file? - Introduce a new table, the open file description
between the file descriptor table and the i-node
table.
30UNIX File System
- The relation between the file descriptor
table, the open file description
31UNIX I/O System
- All I/O devices are made to look like files and
are accessed with the same read/write system
calls. - UNIX integrates I/O devices to the file system as
special files. E.g. /dev/hd1 (hard disk),
/dev/lp1 (printer), /dev/net. - Programs can read/write special files the same
way with regular files. E.g. cp account.txt
/dev/lp1 - Special files
- block sequence of numbered blocks (e.g. disks)
- character input or output character streams
(e.g. keyboard, printer)
32Networking
- Sockets are analogous to mailboxes or wall
sockets and are used for networking - Sockets are created (returns a file descriptor)
and destroyed dynamically - Each socket supports
- reliable connection-oriented byte stream
(equivalent of a pipe between 2 processes) - reliable connection-oriented packet stream
(preserves packet boundaries) - unreliable packet transmission (access to the raw
network no guarantees) - Before a socket can be used, it must have an
address
33Terminal Management
- The main POSIX calls for managing the terminal
34UNIX I/O
- Some of the fields of a typical cdevsw table
35UNIX I/O
- The UNIX I/O system in BSD