1 / 33

Programming in C & Living in Unix

Programming in C & Living in Unix. 15-213: Introduction to Computer Systems Recitation 6: Monday, Sept. 30, 2013 Marjorie Carlson Section A. Weekly Update. Buffer Lab is due Tuesday (tomorrow), 11:59PM This is another lab you don’t want to waste your late days on.

kostya
Download Presentation

Programming in C & Living in Unix

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming in C & Living in Unix 15-213: Introduction to Computer SystemsRecitation 6: Monday, Sept. 30, 2013 Marjorie CarlsonSection A

  2. 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. • Write a 200- to 300-line C program that simulates the behavior of cache. (Let the coding in C begin!) • Optimize a small matrix transpose function in order to minimize the number of cache misses. • Next recitation will address part 2. For part 1, pay close attention in class this week. 

  3. Agenda • Living in Unix • Beginner • The Command Line • Basic Commands • Pipes & Redirects • Intermediate • More Commands • The Environment • Shell Scripting • Programming in C • Refresher • Compiling • Hunting Memory Bugs • GDB • Valgrind

  4. “In the Beginning was the Command Line…” • Command Line Interface • “Provides a means of communication 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 operating 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

  5. Unix – Beginner: Basic Commands

  6. 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. • sudorm –rvf /*– deletes the entire 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 confirmation before deleting important files.

  7. Quick Aside: Man Page Sections • man fooprints the manual page for the foo, where foo is any user command, system call, C library function, etc. • However, some programs, utilities, and functions have the same name, so you have to specify which section you want (e.g., man 3 printfgets you the man page for the C library function printf, not the Unix command printf). • How do you know which section you want? • whatisfoo • man –ffoo • man –kfoo lists all man pages mentioning “foo.”

  8. 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?  • Option 1: Pipes $ cat exploitfile | ./hex2raw | ./bufbomb -tandrewID • Option 2: Redirects $ ./hex2raw < exploitfile > exploit-rawfile $ ./bufbomb -t andrewID < exploit-rawfile

  9. Agenda • Living in Unix • Beginner • The Command Line • Basic Commands • Pipes & Redirects • Intermediate • More Commands • The Environment • Shell Scripting • Programming in C • Refresher • Compiling • Hunting Memory Bugs • GDB • Valgrind

  10. Unix – Intermediate: More Commands * – Bash uses export. Csh uses setenv.

  11. Unix – Intermediate: Environment Variables • Your shell’s environment is a set of variables, including information such as your username, your home directory, even your language. • envlists all your environment variables. • echo $VARIABLENAMEprints 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, orby scripts, in which case the script must use export(bash) or setenv(csh) to export changes to the scope of the shell.

  12. Unix – Intermediate: Shell Scripting • Shell scripting is a powerful tool that lets you integrate command line tools quickly and easily to solve complex problems. • Shell scripts are written in a very simple, interpreted language. The simplest shells scripts are simply a list of commands to execute. • Shell scripting syntax is not at all user-friendly, but shell scripting remains popular for its real power. • Teaching shell scripting is depth is beyond the scope of this course – for more information, check out Kesden’s old 15-123 lectures 3, 4 and 5.

  13. Unix – Intermediate: Shell Scripting • 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+xfoo.sh it to make it executable. • Now run it as ./foo.sh!

  14. Unix – Intermediate: Shell Scripting • Anything after a ‘#’ is a comment. • Variables • Setting a variable takes the form ‘varName=VALUE’. • ThereCANNOT be any spaces to the left and right of the “=“. • Evaluating a variable takes the form “$varName”. • Shell scripting is syntactically subtle. (Unquoted strings, “quoted strings”, ‘single-quoted strings’ and `back quotes` all mean different things!)

  15. Agenda • Living in Unix • Beginner • The Command Line • Basic Commands • Pipes & Redirects • Intermediate • More Commands • The Environment • Shell Scripting • Programming in C • Refresher • Compiling • Hunting Memory Bugs • GDB • Valgrind

  16. 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 functions that involve pointers, make sure you know if you need to free it. (This will be relevant in proxylab.)

  17. C – Refresher: Things to Remember • There is no String type. Strings are just NULL-terminated char arrays. • Setting pointers to NULL after 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 externwhere appropriate. • Define functions with prototypes, preferably in a header file, for simplicity and clarity.

  18. C – Refresher: Command Line Arguments • If you want to pass arguments on the command line to your C functions, your main function’s parameters must be intmain(intargc, char **argv) • argc is the number of arguments. (Always ≥ 1) • argv is an array of char*s(more or less an array of strings) corresponding to the command line input. • argv[0] is always the name of the program. • The rest of the array are the arguments, parsed on space.

  19. C – Refresher: Command Line Arguments • $ ls –l private • argc = 3 • argv = {“ls”, “-l”, “etc”}

  20. C – Refresher: Headers & Libraries • Header files make your functions available to other source files. • Implementation in .c, and declarations in .h. • forward declarations • struct prototypes • #defines • It should be wrapped in #ifndef LINKEDLIST_H #define LINKEDLIST_H ... #endif bits.h intbitNor(int, int); inttest_bitNor(int, int); intisNotEqual(int, int); inttest_isNotEqual(int, int); intanyOddBit(); inttest_anyOddBit(); introtateLeft(int, int); inttest_rotateLeft(int, int); intbitParity(int); inttest_bitParity(int); inttmin(); inttest_tmin(); intfitsBits(int, int); inttest_fitsBits(int, int); int rempwr2(int, int); int test_rempwr2(int, int); intaddOK(int, int); inttest_addOK(int, int); intisNonZero(int); inttest_isNonZero(int); int ilog2(int); int test_ilog2(int); unsigned float_half(unsigned); unsigned test_float_half(unsigned); unsigned float_twice(unsigned); unsigned test_float_twice(unsignned);

  21. C – Refresher: Headers & Libraries • #include <*.h> - Used for including header files found in the C include path: standard C libraries. • #include “*.h” – Used for including header files in the same directory. • There are command-line compiler flags to control where header files are searched for, but you shouldn’t need to worry about that.

  22. C – Refresher: “Reverse” Demo • reverse takes arguments from the command line and prints each of them backwards. • It calls reverse_strdup. • To compile: gcc –Wall –reverse.c –reverse_strdup.c –o reverse

  23. Agenda • Living in Unix • Beginner • The Command Line • Basic Commands • Pipes & Redirects • Intermediate • More Commands • The Environment • Shell Scripting • Programming in C • Refresher • Compiling • Hunting Memory Bugs • GDB • Valgrind

  24. C – Compiling: Command Line • When compiling C code, all dependencies must be specified. • Remember how we just compiled?gcc –Wall –reverse.c –reverse_strdup.c –o reverse • This will not compile: • gcc –Wall –reverse.c –o reverse

  25. C – Compiling: Command Line • gcc does not requires these flags, but they encourage people to write better C code.

  26. C – Compiling: Makefiles • Projects can get very complicated very fast, and it can take a long time to have GCC recompile the whole project for a small change. • Makefiles are designed to solve this problem; make recompiles only the parts of the project that have changed and links them to those that haven’t changed. • Makefiles commonly separate compilation (.c.o) and linking (.o’s executable).

  27. C – Compiling: Makefiles • Makefiles consist of one or more rules in the following form.

  28. C – Compiling: Makefiles • Comments are any line beginning with ‘#’ • The first line of each command must be a TAB. • If you want help identifying dependencies, you can try: • gcc –MM foo.c outputs foo’s dependencies to the console. • makedependadds dependencies to the Makefile for you, if you already have one. E.g., foo.cbar.cbaz.c .

  29. C – Compiling: Makefiles • Use macros – similar to shell variables – to avoid retyping the same stuff for every .o file. • For more information on Makefiles, check out Kesden’s old 15-123 lecture 16.

  30. Agenda • Living in Unix • Beginner • The Command Line • Basic Commands • Pipes & Redirects • Intermediate • More Commands • The Environment • Shell Scripting • Programming in C • Refresher • Compiling • Hunting Memory Bugs • GDB • Valgrind

  31. C – Hunting Memory Bugs: GDB • Useful for debugging the occasional easy segfault (among other things!). • You’re used to using disas and stepi/nexti to look at and step through Assembly. • If you compile with –g, you can use list and step/next to look at and step through the C, too. • This even works after you’ve segfaulted! • Other useful commands: • where (print function stack and lines) • up/down (traverse the function stack) • display/print variables (like you do now with registers).

  32. C – Hunting Memory Bugs: Valgrind • Great for finding memory problems in C programs. Has a ton of tools: memcheck, leakfind, cachegrind. • Valgrind’smemcheck tool can: • Track memory leaks & (definitely/possibly) lost blocks • Track origin for uninitialized values • Tell you what line of code seems to have been the problem • Syntax (including the verbose flag): valgrind --tool=memcheck -v ./myprogramargs valgrind –-tool=leak-check=full -v ./myprogramargs

  33. Sources and Useful Links • The Linux Information Project: Command Line Definition • Introduction to Linux: A Hands-On Guide (Garrels) • You should be comfortable with chapters 2, 3, 4 and 5. • The On-line Manual Database • Kesden’s 15-213: Effective Programming in C and Unix • Lectures 3, 4 and 5 cover the basics of Shell Scripting. • Lecture 16 covers Makefiles and lecture 15 covers Valgrind.

More Related