Announce ------------ Office Hours: M and Th 2 - 3 or by appointment, prefer afternoons TA: Lin Tan Today: Get comfortable with using CSIL Linux machines Cover high level OS issues If we get to it, common UNIX commands Lecture 2: UNIX / UNIX tutorial -------------------------- UNIX/OS -------- - UNIX describes an Operating System - same realm as windows, DOS, MacOSX ** - OS is software that is - device manager - hd, cdrom, network, video card - all plugged in and turned on but OS handles them all - file organization - most OSes have system for organizing files (directories/folders) - program/process controller - all running programs are managed by the OS - input/output manager - accepts input from keyboard/mouse - handles outputting to screen/printer - many UNIX OSs - AIX, IRIX, Linux, FreeBSD, Darwin - all UNIX because they share common features/interfaces - file access, process model, user model, programming model ** - key concept for understanding UNIX usage is the notion of a PROCESS - everything is: - telling OS to execute a process - interacting with process that is already executed - primary job of OS is to manage running processes. - many processes can appear to run at same time, but in reality are running in serial, OS is switching very fast between processes. before talk about the process Draw picture of abstract computer Keyboard Monitor ^ ^ / | / CPU <--> OS <---> Memory | | Disk The Process ------------ - any program OS is currently running is a process ** - a process has a lifeline 1.) FILE: file on disk, binary - binary contains data in form of instructions that OS can feed to CPU 2.) LOAD: file is loaded, becomes a process. - OS is told to execute process by user or other program - OS copies from disk to memory - each process, at this point, is unique - multiple instances of one binary 3.) EXECUTE: process executes purpose - tell OS to do something - wait for OS to tell it something - interact with devices - interact with user - read/write files - just run code on CPU 4.) EXIT: process exits, OS cleans it from memory - memory is freed for some other process Example: the 'ls' unix command ------------------------------ - ls lists files in a directory, like DOS command dir ** do on computer picture - step 1 there is a file called 'ls' on the disk, marked as executable - user types in 'ls /tmp' - the 'ls' file is found, OS loads the file data into memory with an argument of '/tmp' - we have now created a process, which is in step 2 of the process lifetime - at this point, another user types 'ls', there will be 2 separate 'ls' processes in memory, each originated from the file but are unique processes - step 3 in lifetime, ls process fulfills it's purpose - all of this is being done on the CPU constantly - process inspects the given argument '/tmp' - makes sure argument is a directory - queries the OS for information about files located on disk in directory - waits for OS to tell it what it finds - OS queries the disk, device manage - OS tells process what it found - 'ls' process decides how to display the information - ls tells OS to print information to the video card - ls is now in step 4, tells OS that it is exiting - OS frees the memory devoted to 'ls' process, process is dead Why did we just learn all of that? ----------------------------------- - illustrate the importance of the OS - once understood that all is process, clears up the way users interact with UNIX - when sitting at unix machine, try to think of everything you see and do as a process in some stage of it's lifetime UNIX user model --------------- - if everything is a process, and the user tells the OS what to run, what process is accepting input from the user and how did THAT process get started? - OS boots, starts one process on user's behalf called 'init' * start picture of process tree - init then runs a process that accepts user logins - finally a user 'shell' is executed, which is a process that allows the user to tell the OS to run other processes. - bash, csh, tcsh, sh - command interpreter, like cmd.exe or command.com or DOS - shell can run other shells! - when original shell is exited (step 4), user is logged out - user's presence is one big process tree, starting with original shell - many users can all have process trees at the same time, processes tagged with user ID - so far abstract notions of OS and UNIX machines. now useful linux stuff Linux tutorial ------------- - log in: enter user name and password given to you - OS starts a shell, which you do not see - system is set up to run some processes on your behalf - X windows/window manager - maybe xterm, if not, look around for xterm - xterm is a process that starts a window with a shell running inside of it - once you have an xterm (shell), you can start executing processes - run some programs - SHELL----- - ps - ls - echo - date - uname -a - hostname - uptime - X------ - xcalc - xclock - xeyes - xterm - quit x processes by killing the window - you'll notice that when you run an X program (or any program that doesn't exit) your shell prompt does not return until the process is finished - when process is executed, control is passed to that process - take a look at the filesystem - users have their own space - ls, ls -l, pwd - pretty much files and directories - files/directories can have arbitrary names and 'extensions' are not required and in fact ignored - case sensitive! - filesystem laid out in tree structure beginning with / - paths are specified by sequences of directory names delimited / - SHOW EXAMPLE - can be fully or relatively specified - from anywhere: ls /home/nurmi/foo.txt - from /home: ls nurmi/foo.txt - shortcuts . and .. - file and permissions - 10 characters at the left when ls -l - first bit is usually - or d - last nine are split into groups of three for RWX bits or flags that can be set or not - 234 are RWX bits for owner rights - 567 are RWX bits for files group rights - 890 are RWX for all other user rights EX: -rw-r--r-- default file perms drwxr-xr-x default dir perms -rwxr-xr-x deafult binary perms - files - ls -l - file - cat - directories - ls -l - d--------- - mkdir, rmdir, cd - running programs revisited - when you type in 'ls', the shell looks for a file named 'ls' - shell variables: PATH - echo $PATH - if an executable is in a directory not specified in PATH, ./ - env - special shell syntax - we've learned how to run commands - shell has special characters to deal with: - where input is coming from - cat by itself waits for input from keyboard followed by CTRL-D - cat < /etc/passwd - where output is going to - by default, to screen - date > - date >> - how to link two processes together - pipe commands together - cat outputs data - wc by itself waits for input - cat | wc - env | more - job control - whether to run the program in the forground or not - by default in foreground - & - [ENTER] CTRL-Z - bg - fg - jobs - how to kill a process - if program is in foreground, CTRL-C - if program is in background or not under current shell control: kill - getting help: man - man command, space to page down, q to quit - man C function - other useful commands - grep - emacs - vi - top - ps -ax - ls -al - sort - uniq - mozilla - pine - remote access/login - ssh from unix box to unix box csil.cs.ucsb.edu - free windows program called 'putty' - macOSX just open a terminal and use ssh ------------------------------------------------------------------------- Programming in the UNIX environment ------------------------------------ development cycle - 1.) create a program file (foo.c) 2.) use editor to input/edit program code into the file (vi foo.c) 3.) save the file to disk 4.) compile the code into a binary (gcc foo.c -o foo) 5.) execute binary file (./foo) 6.) goto 2 editors - vi,emacs,pico - for example, vi - run : vi somefile - use arrows to move cursor around in text file - type 'i' to enter editing mode, ESC to leave editing mode - type ':' to enter command mode - type w /path/to/file in cmd mode to save - type q in cmd mode to quit - type q! or w! to override warnings - type wq! to save and quit without warnings - have two xterms, one with editor, one for compiling/running - gcc creates binaries, also generates warning and errors if code is not correct in some way