Chapter 1 Introducing UNIX
Total Page:16
File Type:pdf, Size:1020Kb
Chapter 1 Introducing UNIX Tien-Hsiung Weng 翁添雄 [email protected] Objectives • Learn what Operating System is • Understand the role of the system administrator • Log in and out of a UNIX system • Find out how UNIX got steadily fragmented by the emergence of other flavors. • Understand the merging of POSIX and Single Unix Specification into a single UNIX standard • Discover UNIX architecture The Operating System • The software that manages the computer’s hardware and provides a convenient and safe environment for running programs. • Act as an interface between programs and hardware resources • Modern OS are multiprogramming – Multiple programs can reside in memory • Time share: OS break up a unit of time into several slices for cycling multiple jobs. • Preemptive vs Non-preemtive – Whether or not the kernel preempts a process when its time expires • Multiuser: Multiple users can run programs. • Multitasking: One user can run multiple programs. The Operating System • OS interacts with hardware and manages programs. • Programs not expected to know which hardware they will run on. • Must be possible to change hardware without changing the programs. • Programs cannot manage themselves. • OS provides a safe environment for programs to run. The Operating System • UNIX programs are designed to work together • Programs to become general-purpose tools, they must be data-independent. • The output of any program should be usable as the input to another. • Programs can be work together in "pipelines". A vertical bar (|) represents the pipe. • Example: – cat file1 file2 | mail joan – ls –a | more – find . –name *\.txt | grep ‘John’ How a Program Runs • OS loads program from disk, allocates memory and CPU. • Instructions in program are run on CPU and OS keeps track of last instruction executed. • If program needs to access the hardware, OS does the job on its behalf. • OS saves the state of the program if program has to leave CPU temporarily. • OS cleans up memory and registers after process has completed execution. Brief overview of UNIX • Unix creators: Ken Thompson and Dennis Ritchie. • Written in C (make UNIX portable) • UNIX get fragmented into BSD (Berkeley System Distribution) and AT&T SVR4 (System V Release 4) • SUN : Solaris • IBM : AIX •HP : HP-UX • DEC : Tru64-UNIX •Linux The UNIX Architecture • Kernel and shell User User Shell Kernel User Hardware The UNIX Architecture: Kernel •Kernel – is the core of the OS. – Program always resides in memory. – Has direct access to the hardware. – Handles file I/O. – Manages processes. – User programs that need to access the hardware communicate with the kernel using a set of functions called system calls. – Manage system’s memory, schedule processes, decide their priorities, etc. – Only one copy shared by all users. The UNIX Architecture: Kernel – When kernel runs a program, kernel: • allocates a small slice of time - up to a second - and lets the program run until that slice is used up, or the program decides to take a rest of its own accord (this is called "sleeping") whether or not the program is finished, it finds some other program to run. • never takes a vacation. • always watching over the system. – UNIX supports preemptive multitasking • The kernel preempts a process when its time expires The UNIX Architecture: Shell • You talk to shell, not to kernel • The shell protects Unix from users • The shell interprets their commands and either executed them directly or passes them onto other programs. A program or command invoked only when the user logs in. Example %cat m??? • The shell finds the filenames, makes a complete list of them, and calls the cat command to ask it to print the expanded list. The cat command calls on the kernel to find each file on the disk and print its contents as a stream of characters on the display. Different kind of Shells • User has a choice of shells: – sh (the Bourne shell) – csh (the C shell) – ksh (the Korn shell) – bash (the bourne again shell) – tcsh (the extended c shell) sh: the Bourne shell (creator: Steve Bourne). – the oldest of the shells, available on most UNIX systems. Some systems have replaced sh with a newer shell, like ksh or bash, that has the features of sh and more.) – a bit primitive and lacks job control features (the ability to move jobs from the foreground to the background). Most UNIX users consider the Bourne shell superior for shell programming or writing command files. Different kind of Shells • csh: the C shell. – developed at Berkeley as part of their UNIX implementation – has a lot of nice features that aren't available in the Bourne shell, including job control and history • Ksh: the Korn shell (creator: David Korn). – compatible with the Bourne shell, but has most of the C shell's features plus some completely new features, like history editing; the ability to recall old commands and edit them before executing them. – more reliable than csh. – a standard part of UNIX SVR4; has also been included in some other UNIX implementations. • Bash: the "Bourne-again" shell developed by the Free Software Foundation. – fairly similar to the Korn shell, has many of the C shell's features, plus history editing and a built-in help command. • tcsh - extended versions of the C shell – works like the original C shell - but with more features Which Shell Am I running? • Can be recognized by by a character in the prompt it displays: – Bourne-type shells usually have $ in the prompt. – The C shell uses %. – But tcsh users often use > • $ echo $SHELL Daemons • Deamons: – An important group of programs – The system is "helpers." – Run from time to time performing small but important tasks like handling mail, running network communications, feeding data to your printer, keeping track of the time, and so on. – We share the computer with the kernel and some mysterious daemons. – For BSD, type ps -aux to see everything that is running, including the daemons – For many other flavors of UNIX, type ps -el. Daemons • Kernel run your programs and the daemons, any other users programs, or any scheduled programs to run automatically. • Any of the programs that are running may write to the disk at any time. • In UNIX, to turn the computer off while it is writing something on the disk. This will probably destroy the disk, and could damage the disk drive. • One of the daemons may access the disk drive every few seconds or so. Just turn a UNIX computer off might do all sorts of damage to the system's files. Daemons • To turn a UNIX system off, need to run shutdown (program), which kicks everyone off the system and makes sure that a daemon will not try to write to disk. • Then run a program named sync, which makes sure that the disks have finished doing everything. Only then is it safe to pull the switch. • When start up the system, it automatically runs fsck (filesystem check program) to find out if the system is properly shut down and fix any damage. Loggin In – (How) Login from Windows: • telnet kitty.cs.pu.edu.tw • ssh (security shell) – From windows: download putty – Then enter userID and password Loggin In and out – (How) Login from UNIX or Linux: • ssh –l UserID kitty.cs.pu.edu.tw • Logout •exit •^z Loggin In – (Shell Setup Files) • When log in to a UNIX system, the login program usually starts a shell. A shell can run in two modes: as a login shell or a non-login shell. •The login program sets a special flag to tell a shell (a login shell).If the shell doesn't have that flag set (a non-login shell). • Opening a new window in a window system may or may not start a login shell (depends on the configuration.) – xterm -ls starts a login shell in an xterm window; – xterm +ls starts a non-login shell. – ftp and uucp usually starts a non-login shell. – a subshell is never a login shell. Loggin In – (Shell Setup Files) • The login shell – starts the non-login shell and make sure that your environment variables are set. – sets the PATH environment variable, TERM, the UID and GID of the terminal amongst other things. • • Upon start-up, login shell invokes two sets of files: – the profile files – non-login 'shell rc' files. • System-wide profile and shell rc initialization files reside in the /etc directory owned by root. System-wide initialization and profile files: – /etc/profile /etc/bashrc • Users' profile and shell rc files owned by the specific user, reside in their home directories and are hidden. – ~/.bash_profile ~/.bashrc Loggin In – (Shell Setup Files) • The profile files contain the initialization data used at login time, hence: – /etc/profile ~/.bash_profile are used by the login shell. • The non-login shell (bash) files are: – /etc/bashrc ~/.bashrc which are run in order to set up variables and other necessaries at shell initialisation. • Only the root user can change the /etc/profile and /etc/bashrc. login and non-login shell • Log in, edit your .bash_profile. Add MYSCHOOL=“Providence U“ , then Save the file and logout. • Login again., Type echo $MYSCHOOL, What happens? Why? • type bash again. In the new shell type echo $MYSCHOOL • What happens? Why? • Start another "bash" shell, type; echo $MYSCHOOL • What happens? Why? • Edit your .bashrc, type HERSCHOOL=“FengChia U" • Save the file and logout, log in again then type echo $HERSCHOOL and echo $MYSCHOOL • What happens? Why? •Type bash; and echo $HERSCHOOL and echo $MYSCHOOL • What happens? Why? •Type bash; and echo $HERSCHOOL and echo $MYSCHOOL • What happens? Why? • Can you explain what is happening in these examples and why? • Exit back to your original prompt. Loggin In – (Shell Setup Files) Bourne Shell • The original Bourne shell has one file that it reads when you log in (.profile) in home directory.