컴퓨터특강 () [Ch. 1 & Ch. 2] 2006 년봄학기 문양세강원대학교컴퓨터과학과 APUE 강의목적 UNIX 시스템프로그래밍 file, process, signal, network programming UNIX 시스템의체계적이해 시스템프로그래밍능력향상 Page 2 1
APUE 강의동기 UNIX 는인기있는운영체제 서버시스템 ( 웹서버, 데이터베이스서버 ) 공학및상업용으로응용되고있음 연구개발용으로도많이사용됨 ( 특히, Sun의경우학교 ( 미국등 ) 에서애용함 ) Linux: PC를위한무료 OS 시스템프로그래밍 OS 지원을이용한프로그래밍 Unix 시스템호출사용 file, process, IPC, networking, DBMS, compiler, groupware, debugger, Page 3 UNIX 의특징 단순하고이식성이좋다 MIT MULTICS에반대해서 C 언어로작성 (Portable한특징을가짐 ) 다중사용자다중프로세스 (multi user, multi process) 운영체제 여러사용자가동시에사용할수있다. 여러프로그램이동시에실행될수있다. 관리자인 Super-User가있다. 쉘프로그래밍 (Shell Programming) 유틸리티프로그램과파이프라인메커니즘이용 ( 예 : $ who sort) MS-DOS의 batch file과비슷하다. Page 4 2
UNIX 시스템구조 Hardware CPU, Memory, Disk, Peripherals Kernel Kernel Process Management File Management Memory Management Device management System Call The programmer's functional interface to the UNIX kernel Commands, Utilities, Application programs Kernel services using library routines or system calls Hardware UNIX Kernel System call Commands, Utilities, Applications Page 5 시스템호출 (System Calls) (1/2) Process Process Process System call interface File Management IPC Process Management Page 6 3
시스템호출 (System Calls) (2/2) Application programs talk to the operating systems via system calls. The programmer's functional interface to the UNIX kernel. User Process fgetc(), fscanf() User space Kernel space System call Library trap routine call dispatch Kernel read() rdwr() readi(), writei() Page 7 User Mode vs. Kernal User process result=open( file.txt, O_RDONLY); User code Kernel Address of kernel close() Address of kernel open() Address of kernel write() open(char *name, int mode) { <Place parameters in registers> <Execute trap instruction, switching to kernel code > <Return result of system call> C runtime library kernel code for open() { <Manipulate kernel data>... <Return to user code> Kernel system call code Page 8 4
System Calls & Library Functions (1/2) System Calls Well defined entry points directly into the kernel Documented in section 2 of the UNIX man pages (/usr/man/man2) Look like C functions which can be called from a user's program just need to include the appropriate header 예 ) read(), write() Library Functions The library function is often more elaborate than the system call, and usually invokes the system call (System call 보다는좀더사용하기쉽고편리하게제작 ) 예 ) fprintf(), fscanf() Page 9 System Calls & Library Functions (2/2) application code user process C library functions system calls kernel kernel hardware (hard disk ) Page 10 5
C I/O Library Functions 표준입력함수 (standard input function) scanf(), getchar() 표준출력함수 (standard output function) printf(), putchar() 표준파일입력함수 fscanf(), fgets(), fgetc() 표준파일출력함수 fprintf(), fputs(), fputc() Page 11 파일입출력 (1/2) 파일입출력과정 파일열기, 읽기 / 쓰기, 파일닫기 파일열기 : fopen #include <stdio.h> FILE *fp; fp=fopen(" 파일이름 ", " 입출력방식 "); 입출력방식 r: 읽기전용 w: 쓰기전용 a: 추가 (append) 수록 r+: 읽기쓰기겸용 w+: 쓰기읽기겸용 a+: 읽기추가겸용 Page 12 6
파일입출력 (2/2) 파일입력함수 fscanf(), fgets(), fgetc() 열린파일에서내용을읽어들이는함수 파일출력함수 fprintf(), fputs(), fputc() 열린파일에내용을기록하는함수 파일닫기 fclose() Page 13 파일출력예제 (fgets.c) // 키보드에서문자를입력받아파일에출력하는예제 #include <stdio.h> main() { FILE *fp; int c; fp = fopen(./test.txt","w"); // 쓰기전용으로 c = getchar(); // 키보드로부터입력받은문자의 ASCII 코드반환 while(c!=. ) { //. 가아니면. fputc(c, fp); //fp가가리키는파일에문자 c저장 c = getchar(); // 키보드로부터문자를읽어 c에저장 fclose(fp); Page 14 7
파일입출력예제 (fcopy.c) // 파일 (fgets.c) 에서읽어들여파일 (test.txt) 에출력하는함수 #include <stdio.h> void main() { FILE *fp; FILE *fp_write; char buffer[100];// 데이터를임시로저장하기위한배열 fp=fopen("fgets.c","r"); fp_write=fopen("test.txt","w"); if(fp==null) printf("file not found"); /* 최대길이가 100인문자열을 fp가가리키는파일에서읽어서 buffer에저장후 fp_write가가리키는파일에 buffer 내용기록 */ while(fgets(buffer, 100, fp)!=null) fputs(buffer, fp_write); fclose(fp); Page 15 시스템프로그래밍준비 에디터 We already knew vi. 컴파일러 We can use gcc. Thanks to 양기운 / 박기덕 디버거 You should learn either gdb or dbx. make We already learned about make and Makefile. Page 16 8