
Carnegie Mellon Programming in C & Living in Unix 15-213: Introduc7on to Computer Systems Recitaon 6: Monday, Sept. 30, 2013 Marjorie Carlson Secon A 1 Carnegie Mellon Weekly Update Buffer Lab is due Tuesday (tomorrow), 11:59PM . This is another lab you don’t want to waste your late days on. Cache Lab is out Tuesday (tomorrow), 11:59 PM . Due Thursday October 10th . Cache Lab has two parts. 1. Write a 200- to 300-line C program that simulates the behavior of cache. (Let the coding in C begin!) 2. Op7mize a small matrix transpose func7on in order to minimize the number of cache misses. Next recitaon will address part 2. For part 1, pay close aen7on in class this week. 2 Carnegie Mellon Agenda Living in Unix Programming in C . Beginner . Refresher . The Command Line . Compiling . Basic Commands . Hun7ng Memory Bugs . Pipes & Redirects . GDB . Intermediate . Valgrind . More Commands . The Environment . Shell Scripng 3 Carnegie Mellon “In the Beginning was the Command Line…” Command Line Interface . “Provides a means of communicaon between a user and a computer that is based solely on textual input and output.” Shell . A shell is a program that reads and executes the commands entered on the command line. It provides “an interface between the user and the internal parts of the operang system (at the very core of which is the kernel).” . The original UNIX shell is sh (the Bourne shell). Many other versions exist: bash, csh, etc. The Linux Information Project: Shell The Linux Information Project: Command Line 4 Carnegie Mellon Unix – Beginner: Basic Commands Moving Around Manipulating Files ls List directory contents mv Move (rename) files cd Change working directory cp Copy files (and directories with “-r”) pwd Display present working directory rm Remove files (or directories with “-r”) ln Make links between files/directories cat Concatenate and print files mkdir Make directories chmod Change file permission bits Working Remotely Looking Up Commands ssh Secure remote login program man Interface to online reference manuals sftp Secure remote file transfer program which Shows the full path of shell commands scp Secure remote file copy program locate Find files by name Managing Processes Other Important Commands ps Report current processes status echo Display a line of text kill Terminate a process exit Cause the shell to exit jobs Report current shell’s job status history Display the command history list fg (bg) Run jobs in foreground (background) who Show who is logged on the system 5 Carnegie Mellon Quick Aside: Using the rm Command rm ./filename – deletes file “filename” in current dir. rm ./*ame – deletes all files in the current directory that end in “ame.” rm –r ./* – deletes all files and directories inside the current directory. rm -r ./directory – deletes all files inside the given directory and the directory itself. sudo rm –rvf /* – deletes the en7re hard drive. DO NOT DO THIS!!! . sudo runs the command as root, allowing you to delete anything. -v (verbose) flag will list all the files being deleted. -f (force) flag prevents it from asking for confirmaon before dele7ng important files. 6 Carnegie Mellon Quick Aside: Man Page SecHons man foo prints the manual page for the foo, where foo is any user command, system call, C library func7on, etc. However, some programs, u7li7es, and func7ons have the same name, so you have to specify which secHon you want (e.g., man 3 printf gets you the man page for the C library func7on prin, not the Unix command prin). How do you know which sec7on you want? . whatis foo . man –f foo . man –k foo lists all man pages men7oning “foo.” 7 Carnegie Mellon Unix – Beginner: Pipes & Redirects A pipe (|) between two commands sends the first command’s output to the second command as its input. A redirect (< or >) does basically the same thing, but with a file rather than a process as the input or output. Remember this slide? . Opon 1: Pipes $ cat exploitfile | ./hex2raw | ./bufbomb -t andrewID . Op7on 2: Redirects $ ./hex2raw < exploitfile > exploit-rawfile $ ./bufbomb -t andrewID < exploit-rawfile 8 Carnegie Mellon Agenda Living in Unix Programming in C . Beginner . Refresher . The Command Line . Compiling . Basic Commands . Hun7ng Memory Bugs . Pipes & Redirects . GDB . Intermediate . Valgrind . More Commands . The Environment . Shell Scripng 9 Carnegie Mellon Unix – Intermediate: More Commands Transforming Text Useful with Other Commands cut Remove sections from each line of screen Screen manager with terminal files (or redirected text) emulation sed Stream editor for filtering and sudo Execute a command as another user transforming text (typically root) tr Translate or delete characters sleep Delay for a specified amount of time Archiving Looking Up Commands zip Package and compress files alias Define or display aliases tar Tar file creation, extraction and export Exposes variables to the shell manipulation (setenv)* environment and its following commands Manipulating File Attributes Searching Files and File Content touch Change file timestamps (creates find Search for files in a directory hierarchy empty file, if nonexistent) umask Set file mode creation mask grep Print lines matching a pattern * – Bash uses export. Csh uses setenv. 10 Carnegie Mellon Unix – Intermediate: Environment Variables Your shell’s environment is a set of variables, including informaon such as your username, your home directory, even your language. env lists all your environment variables. echo $VARIABLENAME prints a specific one. $PATH is a colon-delimited list of directories to search for executables. Variables can be set in config files like .login or .bashrc, or by scripts, in which case the script must use export (bash) or setenv (csh) to export changes to the scope of the shell. 11 Carnegie Mellon Unix – Intermediate: Shell ScripHng Shell scrip7ng is a powerful tool that lets you integrate command line tools quickly and easily to solve complex problems. Shell scripts are wriZen in a very simple, interpreted language. The simplest shells scripts are simply a list of commands to execute. Shell scrip7ng syntax is not at all user-friendly, but shell scrip7ng remains popular for its real power. Teaching shell scrip7ng is depth is beyond the scope of this course – for more informaon, check out Kesden’s old 15-123 lectures 3, 4 and 5. 12 Carnegie Mellon Unix – Intermediate: Shell ScripHng To write a shell script: . Open your preferred editor. Type #!/bin/sh. When you run the script, this will tell the OS that what follows is a shell script. Write the relevant commands. Save the file. (You may want its extension to be .sh.) . Right now it’s just a text file – not an executable. You need to chmod +x foo.sh it to make it executable. Now run it as ./foo.sh! 13 Carnegie Mellon Unix – Intermediate: Shell ScripHng hello.sh hello.sh with variables #!/bin/sh #!/bin/sh # Prints “Hello, world.” to STDOUT str=“Hello, world.” echo “Hello, world.” echo $str # Also prints “Hello, world.” Anything aer a ‘#’ is a comment. Variables Seng a variable takes the form ‘varName=VALUE’. There CANNOT be any spaces to the leo and right of the “=“. Evaluang a variable takes the form “$varName”. Shell scrip7ng is syntac7cally subtle. (Unquoted strings, “quoted strings”, ‘single-quoted strings’ and `back quotes` all mean different things!) 14 Carnegie Mellon Agenda Living in Unix Programming in C . Beginner . Refresher . The Command Line . Compiling . Basic Commands . Hun7ng Memory Bugs . Pipes & Redirects . GDB . Intermediate . Valgrind . More Commands . The Environment . Shell Scripng 15 Carnegie Mellon C – Refresher 16 Carnegie Mellon C – Refresher: Things to Remember If you allocate it, free it. int *array= malloc(5 * sizeof(int)); … free(array); If you use Standard C Library func7ons that involve pointers, make sure you know if you need to free it. (This will be relevant in proxylab.) 17 Carnegie Mellon C – Refresher: Things to Remember There is no String type. Strings are just NULL-terminated char arrays. Seng pointers to NULL aer freeing them is a good habit, so is checking if they are equal to NULL. Global variables are evil, but if you must use make sure you use extern where appropriate. Define func7ons with prototypes, preferably in a header file, for simplicity and clarity. 18 Carnegie Mellon C – Refresher: Command Line Arguments Many C func7ons take command-line arguments: $ gdb bomb $ ./makecookie marjorie $ ls –l /etc If you want to pass arguments on the command line to your C func7ons, your main func7on’s parameters must be int main(int argc, char **argv) # of arguments the arguments, as an (always ≥ 1) array of char*s (strings) 19 Carnegie Mellon C – Refresher: Command Line Arguments int main(int argc, char **argv) . argv[0] is always the name of the program. The rest of the array are the arguments, parsed on space. $ ls –l /etc program name arguments argv[0] argv[1], argv[2] argc = ? 20 Carnegie Mellon C – Refresher: Headers & Libraries bits.h Header files make your int bitNor(int, int); int test_bitNor(int, int); func7ons available to other int isNotEqual(int, int); int test_isNotEqual(int, int); source files. int anyOddBit(); int test_anyOddBit(); Implementaon in .c, and int rotateLeft(int, int); int test_rotateLeft(int, int); declaraons in .h. int bitParity(int); int test_bitParity(int); . forward declaraons int tmin(); int test_tmin(); . struct prototypes int fitsBits(int, int); int test_fitsBits(int, int); . #defines int rempwr2(int, int); int test_rempwr2(int, int); int addOK(int, int); It should be wrapped in int test_addOK(int, int); #ifndef LINKEDLIST_H int isNonZero(int); int test_isNonZero(int); #define LINKEDLIST_H int ilog2(int); int test_ilog2(int); ... unsigned float_half(unsigned); unsigned test_float_half(unsigned); #endif unsigned float_twice(unsigned); unsigned test_float_twice(unsignned); 21 Carnegie Mellon C – Refresher: Headers & Libraries #include <*.h> - Used for including header files found in the C include path: standard C libraries.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages34 Page
-
File Size-