Linux/Cygwin Practice 2010 Computer Architecture
Linux Login Use ssh client applications to connect (Port : 22) SSH Clients zterm ( http://www.brainz.co.kr/products/products4_2.php ) Putty ( http://kldp.net/frs/download.php/3411/hangulputty-0.58.h2.exe ) Some departments provide Linux machines for undergrads Change your default password using command passwd 2/23
Cygwin On Windows machine Cygwin provides a similar environment to Linux Windows DLL for Linux API emulation Tools with Linux look & feel Download Cygwin setup file from the following URL and install it http://www.cygwin.com 3/23
Shell Shell? Interface program between users and OS Typing commands, user can launch programs Prompt and cursor Ready to take commands from users 4/23
File and Directory Directory Separate space (or folder) for related files Usually keep different files in different directories / indicates the hierarchy of directories and files Ex) /home/beowulf/readme.txt Examples: /etc and /bin represent two different directories. : current directory / : top level directory (root directory) Current directory.. : immediately above directory Users work within a specific directory for file editing and executing apps. pwd command shows the full path of current directory After login (or launching cygwin), the initial directory is called home directory Ex) /home/userid 5/23
Usage Rights User On Linux, userid distinguish individual users Example: jschoi, jdseo, etc Group Multiple users belong to a group Example: users = (jschoi, jdseo, ) ca_st = (st001, st002, ) Ownership Each file has a designated user and group Each file has different usage right for 3 categories (owner, group, other) For each category, Read, write, execute rights can be differently specified For directories, execute right means right to change directory 6/23
File List ls command List up files and sub-directories in current directory. ls -al : can have options such as -al (For details, ls --help) [directory(1)][user(3)][group(3)][other(3)] r=read, w=write, x=execute 7/23
Directory Command cd <directory_name> : changes current directory Ex) cd.., cd work, cd /usr/local, If no directory_name is specified, changes to home directory pwd : shows the full path of the current directory mkdir <directory_name> : creates a new directory rmdir <directory_name> : delete the specified directory temp 8/23
File Commands cp <source-file-name> <target-file-name> Copy the source file to the target file mv <source-file-name> <target-file-name> Similar to cp, but the source file is deleted (move) rm <file-name> - Delete the file cat <file-name> - show the content of the file 9/23
Misc. Commands (1) chmod Change the access right of the file Example chmod 764 <file-name> 7 = user access right : 4(read)+2(write)+1(execute) 6 = group access right : 4(read)+2(write) 4 = other access right : 4(read) Each number can be 0~7 depending on access right you want to specify man [section] <command> - show manual pages for command Can look up the manual pages of C system calls and library functions E.g. write(2) : write command in section 2 For most commands, <command> --help shows the usage pages Try apropos 10/23
Misc. Commands (2) Function-name(section) Brief description Necessary headers, function type Detailed description Return value Error code 11/23
Misc. Commands(3) Error code (cont d) Other notes to mention Related functions 12/23
Using vi vi Most prevail file editor in Unix/Linux environment Command mode Search and edit command can be used Vi starts in command mode Input mode Type characters into the file Switch between two modes Command mode input mode : press one of the following - i, l, a, A, o, O, R Input mode command mode : press ESC 13/23
Using vi Command mode input mode Press one of the following i, l, a, A, o, O, R Key pressed i I a A o O R Action Insert at the cursor Insert at the beginning of the current line Append at the next position of the cursor Append at the end of the current line Open a new line at the end of current line Open a new line in front of the current line Replace from the current cursor 14/23
Using vi Move cursor in command mode Key Cursor Position Key Cursor Position k One line above b One word left j One line below Ctrl-D Half screen down l One character right Ctrl-F Full screen down h One character left Ctrl-U Half screen up ^ Beginning of the line Ctrl-B Full screen up $ End of the line :n n-th line w One word right 15/23
Using vi Delete text Key x dw dd D :nd Action Delete a character at the cursor Delete a word at the cursor Delete a line at the cursor Delete characters from the cursor to the end of line Delete n lines from the cursor 16/23
Using vi File save, read, quit Key Action :w <filename> Save current file to <filename> :w Save current file :<range>w <filename> Save the lines in the <range> to <filename> :r <filename> Read <filename> at the current cursor :q Quit if the file is not modified :q! Force to quit without saving the modified text :wq Save the modified text and quit 17/23
Using vi Other commands Copy text Key p Action Put the text in the buffer at the next character of the cursor or at the next line of the cursor Search text P Key Put the text in the buffer in front of the cursor or at the above line of the cursor Action /[pattern]?[pattern] Search for [pattern] in the forward direction Search for [pattern] in the backward direction / Repeatedly search for the recently searched pattern in the forward direction? Repeatedly search for the recently searched pattern in the backward direction n N Repeatedly search for the recently searched pattern in the current direction Repeatedly search for the recently searched pattern in the opposite direction 18/23
Using vi Learn frequently used commands first! Tutorial for vi Korean version http://www.joinc.co.kr/modules/moniwiki/wiki.php/site/dev elopment/env/usedvim English version http://www.eng.hawaii.edu/tutor/vi.html 19/23
Structure of C Program #include headers, global variables, function declarations main() entry function Local variables Program body Return 0 end of main() Another function divide() 20/23
Comment in C Programs Comment Describe the program Ignored when compiling Form of comment in C /* comment-text */ // comment-text all characters to the end of line are comment 21/23
Compile and Execute gcc <options> <C-program-file> Without options, the default executable file is a.out Typing./a.out at the prompt will execute the program gcc -o <executable-filename> <C-program-file> will produce an executable file with the specified name E.g.) gcc o div div.c [cygwin-pc]$./div 22/23
Practice 1) Create a directory named lab in your home directory 2) Using vi, create a file called div.c in your lab directory 3) Compile the program to produce the executable file named div 4) Move your div program to your home directory and run it there 23/23
Computer Architecture Lab. 24/23