Linux/UNIX Programming
APUE (Introduction)
문양세강원대학교 IT 대학 컴퓨터과학전공
UNIX System Program-mingby Yang-Sae Moon
Page 2
APUE 강의 목적
UNIX 시스템 프로그래밍
• 파일 , 프로세스 , 시그널 (signal), 네트워크 프로그래밍
UNIX 시스템의 체계적 이해
시스템 프로그래밍 능력 향상
APUE (Introduction)
UNIX System Program-mingby Yang-Sae Moon
Page 3
APUE 강의 동기
UNIX 는 인기 있는 운영체제
• 서버 시스템 ( 웹 서버 , 데이터베이스 서버 )
• 공학 및 상업용으로 응용되고 있음
• 연구 개발 용으로도 많이 사용됨 ( 특히 , Sun 의 경우 학교 ( 미국 등 ) 에서 애용함 )
• 최근 리눅스가 임베디드 시스템 및 모바일 디바이스에 많이 활용되고 있음
시스템 프로그래밍
• OS 자원을 이용한 프로그래밍
• Unix 시스템 호출 사용
• 파일 , 프로세스 , IPC, 네트워킹 , …
• DBMS, compiler, groupware, debugger, …
APUE (Introduction)
UNIX System Program-mingby Yang-Sae Moon
Page 4
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
APUE (Introduction)
Hardware
UNIX KernelSystem call
Commands, Utilities, Applications
UNIX System Program-mingby Yang-Sae Moon
Page 5
시스템 호출 (System Calls) (1/2)
APUE (Introduction)
Process Process Process
System call interface
File Management IPC Process Manage-ment
UNIX System Program-mingby Yang-Sae Moon
Page 6
Application programs talk to the operating systems via system
calls.
The programmer's functional interface to the UNIX kernel.
APUE (Introduction)시스템 호출 (System Calls) (2/2)
UserProcess
System call Library
Kernel
trap routine
call dispatch
User space
Kernel space
read()
rdwr()
readi(), writei()
fgetc(), fscanf()
UNIX System Program-mingby Yang-Sae Moon
Page 7
User Mode vs. KernalAPUE (Introduction)
Kernel
Address of kernel open()
Address of kernel write()
Address of kernel close()
kernel code for open()
{ <Manipulate kernel data>
. . .
<Return to user code>
}
Kernel system call code
open(char *name, int mode) { <Place parameters in registers>
<Execute trap instruction,
switching to kernel code >
<Return result of system call>
}
User process
C runtime
library
User code
result=open(“file.txt”, O_RDONLY);
UNIX System Program-mingby Yang-Sae Moon
Page 8
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()
APUE (Introduction)
UNIX System Program-mingby Yang-Sae Moon
Page 9
System Calls & Library Functions (2/2)
APUE (Introduction)
application code
C library func-tions
system calls
hardware (hard disk…)
user process
kernel
kernel
UNIX System Program-mingby Yang-Sae Moon
Page 10
C I/O Library Functions
표준 입력 함수 (standard input function)• scanf(), getchar()
표준 출력 함수 (standard output function)• printf(), putchar()
표준 파일 입력 함수• fscanf(), fgets(), fgetc()
표준 파일 출력 함수• fprintf(), fputs(), fputc()
APUE (Introduction)
UNIX System Program-mingby Yang-Sae Moon
Page 11
파일 입출력 (1/2)
파일 입출력 과정• 파일 열기 , 읽기 / 쓰기 , 파일 닫기
파일 열기 : fopen#include <stdio.h>
FILE *fp;
fp=fopen(" 파일 이름 ", " 입출력방식 ");
입출력 방식• r: 읽기 전용• w: 쓰기 전용• a: 추가 (append) 수록• r+: 읽기쓰기겸용• w+: 쓰기읽기겸용• a+: 읽기추가겸용
APUE (Introduction)
UNIX System Program-mingby Yang-Sae Moon
Page 12
파일 입출력 (2/2)
파일 입력 함수• fscanf(), fgets(), fgetc()• 열린 파일에서 내용을 읽어 들이는 함수
파일 출력 함수• fprintf(), fputs(), fputc()• 열린 파일에 내용을 기록하는 함수
파일 닫기• fclose()
APUE (Introduction)
UNIX System Program-mingby Yang-Sae Moon
Page 13
파일 출력 예제 (fgets.c) (1/2)APUE (Introduction)
UNIX System Program-mingby Yang-Sae Moon
Page 14
파일 출력 예제 (fgets.c) (2/2)APUE (Introduction)
UNIX System Program-mingby Yang-Sae Moon
Page 15
파일 입출력 예제 (fcopy.c) (1/2)APUE (Introduction)
UNIX System Program-mingby Yang-Sae Moon
Page 16
파일 입출력 예제 (fcopy.c) (2/2)APUE (Introduction)
UNIX System Program-mingby Yang-Sae Moon
Page 17
시스템 프로그래밍 준비APUE (Introduction)
에디터
컴파일러
디버거
make
We already knew “vi”.
We may use “cc” or “gcc”
You should learn either “gdb” or “dbx”.
We already learned about “make” and “Make-file”.
Top Related