프로그램 만으로는 불가능한 기능을, 커널의 기능을 빌려서 수행
WIN16 API, WIN32 API, WIN64 API
프로그램의 하위 호환성을 제공
Win 2.0의 프로그램이 Win 7에서도 수행
kernel에 어셈블리 코드가 포함되어있지만, 직접 접근할 수 없으므로 system calls이 중간 인터페이스의 역할을 한다.
Unix Standard Header
Unix 시스템 호출 전반을 담당하는 헤더 파일
File Control Header
Unix 시스템 중 파일을 담당하는 헤더 파일
#include <stdio.h>
#include <unistd.h>
int main() {
print£("UID : $d, EVID : Sd\n",getuid(), geteuid());
print£("GID : $d, EGID : $d\n",getgid(), getegid());
return 0;
}
C언어 : fopen() (stdio.h)
Unix 호출 : open() (fcntl.h)
사용법 : open(파일명, 모드, 권한)
C언어 : FILE* fp = fopen(“input.txt”, “w+”);
Unix : int fd = open(“input.txt”, O_RDWR);
파일 서술자(File Descriptor)?
0 : stdin (표준 입력, 파일로부터 읽기만 가능)
1 : stdout (표준 일반 출력, 파일에 쓰기만 가능)
2 : stderr (표준 오류 출력, 파일에 쓰기만 가능)
파일 서술자 추가 시, 할당되지 않은 서술자 중 가장 작은 번호로 자동 할당한다
O_RDONLY : 읽기 전용으로 열기
O_WRONLY : 쓰기 전용으로 열기
O_RDWR : 읽기/쓰기 겸용으로 열기
O_APPEND : 파일의 끝에서부터 추가
O_CREAT : 파일이 없으면 새로 생성
O_TRUNC : 파일이 이미 있으면 내용을 삭제
O_EXCL : 파일이 이미 있으면 오류 발생
int fd = creat (파일명, 권한);
close(fd);
int fd2 = dup(fd1);
dup2(fd1, fd2);
#include <stdlo.n>
#include <unistd.h>
#include <fcntl.h>
#define TEST "test.txt"
int main()
{
int fd = open(TEST,O_RDWR|O_CREAT, 0644);
printf("Before dup2()\n");
dup2(fd,1);
printf ("After dup2()\n");
close(fd);
return 0;
}
unix@unixsystem:~$ cc -o dup dup.c
unix@unixsystem:~$ ./dup
Before dup2()
unix@unixsystem:~$ cat test.txt
After dup2()
unix@unixsystem:~$ _
dup2() 이후 test.txt 파일에 출력되었다. (표준 입출력 자체에도 영향을 끼칠 수 있다.)
int result = read(fd, buffer, bytes)
int result = write(fd, buffer, bytes)
#incluage <unistd.h>
#include <fcntl.h>
int main()
{
int bytes;
char buffer[256];
do
{
bytes = read(0,buffer,255);
if(bytes <= 0) break;
write(1,buffer,bytes);
}
while(1);
return 0;
}
unix@unixsystem:~$ cc -o read read.c
unix@unixsystem:~$ ./read
Unix System Programming
Unix System Programming
Text read from stdin
Text read from stdin
and written to stdout
and written to stdout
Press Ctrl+D to terminate current input
Press Ctrl+D to terminate current input
Ctrl+D also breaks out if nothing was read.
Ctrl+D also breaks out if nothing was read.
unix@unixsystem:~$ _
#include <stdio.h>
int main(int argc, char* argv[]){
}
int main(int argc, char** argv){
}
argc : 매개변수의 개수 (파일 실행 명령어 포함)
argv : 각각의 매개변수
#include <stdio.h>
int main(int argc, char* argv[]){
int count;
printf(“argc : %d\n”, argc);
printf(“------arguments list------\n”);
for(count=0; count < argc ; count++){
printf(“argv[%d] : %s\n”, count, argv[count]);
}
return 0 ;
}
lseek()
UNIX - system call [exit, wait , waitpid], zombie process, orphan process (0) | 2021.10.16 |
---|---|
UNIX - 파일 및 디렉터리 조작하기 (0) | 2021.10.15 |
UNIX - compile 명령어 정리, Makefile (0) | 2021.10.12 |
UNIX - main function, process, fork, exec (0) | 2021.10.09 |
UNIX - current directory, sync / fsync, device files (0) | 2021.10.08 |