CISC 220: System-Level Programming instructor: Margaret Lamb e-mail: malamb@cs.queensu.ca office: Goodwin 554 office phone: 533-6059 (internal extension 36059) office hours: Tues/Wed/Thurs 2-3 (this week only) course web site: research.cs.queensu.ca/home/cisc220 my home page: research.cs.queensu.ca/home/malamb labs start next week (none during week 1) Web page contains the above information plus: these slides, audio/video more administrative info 1
What's This Course About? From Calendar: Basic concepts of Unix-like systems. Shells and scripting. System-level programming in the C language. Software development tools and techniques. Useful Skills: Linux & C programming. Concepts Include: command-line interfaces, shell scripting, processes, signals, pipes, memory management 2
Marking Scheme assignments: 20% (best 6 of 8) 3 quizzes: 40% each (best 2 of 3) final exam: 40% up to 2 bonus points possible (see web site) Warning about final exam scheduling: don t make travel plans yet! Quizzes & Final Exam are closed book -- but will include summary sheets 3
More Administrivia web site (admin, slides, labs, audio/video) Moodle Academic Integrity prerequisites & backgrounds assignments (marked & unmarked) labs (starting next week, work on assignment 1) text: in bookstore special accommodations, excuses attendance learning, getting help 4
Operating Systems Operating System = software that manages what happens on a computer: programs memory file system hardware & external devices diagram from wikipedia 5
Unix History Originally developed at Bell Labs (AT&T) 1960s: Bell & others working on Multics Operating System (Multiplexed Information and Computing Service) some good ideas, but poor performance, never commercially successful 1970s: Bell employees created simpler, efficient variant: "Unics" (Uniplexed Information and Computing System) spelling later changed to "Unix" originally written in assembly 1973: re-written in C, so more portable Unix has evolved since then 6
Kernel "kernel" of a Unix or Unix-like system: the basics file system device drivers memory management ability to load & run programs Kernel doesn't include a user interface Unix or Unix-like system includes kernel plus: text-oriented user interface ("shell") often a GUI interface as well utility programs & libraries for accessing kernel functions other tools such as compilers 7
Linux & GNU Linux = Unix-like kernel written by Linus Torvalds GNU project = Unix-like libraries, utilities, compilers & other programs recursive acronym: "GNU's Not Unix" CASLab's "Linux" system: Linux kernel + GNU software more correctly named GNU/Linux CASLab Linux is Ubuntu verison 10 GNU & Linux are open-source software 8
How To Get To Linux 1. dual-boot machines in labs 2. install Linux on your own computer alone or dual-boot 3. putty or NXwindows to CASLab Linux machines from anywhere on the Internet 4. boot from Linux Live DVD 5. Macintosh terminal = Linux-like For 220, your scripts & programs must run on CASLab Linux. Scripts must work with bash. 9
Text vs. GUIs Older Unix systems: user communicates via a "shell" program. CLI = Command Line Interface Most newer Unix-like systems also have a GUI interface (point & click like Windows) Advantage of GUIs over CLIs? CLIs over GUIs? 10
Linux Shells "Shell" = text program for communicating with the OS Original shell: "sh" (Bourne shell) Many other versions: csh: C shell ksh: Korn shell bash: ("Bourne-again shell") For CISC 220: use bash! 11
The Shell: Make Sure You're Running Bash command: echo $SHELL result should be /bin/bash if not: use CASLab account management tool 12
Linux File System Hierarchical file system Directories (folders) contain files & other directories. Separate directory & file names with forward slash: / No device names as in Windows (a:, c:, etc.) Root of file system is /. File/Directory names are case sensitive Special file names:. : current directory.. : parent directory ~ or $HOME: your home directory Some special system files & directories start with "." Commands for moving between directories: cd, pwd, pushd, popd 13
General Structure of Linux Commands command = sequence of words, separated by spaces first word = name of command remaining words = arguments arguments starting with a dash ("-") are "flags" command may be: basic command built into shell utility program provided with GNU other program (shell script or executable program) 14
Listing File Information: ls Command arguments should be names of files and/or directories for each file: lists the file for each directory: lists contents of directory no arguments: equivalent to ls. flags: -a: include files & directories starting with "." -d: for directories, show directory itself instead of contents -l: (lower-case L) long format (lots of info) -R: list sub-directories recursively -1: (one) list each file on separate line (no columns) may combine flags: ls -al 15
Wildcards Use wildcards to specify several file names. * = any sequence of zero or more characters? = any single character [abc] = a or b or c [a-f] = single character in range a through f Note: wildcards won't match an initial "." 16
Online Command Information man command: information about command 17
More Useful Commands cat (concatenate): show contents of file less: show contents of file one screenful at a time rm: remove files cp: copy a file mv: move a file (rename) mkdir: create a directory rmdir: remove a directory exit: end current shell logout: ends Linux session (if login shell) echo > filename: creates new, empty file 18
Text Editors: Creating & Changing Files Many Linux editors nano (simple & easy, fairly limited) xemacs vim (more powerful) scite or gedit (GUI only) quick demos... 19
More than one program running at once: one is "foreground" others are "background" Many programs can be suspended with control-z Or type a command followed by "&" to run it in the background. To see a list of current jobs: jobs To switch to a background job: % + number of job or unique prefix of job name To terminate a job: kill %vim or: kill %3 Jobs 20
Three Features That Save Typing 1. filename completion 2. history 3. command-line editing All three: many options, fancy features. Basics are simple. 21
1. Filename Completion When typing arguments to a command, tab can help. Unique prefix of file name + tab: Linux completes file name. Non-unique prefix + tab: Linux adds all the characters it can guess Non-unique prefix of file name + two tabs: Linux shows you all possible choices 22
2. Bash History Facilities To repeat the last command you typed:!! To see list of last commands typed: history To see last 10 commands: history 10 To repeat command 137:!137 To repeat the most recent command that started with "ls":!ls Use up and down arrow keys to browse among previous commands. 23
3. Command-Line Editing To edit command you're currently typing: arrow keys to move within line (or ^f and ^b) type normal characters to insert before cursor delete or ^d to delete character at cursor backspace to delete character before cursor Use with up and down arrows to edit previous lines. 24
File Permissions Three kinds of permissions for a file: read write execute (if it's a program) Every file has an owner and a group. We won't be doing much with groups in 220. Permissions are set for three categories of users: the file's owner users in the file's group everyone else 25
Viewing File Permissions Example: malamb@linux4:~/demo$ ls -l game -rwxr-xr-x 1 malamb cisfac 630 2007-01-01 14:20 game all other users can read and execute other group members can read and execute owner can read, write and execute 26
Directory Permissions A directory is a special file containing pointers to other files read = can list contents of directory write = can add & remove files from directory execute = can access files in directory Example: malamb@linux4:~$ ls -ld $HOME drwx------ 34 malamb cisfac 1536 2007-01-02 11:54 /cas/staff/malamb/ No one except me can see or change my home directory. Another Example: malamb@linux4:~$ ls -ld /cas/course/cisc220 drwxr-xr-x 3 malamb cisfac 512 2007-01-01 16:18 /cas/course/cisc220/ Everyone can see & use files in this directory, but only I can add & delete files. 27
Changing File/Directory Permissions chmod command (change mode) Category of user denoted by single character: u = user who owns file g = users in file's group o = all other users Type of permission denoted by single character: r = read, w = write, x = execute To allow all users (outside the file's group) to read myfile: chmod o+r myfile To remove permission for group members to change myfile: chmod g-w myfile To set permission for group members to r&w, not x: chmod g=rw myfile 28
Protecting Your Linux Files Your assignment solutions should be read protected! One easy way: put in read-protected directory. To make sure your home directory can't be read by others: chmod go-rwx $HOME Another way: check the default protections of new files: umask S To change your default: umask go-rwx 29