Title: Intro to Unix for Smart People Part I
1Intro to Unix for Smart People Part I
2GE 645
(Artists rendition)
3GE 645 Specs
- Dual-core .000435 GHz
- 3 Mb RAM
- 16 Mb swapping drum
- 136 Mb HD
4Multics achievements
- Modular design
- Memory mapped files
- Dynamic linking
- Hot swappable everything
- Ring oriented security
- First OS to have a hierarchical file system
- Symlinks
- Per-process stacks
5Verdict
- Only supported 35 users
- Performance deemed inadequate
- Verdict Failure
- Bell Labs Were out of here.
6Take 2 Unics
- Bell Labs pulled out of Multics in 1969
- Started work on a simpler alternative
- Unics Uniplexed Information and Computing
System - Also a pun on Eunuchs
- Later name simplified to Unix
7Unix family tree (simplified)
1970
ATT Bell Labs
UC Berkeley
1 BSD
Version 7
1980
Microsoft
4.2 BSD
Unix System III
Xenix
SUN Microsystems
Unix System V
SUN OS
Licenses
4.3 BSD
1990
Linux
Novell
Solaris
SCO
Red Hat
Caldera
2000
SCO Group
Lawsuits
8Strange and wonderful fork()
int pid fork () if (pid 0) / I am the
child process /else / I am the parent
process and pid is my new child/
9exec ()
- exec family of system calls
- execl, execv, execle, execve, execlp, execvp
- Load executable file and replace myself (while
keeping same process identity and resources)
/ goodbye, cruel world / exec (/bin/foo)
/bin/foo
LOAD
/ not reached /
/ process continues with code loaded from
/bin/foo /
10Launching a program
pid_t pid if ((pid fork ()) 0) exec
() / not reached / / child has
been launched parent can wait if desired
/ int status while (wait (status) ! pid)
11Orphans and zombies
- Zombies (defunct processes)
- All processes have entries in a process table
- When child terminates and parent calls wait(),
entry is removed - If child terminates, but parent does not wait()
zombie (dead but not reaped) - Orphans
- If parent dies before child, child becomes an
orphan and is adopted by init - If child exits and parent dies without calling
wait(), child is an orphan zombie - No problem, init periodically checks all its
adoptees and reaps them when they terminate
12Unix system startup
/etc/inittab
getty
fork/exec (respawn)
exec
login
init (pid 1)
exec
sh
fork/exec (respawn)
getty
exec
login
Start other required processes
exec
sh
13Simple structure is very adaptable
- Shell is just a program, not a deep component of
the OS - Anybody can write one!
- Easy to provide computing services to anything
that can send/receive chars - For example telnetd
- Listen on a port
- When an incoming request is received, fork a
handler process and exec login
14A small but working shell
main () while (1) printf ("49
") char line 1024 if ((fgets (line,
sizeof line, stdin) 0) (strncmp (line,
"quit", 4) 0)) exit (0) char
tokens 256 tokens 0 strtok (line, "
\n") int i 1 while (tokens i strtok
(0, " \n")) if (i 255) break tokens
i 0 for (i0 ilt2 i) pid_t
pid int status if ((pid fork ())
0) execvp (tokens0, tokens) perror
("error") exit (1) else
while (wait (status) ! pid)
49 ls foo.txt bar.txt baz.txt foo.txt bar.txt baz
.txt 49
15A history of shells
- Bourne shell (sh) the classic
- Slightly unwieldy for interactive use
- ash - lightweight version of sh
- csh better interactivity, crappy for
programming - tcsh fewer bugs, more features
- ksh Korn shell (great but not free)
- bash Bourne again shell (learn this one)
- rc a shell for Plan9 - simple and clean
- zsh another feature-laden shell
- es an enhanced version of rc
16Bash shell examples
example 1 foo gt bar 2gt1 example 2 foo 2gt1
gt /tmp/out wc example 3 mkfifo /tmp/fifo foo
gt /tmp/fifo 2gt1 ... cat /tmp/fifo example
4 for i in grep -l foo do cp i
i.saved done example 5 find /home/jdm -type f
-iname 'foo' xargs grep bar or even better
(handles filenames with spaces) find /home/jdm
-type f -iname 'foo -print0 xargs -0 grep
bar
17Closing thoughts
- In general Unix desktops are primitive compared
to other popular operating systems - (NeXT and Mac being notable exceptions)
- But its not just about features, ease of use,
and stability - (This better be good)
- Its also about internal interfaces
- Make OS reusable in more contexts
- Create a breeding ground for improvements
- For example
- Accessibility (rewrite the window manager)
- Embedded systems (replace /etc/inittab)
18Unix versus Windows
- Windows Best platform for creating end-user
applications - Unix Best set of building blocks for general
purpose, secure, multi-user, multi-process
computing
19Final closing thoughts (really)
- Windows
- Intelligent design
- Unix
- Mutation and natural selection