140 likes | 778 Views
Lab 1. 시스템 호출을 이용하는 mycp.c. mycphint1.c & mycphint2.c coding & compile & run 제출 방법 ( 집에서도 접속 가능합니다 ) 2 Electronic versions: multi.incheon.ac.kr (117.16.244.53) 의 지정 디렉토리 /export/home/os2011hwa 또는 os2011hwb 에 자기 학번의 디렉토리 만들고 그 곳에 소스파일과 실행파일 복사
E N D
Lab 1. 시스템 호출을 이용하는 mycp.c • mycphint1.c &mycphint2.c coding & compile & run • 제출 방법 (집에서도 접속 가능합니다) • 2 Electronic versions: • multi.incheon.ac.kr (117.16.244.53)의 지정 디렉토리 /export/home/os2011hwa 또는 os2011hwb 에 자기 학번의디렉토리 만들고 그 곳에 소스파일과 실행파일 복사 • mylinux.incheon.ac.kr(117.16.244.59) 지정 디렉토리 /home/os2011hwa 또는 os2011hwb 에 자기 학번의디렉토리 만들고 그 곳에 소스파일과 실행파일 복사 운영체제
Example of System Calls • System call sequence to copy the contents of one file to another file $ mycp a b 운영체제
(Hint 1: mycphint1.c) 과제 2-2 연습문제 2.18 #include <stdio.h> #include <unistd.h> #define NAME_LENGTH 25 int main(void) { char in_file[NAME_LENGTH], out_file[NAME_LENGTH]; FILE *in, *out; int c; printf("Enter source file name: "); scanf("%s",in_file); printf("Enter destination file name: "); scanf("%s",out_file); if ( (in = fopen(in_file,"r")) == NULL) { fprintf(stderr,"Cannot open %s for reading\n",in_file); return -1; } if ( (out = fopen(out_file,"w")) == NULL) { fprintf(stderr,"Cannot open %s for writing\n",out_file); return -1; } while ( (c = getc(in)) != EOF) putc(c,out); fclose(in); fclose(out); } 운영체제
3/28야 (Hint 2: mycphint2.c) 과제 2-2 연습문제 2.18 #include <stdio.h> #include <stdlib.h> #define PERMS 0644 char *progname; main(int argc, char *argv[]) { int f1, f2, n; char buf[BUFSIZ]; if (argc != 3) printf("Usage: %s a b\n", progname); if ((f1 = open(argv[1], O_RDONLY, 0)) == -1) printf("can't open %s\n", argv[1]); if ((f2 = open(argv[2], O_RDWR|O_CREAT|O_APPEND )) == -1) printf("can't creat %s\n", argv[2]); while ((n = read(f1, buf, BUFSIZ)) > 0) if (write(f2, buf, n) != n) printf("write errono"); exit(0); } 수정1: include 수정2: permission 수정3: truncate 운영체제
(C언어 보충) Command-line Arguments • Command $ gcc myecho.c –o myecho $ ./myecho hello world! • Output hello world! • argc 와 argv[] • argc=3 • argv[0]: “echo” • argv[1]: “hello” • argv[2]: “world” • Source code $ cat myecho.c #include <stdio.h> main(int argc, char *argv[]) { int i; for (i = 1; i < argc; i++) printf(“%s%s”, argv[i], (i< argc-1) ? “ “ : “”); printf(“\n”); return 0; } argv: myecho\0 hello\0 world\0 0 운영체제
(C언어 보충) argv 처리: optional flag • myecho2.c (숫자 option 처리) $ cat myecho2.c #include <stdio.h> main(int argc, char *argv[]) { int i; for(i=1; i<argc; i++) { printf("%s%s", argv[i], (i<argc-1)? " “ : ""); if(argv[i][0] == '-') printf(“ (제곱값은 %d) ", (atoi(argv[i]))*(atoi(argv[i]))); } printf("\n"); return 0; } 운영체제
(C언어 보충) argv 처리: optional flag • myfind-n -x pattern $ cat myfind.c #include <stdio.h> #include <string.h> #define MAXLINE 1000 int getline(char *line, int max); /*find : print lines that match pattern from 1st arg */ main(int argc, char *argv[]) { char line[MAXLINE]; long lineno = 0; int c, except =0, number =0, found =0; while(--argc > 0 && (*++argv)[0] == '-') while(c = *++argv[0]) switch(c) { case 'x': except = 1; break; case 'n': number = 1; break; default: printf("find : illegal optin %c\n",c); argc = 0; found = -1; break; } if(argc != 1) printf("Usage : find -x -n patttern\n"); else while(getline(line, MAXLINE) > 0) { lineno++; if((strstr(line, *argv) != NULL ) != except) { if(number) printf("%ld:", lineno); printf("%s\n", line); found++; } } return found; } int getline(char s[], int lim) { int c, i; for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = '\0'; ++i; } s[i] = '\0'; return i; } 운영체제
Linux/Unix C 언어 프로그래밍 • cc compiler • man cc • $ cc –o hello hello.c • $ hello • gcc compiler • GNU project C and C++ Compiler • GNU(GNU’s Not Unix, Free Software Foundation의 Richard Stallman) • man gcc • info gcc • $ gcc hello.c • $ a.out • $ gcc –o hello hello.c • $ hello 운영체제
유닉스 디버거 : gdb • 프로그램을 기호적으로 디버깅 : dbx, adb, sdb, gdb, ddd(Motif) 등 • 단일단계이동(single stepping) • 정지점(breakpoint) • 디버거 내에서 편집 • 변수의 접근 및 수정 • 함수의 탐색 • 추적(tracing) • gdb • GNU debugger, 별도의 프롬프트 표시 • 관련정보는, 프롬프트에서 help를 입력 • dbx executableFilename • 디버그를 위한 프로그램 준비 • cc의 -g 옵션으로 프로그램을 번역 • ⇒ 목적 모듈 내에 디버깅 정보 포함 운영체제
유닉스 디버거 : gdb • gdb 실행gdb 실행 파일이름 : 실행 파일을 디버그함 (실행 파일은 반드시 -g 옵션을 사용하여 컴파일되어야 함) • gdb 명령어 • b (breakpoint) : 실행 중 디버그를 위해 멈추는 위치 지정 • b 함수명 : 함수명에서 멈춤 • b 라인번호 : 라인번호에서 멈춤 • r (run) : 실행 시작 • n (next) : 현재 라인 실행 (함수의 경우 실행하고 다음 라인으로 넘어 감) • s (step) : 현재 라인 실행 (함수의 경우 호출된 함수 내로 들어가 실행 계속) • c (continue) : 다음 breakpoint까지 실행 • l (list) : 현재 수행되고 있는 라인부터 10개 라인씩 연속적으로 소스 코드를 프린트 • p (print) 변수명 : 변수명으로 저장되어 있는 내용을 프린트 • h (help) : 도움말 • q (quit) : gdb 종료 운영체제
replace append insert open esc esc esc esc a i R oO $Vi filename ~ ~ Vi mode (Vi 명령 모드) ~ $ :q! (기록 않음) ZZ(기록) :wq x dd r 커서이동 Vi 편집 모드 운영체제
커서의 이동 hjkl 이용 H J K L [Vi mode] 4j G 명령으로원하는 행으로 이동 7G G : 마지막 행으로 화면 이동 ^F ^B ^D ^U 텍스트 추가, 삽입, 수정 a(append) i(insert) o(open) O(Open) R(Replace) 텍스트의 삭제 및 취소(undo) x(exclude?) d(delete) dw db d$ d^ r(replace) u(update) U(Update) 최근 명령 재 실행 . 파일 관리 Vi를 벗어나지 않고 저장하기: :w 저장 않고 끝내기: :q! 또 다른 파일 편집: :e xx 또는 :e! xx 다른 파일을 읽어 와 덧붙이기: :r xx http://marvel.inchon.ac.kr/ 의 Information 참조 Vi를 이용한 기본 텍스트 편집 운영체제