Intro to Unix for Smart People
Download
Report
Transcript Intro to Unix for Smart People
Intro to Unix for Smart
People Part I
Joe Morrison, Lab49
GE 645
(Artist’s rendition)
GE 645 Specs
Dual-core .000435 GHz
3 Mb RAM
16 Mb swapping drum
136 Mb HD
Multics 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
Verdict
Only supported 35 users
Performance deemed inadequate
Verdict: Failure
Bell Labs: “We’re out of here.”
Take 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
Unix family tree (simplified)
1970
AT&T Bell Labs
UC Berkeley
1 BSD
Version 7
1980
Microsoft
Xenix
Unix System III
4.2 BSD
SUN Microsystems
Unix System V
SUN OS
Licenses
1990
4.3 BSD
Linux
Novell
SCO
Caldera
2000
SCO Group
Lawsuits
Red Hat
Solaris
Strange 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*/
}
exec ()
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”);
LOAD
/* not reached */
/bin/foo
/* process continues with code loaded from /bin/foo */
…
Launching 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)
;
Orphans 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
Unix system startup
/etc/inittab
fork/exec
(respawn)
init (pid = 1)
getty
exec
login
exec
sh
fork/exec
(respawn)
getty
exec
login
Start other required processes
exec
sh
Simple 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
A 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 (i=0; i<2; i++) {
pid_t pid;
int status;
if ((pid = fork ()) == 0) {
execvp (tokens[0], 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:
A 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
Bash shell examples
# example 1
foo > bar 2>&1
# example 2
foo 2>&1 > /tmp/out | wc
# example 3
mkfifo /tmp/fifo
foo > /tmp/fifo 2>&1 &
...
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
Closing thoughts
In general Unix desktops are primitive compared to
other popular operating systems
(NeXT and Mac being notable exceptions)
But it’s not just about features, ease of use, and
stability
(This better be good)
It’s 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)
Unix 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
Final closing thoughts (really)
Windows
Intelligent design
Unix
Mutation and natural selection