Download notes - University of Arizona Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

MTS system architecture wikipedia , lookup

Acorn MOS wikipedia , lookup

RSTS/E wikipedia , lookup

Process management (computing) wikipedia , lookup

Berkeley Software Distribution wikipedia , lookup

Unix time wikipedia , lookup

Plan 9 from Bell Labs wikipedia , lookup

Spring (operating system) wikipedia , lookup

Unix wikipedia , lookup

CP/M wikipedia , lookup

VS/9 wikipedia , lookup

Burroughs MCP wikipedia , lookup

DNIX wikipedia , lookup

Batch file wikipedia , lookup

Comparison of command shells wikipedia , lookup

Unix security wikipedia , lookup

Transcript
Outline
Unix Basics
OS Basics
Shell Basics
Process
I/O, Redirection & Pipe
Stanley Yao
Computer Science Department
University of Arizona
Csc352-Summer03, Stanley Yao
What is Operating System?
OS Concepts
People
Word processor Web browser etc.
Compilers
Shell
Operating System
Machine language
Micro-architecture
Physical devices
etc.
}
OS: protected library of useful functions
OS Functions:
Application
}
}
System
Programs
Hardware
Csc352-Summer03, Stanley Yao
3
Unix
Multi-programmed: OS runs multiple processes
simultaneously
Process: running program
Timeshared: multiple users share the system
simultaneously
5
AT&T: System V
Berkeley: BSD
Sun: Solaris, SunOS
IBM: AIX
etc.
Microsoft Windows
Csc352-Summer03, Stanley Yao
4
OS Families
UNIX is a multiprogrammed, timeshared
operating system
Standard library: provide standard facilities
(abstractions) that everyone needs.
Coordinator: allow several things to work
together in efficient and fair ways (resource
management).
Csc352-Summer03, Stanley Yao
UNIX
2
Win2000, WinNT, WinXP
MacOS
And many many more …
Csc352-Summer03, Stanley Yao
6
1
Unix & C Programming Language
Outline
Unix is written in C
C is a programming language on Unix
Who comes first?
OS Basics
Shell Basics
Process
I/O, Redirection & Pipe
Programming languages .vs. Human
Languages
Csc352-Summer03, Stanley Yao
7
Shell
Interpreter
Environment
A separate program, and not part of the OS
What shells are there in UNIX?
9
Command can be a built-in or a file name
For a file name, where to find the file? PATH
ls /home/yao
cat schedule > mysched
Csc352-Summer03, Stanley Yao
Wait for the
command
from the user
Csc352-Summer03, Stanley Yao
Example:
Child Process
Execute the
command
Command
finished
10
Command Helpers
command [command args] [shell args]
Executables
Shell scripts
Parent Process
Command
finished
Command format
Built-in commands
Programs
C Shell
Bourne Shell
bash
tcsh
etc.
Csc352-Summer03, Stanley Yao
Invented by Bill Joy
Commands
Wait for the
command to finish
Command-line interface to the OS
C-Shell
What is “Shell”?
8
Csc352-Summer03, Stanley Yao
11
“which” command will tell you where the
command is found in the path.
“man” command can get the information
about commands and standard C library
functions.
“command -h” or “command —help” to get
brief summary of the usage of “command” in
GNU release
Csc352-Summer03, Stanley Yao
12
2
Multiple Commands
cmd_1; cmd_2; …; cmd_n
Command Groups
Execute sequentially
Prompt returns when the last one finishes
No waiting among commands
Appropriate for a set of commands that forms a
logical group
Example:
mkdir proposal; cd proposal; ls
cd utils; cc –o gets gets.c; cd ..
Csc352-Summer03, Stanley Yao
13
Csc352-Summer03, Stanley Yao
15
Internal to the shell
set name = value
unset name
Use: $name
Show all variables: set
Examples:
14
setenv VARNAME string
unsetenv VARNAME
Use: $VARNAME
Show all variables: env (or printenv)
Conventionally all cap
Examples:
PATH
HOME
Csc352-Summer03, Stanley Yao
Shell Variables
(cd utils; cc –o gets gets.c)
Environment Variables (cont.)
User environment: special information, e.g.
login directory, your mailbox, your terminal
type, etc.
Environment variables: maintains the special
environment information mentioned above
Can be passed to programs executed from
the shell and affect the programs’ behavior,
e.g. default printer, mailbox, TERM in vi
Csc352-Summer03, Stanley Yao
Environment: the set of characteristics
describing a user’s working area
Creating a subshell
Example:
Environment Variables
(cmd_1; cmd_2; …; cmd_n)
Executing without affecting the current
environment
16
Outline
OS Basics
Shell Basics
Process
I/O, Redirection & Pipe
path: kept sync by shell
noclobber: prevents redirection from overwriting an
existing file. (wc foo.c >! /tmp/out)
status
Csc352-Summer03, Stanley Yao
17
Csc352-Summer03, Stanley Yao
18
3
Unix Process Basics
Process
Standard I/O for Each Process
Process tree
Running program
Run the same program
twice?
login
csh
csh
gcc
httpd sshd orphans
pine Proc_1 pine
Proc_2
Standard I/O connections (file descriptors)
login
ps: ps aux
pstree
Csc352-Summer03, Stanley Yao
Init
Proc_3
19
A typical UNIX process reads its input from
stdin, writes its output to stdout, and any
error messages to stderr.
Csc352-Summer03, Stanley Yao
Inherited from the parent
Shell can control the file descriptors of its
children (redirection)
This is a very powerful feature of UNIX, as it
means the shell can make a process
read/write different things without modifying
the program, e.g. files, devices, another
process.
Csc352-Summer03, Stanley Yao
21
Zero: OK
Non-zero: Error
The parent uses the value to decide what to
do next.
The C-shell stores this value in a variable
called status.
Csc352-Summer03, Stanley Yao
22
Why redirection or pipe?
OS Basics
Shell Basics
Process
I/O, Redirection & Pipe
Csc352-Summer03, Stanley Yao
A UNIX process can return a status value to
its parent when it exits
Outline
20
Process Return Value
Standard I/O for Each Process (cont.)
stdin: standard input (e.g. terminal keyboard)
stdout: standard output (e.g. terminal screen)
stderr: standard error (e.g. terminal screen)
23
You want to store the result in a file instead
of print them out on the screen
You want to prepare the input in a file
instead of typing them every time the
program is run
You want to connect several UNIX tools in a
chain to finish a more complex work. The
data flow through those tools. The output of
a previous tool will be the input of the next.
Csc352-Summer03, Stanley Yao
24
4
Redirection
Output Redirection Summary
Default standard I/O: the same as the shell
Shell arguments in the command line can specify the
change of the standard I/O
Examples:
Write stdout to a file: wc foo.c > /tmp/foo
Append stdout to a file: wc foo.c >> /tmp/foo
Write stdout & stderr to a file: wc foo.c >& /tmp/foo
Ignore stdout: wc foo.c > /dev/null
Read stdin from a file: wc < foo.c
Read stdin from a device: wc < /dev/null
Csc352-Summer03, Stanley Yao
25
Create/truncate
and write
Append
Stdout
>
>>
Stdout+stderr
>&
>>&
Csc352-Summer03, Stanley Yao
Pipe
Pipe (cont.)
cmd_1 | cmd_2 | … | cmd_3
Connect a program’s standard output with
another program’s standard input
Example:
What commands can be connected with
pipes?
who > temp; wc –l < temp; rm temp
who | wc –l
cat dict1 dict2 dict3 | sort +1 | pr
| lpr
cc pcman.c |& more
cc pcman.c |& pr | lpr
Csc352-Summer03, Stanley Yao
26
27
Command on the left must write to the standard
output
Command on the write must read from the
standard input
Example:
ls | rm
Result: Broken pipe line
Csc352-Summer03, Stanley Yao
28
Acknowledgement
John H. Hartman, Classnotes for Csc352Spring03, CS Dept., University of Arizona,
2003
Gail Anderson, Paul Anderson, The Unix C
Shell Field Guide, Prentice Hall, 1986
Andrew S. Tanenbaum, Modern Operating
Systems (2nd Ed.), Prentice Hall, 2001
Csc352-Summer03, Stanley Yao
29
5