Title: Uppsala University Operating Systems
1Uppsala UniversityOperating Systems
UNIX Brahim Hnichhttp//www.csd.uu.se/brahim/os
1.html
2Overview
- History and Evolution
- Overview of the UNIX system
- Fundamental concepts
- Key system calls
- Implementation of UNIX
3Portable UNIX
- UNIX was written in C
- moving it to a new machine (porting it) is easy
- C compiler for the new machine
- writing device drivers for the new machine I/O
- small amount of machine-dependent code
- interrupt handlers
- memory management routines (assembly language)
- Portable C compiler could be retargeted to
produce code for any reasonable machine with very
few effort
4Berkley UNIX
- University of California at Berkley acquired UNIX
- modified the system substantially
- introduced virtual memory, paging,
- implementation of file system changed much
faster - signal handling was made more reliable
- Networking introduced, TCP/IP standard
- added substantial number of utilities
- new editor (vi)
- new shell (csh)
- Pascal and Lisp compilers
- Berkley UNIX became well established in the
academic, research, and defense worlds
5Standard UNIX
- By late 80s two different, and quite incompatible
versions of UNIX - The first serious attempt to reconcile the two
flaws, was by IEEE standard board (1003.1) - attempt to standard failed
- Every group is developing UNIX in his own way
- gt UNIX became huge and complicated, rather than
small and simple!!
6UNIX Goals I
- UNIX an interactive timesharing system
- designed by programmers, for programmers
- sophisticated!
- Engaged in complex system development
- UNIX allows people to work together and share
data in controlled ways - different from the personal computer model of a
single beginner working alone with a word
processor
7UNIX Goals II
- What a good programmer requires from the system?
- simple
- elegant
- consistent
- examples
- ls A
- rm A (principle of least surprise)
- grep ard f
8Interfaces to UNIX
USERS
User interface
User Mode
Standard utility programs (shell, editors,
compilers, etc)
Library interface
Standard library (open, close, read, write, fork,
etc)
System call interface
UNIX operating system (process management, memory
management, the file system, I/O, etc)
Kernel Mode
Hardware (CPU, memory, disks, terminals,etc)
9Logging Into UNIX
- To use UNIX, you first login
- login
- password
- Necessary to provide security
- resources
- file system
- Doesnt keep all user names and passwords in
secret files - UNIX scheme
- the password file contains one entry for each
user (login, ID, encrypted password, home
directory,) - when user logs in, password is encrypted
- compares encrypted password with password file
- () eliminates the danger that the password file
will leak somehow
10UNIX Shell
- After a successful login, the shell initializes
itself and types a prompt character - When the user types a command line
- the shell extracts the first word from it
- assumes it is the name of a program to be run
- searches for this program, if found executes it
- A program, like the shell, does not need to open
the terminal in order to read from or write to - standard input
- standard output
- standard error
- Redirection of input/output
- e.g. sort ltin gtout
- Shell scripts files containing shell commands
11Files and Directories in UNIX
- A UNIX file sequence of bytes containing
arbitrary information - No distinction between ASCII, binary, or any
other kinds of files - The meaning of the bits in a file is entirely up
to the files owner - Files can be protected by 9 bits right bits
- Files can be grouped in directories
- Directories are stored as files, and hence can be
treated as files - Directories can contain subdirectories
r
r
r
w
x
w
w
x
x
Everyone else
group
owner
12UNIX Utility Programs
- Files and directory manipulation commands
- Filters
- Compilers and program development tools
- Text processing
- System administration
- Miscellaneous
13Processes in UNIX I
- The only active entities in a UNIX system are
processes - Each process runs a single program
- UNIX is a multiprogramming system multiple
processes - Even when a logged user user is absent, multiple
background processes are running daemons - e.g. the cron daemon wakes up once a minute, doe
ssome work, the goes to sleep - Fork command creates processes
- e.g. pid fork()
14Processes in UNIX II
- The ability to form a tree of processes gt
timesharing - Processes in UNIX communicate with each other
using a form of message passing - It is also possible to create a channel for
communication between 2 processes pipes - e.g. sort ltf head
- Processes can communicate by software interrupts
- process sends a signal to another process
- processes can tell the system what they want to
happen when a signal arrives - Each user is identified by an integer uid
(password file), every process automatically
acquires uid - Superuser uid 0 (root) has the power to read
or write all files in the system
15UNIX Memory Model
- Each UNIX process has an address space consisting
of 3 segments - text segment machine instruction
- data segment storage for the programs
variables, - initialized part (Data)
- uninitialized part (BSS)
- stack segment starts at top of the virtual
address space and grows down wards - UNIX supports shared text segments
16UNIX File System
- accessing files
- open file
- OS checks access rights
- return a file descriptor, if access is allowed
- absolute path
- relative path to the working directory
- linking is allowed in UNIX
- locking to implement mutual exclusion without
semaphores - specify the file to be locked
- the starting bytes
- the number of bytes
- two kinds of locks
- shared
- exclusive
17Input/Output in UNIX
- UNIX solution to access devices, is to integrate
them into the file system special files - Each I/O device is assigned a path name (/dev)
- These special files can be accessed the same way
as any other files - e.g. cp file /dev/lp
- Usual file protection applies to I/O devices
- Special files
- block special files
- character special files
18UNIX System Calls
Process Management
Files and Directories Management
pid fork()
Create a child process
fdcreate(name,mode)
Create a new file
swaitpid(pid, status, opts)
Wait for a child to terminate
fdopen(name,how)
Open a file for reading or writing
sexecve(name, argv, envp)
Replace a process core image
Close an open file
sclose(fd)
exit(status)
Terminate execution
nread(fd,buffer,nbytes)
Read data from file into a buffer
ssigaction(sig, act,oact)
Specify action to take for a signal
nwrite(fd,buffer,nbytes)
Write data from buffer to file
skill(pid,sig)
Send a signal to process
poslssek(fd,offset,whence)
Move the file pointer somewhere
residualalarm(seconds)
Schedule a SIGALRM signal later
sstart(name,buf)
Read and return info. about file
pause()
Suspend the caller unit until next signal
smkdir(name,mode)
Create a new directory
srmdir(name)
Delete an empty directory
slink(name1,name2)
Create a new directory entry for an old file
Memory Management
sunlink(name)
Remove a directory entry
sizebrk(addr)
Set the size of data segment
schdir(dirname)
Change the working directory
schmod(name,mode)
Change a files protection bits
Input/Output Management
scfsetospeed(termios, speed)
Set the output speed
scfsetispeed(termios, speed)
Set the input speed
scfgetospeed(termios, speed)
Get the output speed
scfgetispeed(termios, speed)
Get the input speed
stcsetattr(fd,opt,termios)
Set terminal attributes
stcgetattr(fd,termios)
Get terminal attributes
19UNIX Implementation
- Machine-dependent kernel
- directly drives the H/W
- has to be rewritten from scratch whenever UNIX is
ported to a new machine - interrupt handlers
- low-level I/O system device drivers
- part of the memory management S/W
- Machine-independent kernel same for all machines
- system call handling
- process management
- scheduling
- pipes
- paging and swapping
- file system
- high-level part of I/O system
20Implementation of Processes in UNIX
- Every process has two parts
- user part
- kernel part
- becomes active only when a system is invoked
- it has its own stack
- it has its own program counter
- Kernel maintains two key data structures
- process table
- resident all time in memory
- contains information needed for all processes
- user structure
- paged out or swapped, when its associated process
is not in memory - process table information
- scheduling parameters
- memory image
- signals
- user structure contains info. that is not needed
when process is not in memory - machine registers
- system call state
21Process Creation
- Fork is executed gt trap to the kernel
- kernel looks for a free slot in the process table
for use by the child - copies all the information from parents process
table entry to childs entry - allocates memory for the childs data and stack
segment (exact copy of the parents segment) - the user structure is kept adjacent to the stack
segment, and copied along with it - child is ready to run
22Scheduling Algorithm
- Two-level scheduling algorithm
- low-level algorithm picks the process to run
next from the set of processes in main memory - high-level algorithm moves processes between
memory and disk
23Implementation of Memory Man. in UNIX Swapping
- Swapping
- swapper it is the high-level scheduler, which
handles movement between memory and disk - events that lead to swapping
- a fork system call needed memory for a child
process - a BRK system call needed to expand a data segment
- a stack became larger and ran out of the space
allocated to it - free storage in memory and on the swap device is
handled by linked lists of holes - uses first-fit
24Implementation of Memory Man. in UNIX Paging
- Processes need not be entirely in memory to run
- The only required information for a process to
run - page tables
- user structure
- The pages of the text, data, and stack segment
are brought in dynamically, once at a time, as
they are referenced - If the user structure and page table are not in
memory, the process cannot run until the swapper
brings them in - Paging is implemented partly by the main kernel,
and partly by a new process called page daemon - Page daemon is started up periodically so it can
look around to see if there is any work for it to
do - If page daemon discovers that the number of free
pages in memory is too low, it initiates action
to free up more pages - when page fault occurs, OS checks if there exist
free page frame in the free list, otherwise the
process is suspended until the page daemon has
freed a page frame - Page replacement algorithm global alg.
- executed by the paging daemon
- modified version of the clock alg.
25Implementation of the UNIX File System
- I-nodes (UNIX)
- associated with each file is a little table
called I-node (index-node)
Addresses of data blocks
Addresses of data blocks
I-node
Single indirect block
Attributes
Addresses of data blocks
Disk addresses
Double indirect block
Triple indirect block
26Implementation Input/Output in UNIX
- I/O in UNIX is implemented by a collection of
device drivers - By providing standard interfaces between the
drivers and the rest of the OS, most of the I/O
system can be put into the machine independent
part of the kernel - I/O system is split into two patrs
- handling of the block special files
- handling of the character special files
Reading/writing files
cooked interface raw interface
User Space
File system
Line disciplines
Buffer cache
Kernel
C-list
Disk driver
Terminal driver
Keyboard
Disk
Terminal
27Summary
- History and Evolution
- Overview of the UNIX system
- Fundamental concepts
- Key system calls
- Implementation of UNIX
- Whats next?
- I go to sleep, you prepare for your exam! GOOD
LUCK and hope not to see you in this course again
o)