Lecture 01: Linux and C Programming Language CSE 564 Computer Architecture Fall 2016 Department of Computer Science and Engineering Yonghong Yan [email protected] www.secs.oakland.edu/~yan 1 Contents • Remote Login using SSH • Linux • C Programming • Compiling and Linking • Assignment 1 2 Computaon Server In the cold and dark server room! Run Linux/Unix Operang System 3 Client/Server and SSH (Secure Shell) 4 Machines • hp://cto.secs.oakland.edu/docs/pdf/linuxServers.pdf • To connect to any available server – login.secs.oakland.edu • Or you can connect directly with – ringo.secs.oakland.edu – harrison.secs.oakland.edu – paul.secs.oakland.edu – gpu.secs.oakland.edu • SSH or puIy – ssh <one of above machine name> -l<ned> • Copy files or using H drive – scp or winscp – hIp://www.secs.oakland.edu/docs/pdf/accessNetworkDrive.pdf • Need VPN from home – hp://secs.oakland.edu/docs/pdf/vpn.pdf 5 Puy SSH connecon login.secs.oakland.edu 6 Linux Basic Commands It is all about dealing with files and folders Linux folder: /home/yan/… • rm (remove a filer/folder) – $ rm foo • ls (list files in the current folder) – $ rm -rf foo – $ ls -l – $ ls -a – $ rm -i foo – $ ls -la – $ rm -- -foo – $ ls -l --sort=me • cat (print the file contents to – $ ls -l --sort=size –r terminal) • cd (change directory to) – $ cat /etc/motd – $ cd /usr/bin – $ cat /proc/cpuinfo • pwd (show current folder name) – $ pwd • cp (create a copy of a file/folder) • ~ (home folder) – $ cp foo bar – $ cd ~ – $ cp -a foo bar • ~user (home folder of a user) • mv (move a file/folder to another – $ cd ~weesan locaon. Used also for renaming) – $ mv foo bar • What will “cd ~/weesan” do? • mkdir (create a folder) – $ mkdir foo Basic Commands (cont) • df (Disk usage) Search a command or a file – $ df -h / • which – $ du -sxh ~/ – $ which ls • man (manual) • whereis – $ man ls – $ whereis ls – $ man 2 mkdir • locate – $ man man – $ locate stdio.h – $ man -k mkdir – $ locate iostream • Manpage secons • find – 1 User-level cmds and apps – $ find / | grep stdio.h • /bin/mkdir – $ find /usr/include | grep stdio.h – 2 System calls • int mkdir(const char *, …); Smarty – 3 Library calls 1. [Tab] key: auto-complete the command • int prin(const char *, …); sequence 2. é key: to find previous command 3. [Ctl]+r key: to search previous command EdiAng a File: Vi • 2 modes • Delete – Input mode – dd (delete a line) – d10d (delete 10 lines) • ESC to back to cmd mode – d$ (delete Rll end of line) – Command mode – dG (delete Rll end of file) • Cursor movement – x (current char.) – h (lej), j (down), k (up), l (right) • Paste – ^f (page down) – p (paste aer) – ^b (page up) – ^ (first char.) – P (paste before) – $ (last char.) • Undo – G (boIom page) – u – :1 (goto first line) • Search • Swtch to input mode – / – a (append) • Save/Quit – i (insert) – :w (write) – o (insert line aer – O (insert line before) – :q (quit) – :wq (write and quit) – :q! (give up changes) C Hello World • vi hello.c • Switch to ediRng mode: i or a • Switching to control mode: ESC • Save a file: in control mode, :w • To quit, in control mode, :q • To quit without saving, :q! • Copy/paste a line: yy and then p, both from the current cursor – 5 line: 5yy and then p • To delete a whole line, in control mode, : dd #include <stdio.h> • vi hello.c /* The simplest C Program */ • ls hello.c • gcc hello.c –o hello int main(int argc, char **argv) { • ls printf(“Hello World\n”); • ./hello return 0; } 10 C Syntax and Hello World #include inserts another file. “.h” files are called “header” files. They contain declarations/definitions needed to interface to libraries and code in other “.c” files. What do the < > mean? A comment, ignored by the compiler #include <stdio.h> The main() function is always /* The simplest C Program */ where your program starts int main(int argc, char **argv) running. { Blocks of code (“lexical printf(“Hello World\n”); scopes”) are marked by { … } return 0; } Return ‘0’ from this function 11 Compilaon Process in C • Compilaon process: gcc hello.c –o hello – ConstrucRng an executable image for an applicaon – FOUR stages – Command: gcc <opRons> <source_file.c> • Compiler Tool – gcc (GNU Compiler) • man gcc (on Linux m/c) – icc (Intel C compiler) 4 Stages of Compilaon Process Preprocessing gcc –E hello.c –o hello.i hello.c à hello.i Compilaon (aaer preprocessing) gcc –S hello.i –o hello.s Assembling (aaer compilaAon) gcc –c hello.s –o hello.o Linking object files gcc hello.o –o hello Output à Executable (a.out) Run à ./hello (Loader) 4 Stages of Compilaon Process 1. Preprocessing (Those with # …) – Expansion of Header files (#include … ) – SubsRtute macros and inline funcRons (#define …) 2. Compilaon – Generates assembly language – Verificaon of funcRons usage using prototypes – Header files: Prototypes declaraon 3. Assembling – Generates re-locatable object file (contains m/c instrucRons) – nm app.o 0000000000000000 T main U puts – nm or objdump tool used to view object files 4 Stages of Compilaon Process (contd..) 4. Linking – Generates executable file (nm tool used to view exe file) – Binds appropriate libraries • Stac Linking • Dynamic Linking (default) • Loading and ExecuRon (of an executable file) – Evaluate size of code and data segment – Allocates address space in the user mode and transfers them into memory – Load dependent libraries needed by program and links them – Invokes Process Manager à Program registraon Compiling a C Program • gcc <op3ons> program_name.c • Opons: Four stages into one ----------- -Wall: Shows all warnings -o output_file_name: By default a.out executable file is created when we compile our program with gcc. Instead, we can specify the output file name using "-o" opon. -g: Include debugging informaon in the binary. • man gcc Linking MulAple files to make executable file • Two programs, prog1.c and prog2.c for one single task – To make single executable file using following instrucRons First, compile these two files with opRon "-c" gcc -c prog1.c gcc -c prog2.c -c: Tells gcc to compile and assemble the code, but not link. We get two files as output, prog1.o and prog2.o Then, we can link these object files into single executable file using below instrucRon. gcc -o prog prog1.o prog2.o Now, the output is prog executable file. We can run our program using ./prog Linking with other libraries • Normally, compiler will read/link libraries from /usr/lib directory to our program during compilaon process. – Library are precompiled object files • To link our programs with libraries like pthreads and realRme libraries (rt library). – gcc <opRons> program_name.c -lpthread -lrt -lpthread: Link with pthread library à libpthread.so file -lrt: Link with rt library à librt.so file Opon here is "-l<library>" Another opRon "-L<dir>" used to tell gcc compiler search for library file in given <dir> directory. CompilaAon, Linking, ExecuAon of C/C++ Programs source object file 1 file 1 source object linking file 2 file 2 load compilation (relocation + file library linking) object file 1 source object file N file N library object file M usually performed by a compiler, usually in one uninterrupted sequence hp://www.tenouk.com/ModuleW.html sum.c • cp ~yan/sum.c ~ (copy sum.c file from my home folder to your home folder) • gcc -save-temps sum.c –o sum • ./sum 102400 • vi sum.c • vi sum.s • View them from H drive • Other system commands: – cat /proc/cpuinfo to show the CPU and #cores – top command to show system usage and memory 20 More on C Programming 21 Lexical Scoping void p(char x) Every Variable is Defined within some scope. { A Variable cannot be referenced by name /* p,x */ char y; (a.k.a. Symbol) from outside of that scope. /* p,x,y */ char z; /* p,x,y,z */ } Lexical scopes are defined with curly braces { }. /* p */ char z; /* p,z */ The scope of Function Arguments is the void q(char a) complete body of that function. { char b; /* p,z,q,a,b */ The scope of Variables defined inside a { char b? function starts at the definition and ends at char c; /* p,z,q,a,b,c */ the closing brace of the containing block } legal? char d; The scope of Variables defined outside a /* p,z,q,a,b,d (not c) */ } function starts at the definition and ends at the end of the file. Called “Global” Vars. /* p,z,q */ 22 Comparison and MathemaAcal Operators == equal to < less than <= less than or equal Beware division: > greater than • 17/5=3, 17%5=2 >= greater than or equal != not equal • 5 / 10 = 0 whereas 5 / 10.0 = 0.5 && logical and • Division by 0 will cause a FPE(Float- || logical or ! logical not point exception) + plus & bitwise and - minus | bitwise or Don’t confuse & and &&.. * mult ^ bitwise xor 1 & 2 = 0 whereas 1 && 2 = <true> / divide ~ bitwise not % modulo << shift left >> shift right The rules of precedence are clearly defined but often difficult to remember or non-intuitive. When in doubt, add parentheses to make it explicit. 23 Assignment Operators x = y assign y to x x += y assign (x+y) to x x++ post-increment x x -= y assign (x-y) to x ++x pre-increment x x *= y assign (x*y) to x x-- post-decrement x x /= y assign (x/y) to x --x pre-decrement x x %= y assign (x%y) to x Note the difference between ++x and x++ (high vs low priority (precedence)): int x=5; int x=5; int y; int y; y = ++x; y = x++; /* x == 6, y == 6 */ /* x == 6, y == 5 */ Don’t confuse “=“ and “==“! int x=5; int x=5; if (x==6) /* false */ if (x=6) /* always true */ { { /* ..
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages84 Page
-
File Size-