1 / 25

4. Reading code in MP

제 13 강 : Reading Code in Modular Process(MP). 4. Reading code in MP. Modular Programming. src. Solves Faster compile Many people can edit New Problems Inter-dependency between files (eg define - use) Which information - which file? Change a file – what next?

tabib
Download Presentation

4. Reading code in MP

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. 제13강 : Reading Code in Modular Process(MP) 4. Reading code in MP

  2. Modular Programming ... src • Solves • Faster compile • Many people can edit • New Problems • Inter-dependency between files (eg define - use) • Which information - which file? • Change a file – what next? • N people update a.c simultaneously? • Debugging?

  3. src . . . . . include net vm sys sys.h inode.h user.h driver.c stream.c hd.c vm.h file.h error.h intrp.c signal.c strategy.c cdev.h bdev.h type.h read.c write.c sleep.c init.h flt.h mount.h nfs.c super.c win.c obj.h text.h abs.h ftp.c telnet.c fill.c . . . . . . . .

  4. Reading Codes . . . read.c src • #include "user.h" • #include "sys.h" • #include "type.h" • int sys_read (pst) • struct buf *pst; • { • char ps[MAX]; • int index; • while (i ... ) • { ... } sys vm net . . . . . sys.h inode.h user.h driver.c stream.c hd.c vm.h file.h error.h intrp.c signal.c strategy.c cdev.h bdev.h type.h read.c write.c sleep.c init.h flt.h mount hnfs.c super.c win.c obj.h text.h abs.h ftp.c telnet.c fill.c . . . . . . . . ex cd linux-practice/src cd lions/usr/sys ls cd ken vi fio.c

  5. Reading Codes . . . read.c • #include "user.h" • #include "sys.h" • #include "type.h" • int sys_read (pst) • struct buf *pst; • { • char ps[MAX]; • int index; • while ((index < MAX ) && (read (temp) != LEVEL) ) { • . . . . • }

  6. Reading Codes . . . read.c • #include "user.h" • #include "sys.h" • #include "type.h" • int sys_read (pst) --------------- who calls this? • struct buf *pst; ------- what is “buf”? • { • char ps[MAX]; • int index; • while ((index < MAX ) && (read (temp) != LEVEL) ) { • . . . . • ------------ where is read()? • what does it perform? • }

  7. Reading Codes src . . . . . include sys vm net Where do I find “read()" ? sys.h inode.h user.h driver.c stream.c hd.c vm.h file.h error.h intrp.c signal.c strategy.c cdev.h bdev.h type.h read.c write.c sleep.c init.h flt.h mount.h nfs.c super.c win.c obj.h text.h abs.h ftp.c telnet.c fill.c . . . . . . . . Manual or automatic search?

  8. Where is getc() function? (1/5)grep • Character String Search Command command stringfile grep read *.c Output: eg function (1), calls(30), comments(100) ex cd linux-practice/src cd lions/usr/sys/ken grep read *.c | more #--- find read() function #--- too many lines

  9. Where is getc() function? (2/5)Regular Expression • Previous approach: too much output • Find out exact location where function is defined: eg Find the line where function read() begins Regular Expression

  10. Skip RE if you already know

  11. finding exact location of a function definition int read (parameters); /* sys call */ eg return type space function name para comment [ C syntax ] • return type alphabets [a-Z]* • space bar | tab [\b \t]* • function name alphabet + any char [a-Z] .* • parameters ( expression )

  12. ---- Regular Expression: Metacharacters ---- . Any single char [ ] any single char from this set [abc] [a-z] [a-zA-Z] +one or more repetition of preceding item (.+ [a-z]+) * zero or more repetition of preceding item (.* [a-z]*) [^ ] negate the set (“caret”) ([^0-9] – any char except digit) [ \t] tab [ \b] blank ^ BOL $ EOL

  13. Strings in the target file . a b c d O .*O a aa aaa abc .+ a aa aaa abc aa (一般 search : no meta-char) abcabc (一般 search :no meta-char) ab+ ab abb abbb ab* a ab abb abbb ab. abc abd abf abc+ abc abccc abccccc ab[1-3] ab1 ab2 ab3 ab[1-3]+ ab11 ab1233 a[1-5]b[6-9] a1b6 a5b7 c[^a-Z]+ c1 c23 c243 ---- RE Examples ---- . Any char [ ] a char from this set [^ ] negate the set + one or more repeat * zero or more repeat [ \t] tab [ \b] blank ^ BOL $ EOL RE

  14. grep---regular expression type spacenamepara • grep ^[a-Z]* [ ]*read( *.c search in all .c files left-parenthesis match “read” string space-or-tab zero-or-more-repetition-of-previous-char any-alphabet-char beginning-of-new-line New line, function type, function_name ( The target pattern

  15. Escape – “Don’t handle this part” RE for grep shell grep ‘ [a-Z]* read(’ *.c (1) user sees shell prompt (2) types command, say, grep [a-Z]* read(*.c (3) shell reads input (line edit) • (Meaning of *’s are different) • grep’s interpretation of * (0 or more repetition) • sh’s interpretation of * (all .c files under cwd) • (1st *) is for grep (child process) • (2nd *) is for sh (current process) • escape – “Current process should not handle this part” (4) Escaped parts are un-touched by current process [a-Z]* read(’ grep

  16. Shell Escape • Escape a single char (\ -- back slash) • echo * vs echo \* • Escape string ( ‘ ‘ -- single quote), • echo ‘this part is not for shell’ • Escape string ( “ ” -- double quote), except • echo “this part not for shell except $PATH” • echo “Today is `date`” • A few exceptions such as: ($) variable expansion (``) command substitution (see next)

  17. Command substitution( ` ` back-quote) • execute inside ` ` • substitute command arg by output from ` ` eg echo date; echo `date` • mail `cat name_file` name_file: bob jim • mail bob jim dan dan

  18. shell escape exercise echo date echo `date` echo * echo \* echo ‘PATH is $PATH’ echo “PATH is $PATH” echo ‘Today is `date`’ echo “Today is `date`”

  19. Used in many commands vi, sh, perl, sed, awk minor differences … ---- Regular Expression ---- . Any char [ ] a char from this set [^ ] negate the set + one or more repetetion * zero or more repetetion [ \t] tab [ \b] blank ^ BOL $ EOL In Linux: -E should be used (grep -E …) -[ ] type blank as it is (not [\b]) -\{ escape needed for { e.g. grep -E“struct[ ]+buf[ ]+\{” -f file_name containing RE pattern

  20. To find MAX • char ps[MAX]; --- It must be a C constant looking for #define MAX 123 RE ^#define[ ]+MAX [ ]+[0-9]+ To find buf • struct buf *pst; --- It must be a C struct • looking for struct buf { • RE ^struct[ ]+ buf[ ]*\{ See next page for this

  21. ex cd cd lions/usr/sys/ken (1) to find struct buf grep –E ‘struct[ ]+buf[ ]*’ *.c (2) to find macro MAX grep –E ‘#define[ ]+MAX[ ]+’*.c

  22. Where is read() function?find src • What if many directories? • Run grep many times (each dir)? • find: traverse all nodes in a sub-tree • find /x -print • Visit each file under /x and print its pathname • find / -name ‘a.out’ -print • all files with name a.out (why single quote here?) • find . -size +12 -print • size 12 blocks • find / -mtime 7 -print • not modified for a week dan bob jim ex find / ^c

  23. Where is read() function?find src dan bob • find /x -exec wc {} \; for this pathname (file) end of this command (wc) • Visit each file under /x and • perform given command (wc) for the current given file • find /x -exec cat {} \; • Visit each file under /x and • cat each current given file • find / -name ‘a.out’ -exec rm {} \; jim ** locate command find is slow locate builds DB fast search

  24. 2 conditions src dan bob • find / -type d-user root -print • !: negate • O: or • conatenation: and • find /x -exec grep read {} \; • find /x -exec grep “^[a-Z]*[ ]+read\(” {} \; • find /x -exec grep -f use_file{} \; use_file: ^[a-Z]*[ ]+read\( • FIND: find /x -exec grep “^[a-Z]*[ ]+$1\(” {} \; FIND getc jim

  25. lions/src/usr/sys dmr ken buf.h ino.h ex cd cd lions/usr/sys/ken #----- to find definition struct “inode” #----- but it is not here in ken/ cd .. #--- to lions/usr/sys #--- visit every file below #--- and check definition of inode #--- use grep command find . -exec grep -E “struct[ ]+inode[ ]*”{} \;

More Related