Lecture 3 --------- Announce --------- - TA Office Hours (IN CSIL), Tue 2-2:50 - Poll class to see who doesn't have CSIL accounts - if you don't, see me after class Review ------- - OS defn - everything is process - process lifetime - process tree - filesystem tree - simple unix commands (ls, cat, date) - file system commands (cd, mkdir, rmdir) - pwd, cd by itself Today ------- - more useful UNIX commands/ideas - UNIX development process - example C program - turnin system - by end of class, able to write and run C programs on UNIX machines End of CLASS ------------ - if most have accounts, assign homework for monday - assign chapter 2 of K&R reading More UNIX ---------- - job control - processes can be in the forground or background, stopped or running - cmd & puts a process in the background, running - jobs shows list of jobs and whether they're stopped or running - cmd [ENTER] CTRL-Z puts a job in the background, stopped - fg %jobnum brings a job to the foreground, running - bg %jobnum takes a background job from stopped to running - killing a process - if job is in foreground, CTRL-C - if job is in background, kill -9 %jobnum - if job is under control of another shell, use ps to get PID - kill -9 PID User Environment ----------------- - interacting with system through a shell process - when a cmd is typed in, executable file must be loaded into memory - shell is waiting for you to give it name of file and arguments - can type in full path to executable - shell tries to help, predefined directories that contain binaries - to help, environment variables - $PATH - string of directories, separated by ':' - look at variables with printenv or echo - see all environment variables with 'env' - create new variables - diff shells have diff ways of doing this - bash: export FOO="/bin/ls" - tcsh: setenv FOO "/bin/ls" - expand variables with $ - $FOO - CWD is not usually in your PATH! ./ - wildcards - sometimes want to operate on subset of files - *, ? ** picture - ex: blah.mp3 fog.c foog.c foooog.c bar.c - all .c files - all files starting with f and ending with g - all .c files starting with b Remote Login ------------- - remotely log in to csil.cs.ucsb.edu or any lab machine - from unix machine use 'ssh' - from windows machine use 'putty' - from macOSX machine find the terminal app and use 'ssh' ** start picture - all of these are creating a network connection to the machine csil.cs.ucsb.edu which runs a shell - output from the shell is shuttled to your local window - input to local window is shuttled to your shell - windows/mac cannot run x windows programs by default - unix machines, if you use ssh -X csil.cs.ucsb.edu, can shuttle xwindows data in addition to shell data - easiest to physically use CSIL machines while learning Getting Help ------------ - first thing to try is 'man' system - almost all unix commands have a manpage - many C functions have a manpage - try cmd --help - consult the internet Text Editing -------------- - Choose to use emacs, vi, pico, whatever. - comical ongoing debate over vi vs. emacs, vi is lightweight, emacs is full featured, both are both - book covers emacs Other useful commands --------------------- - cp, mv, rm - script - grep - sort - uniq ? - top - pine ? - find ? - touch Programming in the UNIX environment ------------------------------------ development cycle - 1.) use editor to create/input/edit program code into the file (vi foo.c) 2.) compile the code into a binary (gcc foo.c -o foo) 3.) execute binary file (./foo) 4.) debug 5.) goto 1 1.) create a program - make a project directory - use your fav editor to create a .c file - save the file to disk 2.) compiling. - UNIX uses 'gcc' to compile code into executable format - gcc goes through following stages 1.) preprocessing - outputs the actual C code that is to be compiled 2.) compiling - input is C code - checks for proper structure, generates warnings/errors - does optimization - outputs assembly language version of code 3.) assembling - takes assembly code, - converts to byte code that can be run on the processor 4.) linking - takes object code potentially from multiple source - 'links' them together to create an executable file - gcc is really only a compiler (step 2) - default takes care of everything for you - runs helper cmds 'cpp', 'gas/as', 'ld' - gcc foo.c - ./a.out 3.) Executing - once you have compiled the code into a binary, run with ./cmd 4.) debug - use gdb, which allows developer to run a program but stop at specified points and examine variables, functions etc and then continue - use gdb, which we'll get to later 5.) goto 1, edit code and start over C Example ------------------------------ - Do a simple C example -------------------------------------- #include main() { printf("Hello World\n"); } --------------------------------------- program disection ------------------ - #include - C language itself does not define any functions - standard libraries have been written so that programmers donot have to start from scratch - function definitions allow use of functions are in 'header' files, .h - #include is a preprocessor step which essentially finds the given file and puts it in place of #include stdio.h - main() { - defines a function called 'main' which takes no arguments '()' - statments are contained within curly braces {} - by default, OS will first call the main function of a program - printf("Hello World\n"); - calling a function, defined in stdio.h, called printf. - passing the printf one parameter within the () - parameter is a 'string' delimited by "s - Hello World\n contains 12 characters, 10 letters, a space, and a 'newline' which tells the cursor to go to the beginning of the next line - semlicolon must be placed after all program statments - } - ends definition of the main function Turnin Program --------------- - for all homeworks and projects, we'll be using turnin program - each homework will have a tag like 'hw1', projects will be 'proj1' - create a project directory 'myhomework1' or hw1 - put ALL your work/code in this directory - in addtion to code - make a README file that contains any special instructions that grader should know - make a script file that shows the compiling and execution of your program - when all set, go up a directory and use 'turnin' - turnin @cs60 cd /home/nurmi mkdir nurmihw1 cd nurmihw1 emacs hw1.c script hw1.script gcc hw1.c -o hw1 ./hw1 Hello World exit rm hw1 cd .. turnin hw1@cs60 nurmihw1 Another C example ------------------ #include main() { int i; int sum; sum = 0; for (i=0; i<10; i++) { printf("Current sum = %d\n", sum); sum = sum + i; } printf("Final sum = %d\n", sum); } program disection ------------------ #include and main() { as before - int i; int sum; - variable declarations. - need to declare what variables you would like to use in upcoming program - need to declare their 'type' which specifies what kind of data variables can hold - sum = 0; - set the contents of the variable 'sum' to the integer value 0 - for (i=0; i<10; i = i + 1) { - loop structure has three parts - i = 0 - before starting the loop, set the contents of variable i to 0 - i < 10 - loop condition, keep looping while the content value of i < 10 - i++ - at the end of each loop iteration, increment the value of i by 1 - {} contains code to be executed at each iteration - printf("Current sum = %d\n", sum); - print the string and contents of the sum variable - sum = sum + i; - set the value of variable sum to whatever sum currently plus the current value of i - printf("Final sum = %d\n", sum); - after the loop has completed, print out the final contents of the sum variable