You are on page 1of 51

CSE4009: Intro.

to System Programming

Fall 2018

Introduction

Youjip Won

Hanyang University
course synopsis
• Instructor: Prof. Youjip Won
• TA’s: Joontaek Oh, Inyeop Son
• Class
• Thursday: 13:00 - 15:00
• Friday: 13:00 - 15:00
• midterm: Oct. 26, final: Dec. 20
• 16 weeks
• two exams(midterm and final) and eleven homeworks
• prerequisite: C/C++, Data Structures
• grading: homework(50%), midterm(25%), final(25%)
• resources

• piazza.com/hanyang.ac.kr/fall2018/cse4009

Hanyang Univ. Youjip Won 2


topics
• Intro
• Building program: compiler, linking and loading
• Process and Address Space
• Virtual Address
• Interrupts, Traps, Exceptions, Device drivers
• Memory management and function call
• Lock
• CPU scheduling
• Filesystem and storage

Hanyang Univ. Youjip Won 3


tools
• We will read code of XV6.
• XV6 ( https://pdos.csail.mit.edu/6.828/2012/xv6.html )
• educational operating system developed by MIT
• 9,000 lines of code
• contains everything that is required to understand the basics of how the program runs.
• inspired by Unix V6 ( https://pdos.csail.mit.edu/6.828/2012/xv6.html )
• qemu
• gdb
• shell
• ctags/cscope, etags
• useful commands: nm, dtruss,

Hanyang Univ. Youjip Won 4


computing device

Hanyang Univ. Youjip Won 5


in essence from hardware

Hanyang Univ. Youjip Won 6


in essence from software

Hanyang Univ. Youjip Won 7


system program

Hanyang Univ. Youjip Won 8


프로그램의 일생

Hanyang Univ. Youjip Won 9


프로그램의 실행

Hanyang Univ. Youjip Won 10


cpu

Hanyang Univ. Youjip Won 11


library

Hanyang Univ. Youjip Won 12


system calls

Hanyang Univ. Youjip Won 13


function call vs. system call

Hanyang Univ. Youjip Won 14


system calls in xv6

Hanyang Univ. Youjip Won 15


Process and memory
• process = user memory (instructs, stacks and data) + process state
• context switch to execute multiple processes
• each process has pid
• System calls

• fork
• wait
• exit

Hanyang Univ. Youjip Won 16


Process and memory
• Process

• 프로세스는 유저 영역의 메모리와 프로세스 상태로 구성된다


• 유저 영역의 메모리는 명령어, 데이터, 스택으로 이루어진다
• 각 프로세스는 서로 다른 PID를 가진다

Hanyang Univ. Youjip Won 17


fork()
• 자식 프로세스를 생성

• 자식 프로세스는 부모 프로세스와 별개의 메모리를 할당 받지만 내용은 같다

• fork()의 리턴 값: 부모는 자식의 pid, 자식은 0이다

Hanyang Univ. Youjip Won 18


fork(): parent vs. child

Hanyang Univ. Youjip Won 19


fork()
int pid = fork();
if(pid > 0) {
printf(“parent: child=%d\n”, pid);
pid = wait();
printf(“child %d is done\n”, pid);
} else if(pid == 0) {
printf(“child: exiting\n”);
exit();
} else {
printf(“fork error\n”);
}

Hanyang Univ. Youjip Won 20


exec()
• 새로운 파일을 실행, 기존 프로세스의 메모리 영역을 새로 실행되는 프로세스의 메모리 영역으로 교체

• 실행 성공시 리턴을 하지 않고 실행할 파일의 시작지점의 명령어를 로드한다


• exec은 두개의 인자를 가짐: 실행 파일명, 인자의 배열

Hanyang Univ. Youjip Won 21


exec()

char *argv[3];

argv[0] = “echo”;
argv[1] = “hello”;
argv[2] = 0;
exec(“/bin/echo”, argv);
printf(“exec error\n”);

Hanyang Univ. Youjip Won 22


exec and shell
int main(void) {
static char buf[100];
int fd;

// Ensure that three file descriptors are open.


while((fd = open("console", O_RDWR)) >= 0){
if(fd >= 3){
close(fd);
break;
}
}

// Read and run input commands.


while(getcmd(buf, sizeof(buf)) >= 0){
if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' '){
// Chdir must be called by the parent, not the child.
buf[strlen(buf)-1] = 0; // chop \n
if(chdir(buf+3) < 0)
printf(2, "cannot cd %s\n", buf+3);
continue;
}
if(fork1() == 0)
runcmd(parsecmd(buf));
wait();
}
exit();
}
Hanyang Univ. Youjip Won 23
wait()

Hanyang Univ. Youjip Won 24


exec in shell
// Execute cmd. Never returns.
void runcmd(struct cmd *cmd) {
int p[2];
struct backcmd *bcmd;
struct execcmd *ecmd;
struct listcmd *lcmd;
struct pipecmd *pcmd;
struct redircmd *rcmd;

if(cmd == 0)
exit();

switch(cmd->type){
default:
panic("runcmd");

case EXEC:
ecmd = (struct execcmd*)cmd;
if(ecmd->argv[0] == 0)
exit();
exec(ecmd->argv[0], ecmd->argv);
printf(2, "exec %s failed\n", ecmd->argv[0]);
break;

...
}
}
Hanyang Univ. Youjip Won 25
I/O and File descriptor
• File descriptor
• 파일, 파이프, 디렉토리, 디바이스를 표현하는 정수
• 프로세스는 File descriptor를 통해 파일, 디렉터리를 열거나 파이프를 만들거나 descriptor를 복제할 수 있다

• 프로세스마다 File descriptor 테이블 존재


• File descriptor 0 (Standard Input), 1 (Standard Output), 2 (Standard Error)는 항상

Hanyang Univ. Youjip Won 26


I/O and File descriptor (Cont.)

Hanyang Univ. Youjip Won 27


I/O and File descriptor (Cont.)

Hanyang Univ. Youjip Won 28


I/O and File descriptor (Cont.)
• Read(fd, buf, n)
• 첫 번째 인자는 File descriptor, 두 번째는 읽어 들일 buffer, 세 번째는 읽을 크기
• 파일의 offset은 읽은 byte 만큼 이동한다
• 파일을 끝까지 읽어 더 이상 읽을 byte가 없으면 0을 리턴한다.
• Write(fd, buf, n)
• 첫 번째 인자는 File descriptor, 두 번째는 파일에 쓸 buffer, 세 번째는 쓸 데이터의 크기
• 에러가 발생하면 n byte보다 작은 크기가 입력된다
• 파일의 offset은 쓴 byte만큼 이동한다

Hanyang Univ. Youjip Won 29


I/O and File descriptor (Cont.)
• Example of ‘cat’

char buf[512];
int n;

for(;;) { Standard Input


n = read(0, buf, sizeof(buf));
if(n == 0)
break;
if(n < 0) { Standard Error
fprintf(2, “read error\n”);
exit();
} Standard Output
if(write(1, buf, n) != n) {
fprintf(2, “write error\n”);
exit();
}
}

Hanyang Univ. Youjip Won 30


I/O and File descriptor (Cont.)
• close(fd)
• File descriptor를 해제
• 새로 할당되는 File descriptor는 사용할 수 있는 가장 낮은 File descriptor로 지정된다.
• File descriptor and system call
• fork()를 통해 부모 프로세스의 메모리가 자식 프로세스에 복사될 때 File descriptor table도 복사

• exec()가 호출되면 새 프로세스의 메모리가 이전 프로세스의 메모리에 덮어 쓰여지지만 File descriptor table은 유지
• 이러한 동작들은 fork, reopen, exec을 실행 했을 때도 I/O redirection을 가능하게 한다

Hanyang Univ. Youjip Won 31


I/O and File descriptor (Cont.)

if(fork() == 0) {
write(1, “hello “, 6);
exit();
} else {
wait();
write(1, “world\n”, 6);
}

Hanyang Univ. Youjip Won 32


I/O and File descriptor (Cont.)
• Redirection
• 데이터의 입출력 방향을 바꿔준다
• 예약된 File descriptor 0~2를 close한 후 바로 open을 하여 standard input / output / error를 사용자가 사용할 수 있다

• 예를 들어 File descriptor 1번(Standard output)을 닫고 바로 텍스트 파일을 open하면 콘솔에 입력한 내용이 텍스트 파
일에 저장된다
• 쉘 명령어에서는 ‘>‘로 redirection을 설정할 수 있다

char *argv[2];

argv[0] = “cat”;
argv[1] = 0;
if(fork() == 0) {
close(0);
open(“input.txt”, O_RDONLY);
exec(“cat”, argv);
}

Hanyang Univ. Youjip Won 33


I/O and File descriptor (Cont.)
• duplicate file descriptor: dup() system call

fd = dup(1);
write(1, “hello “, 6);
write(fd, “world\n”, 6);

Hanyang Univ. Youjip Won 34


I/O and File descriptor (Cont.)
• Redirection in shell

case REDIR:
rcmd = (struct redircmd*)cmd;
close(rcmd->fd);
if(open(rcmd->file, rcmd->mode) < 0){
printf(2, "open %s failed\n", rcmd->file);
exit();
}
runcmd(rcmd->cmd);
break;

Hanyang Univ. Youjip Won 35


Pipes
• 프로세스간 공유할 수 있는 커널 버퍼이다
• Read와 Write를 위한 File descriptor의 쌍으로 구성되어 있다

• 한 쪽 끝에서는 데이터를 read하고, 다른 끝에서는 write한다


• 더 이상 데이터가 없을 때, 데이터의 쓰기 혹은 write를 위한 descriptor들이 모두 닫히기를 대기.

Hanyang Univ. Youjip Won 36


Pipes (Cont.)
int p[2];
char *argv[2];

argv[0] = “wc”;
argv[1] = 0;

pipe(p);
if(fork() == 0) {
close(0);
dup(p[0]);
close(p[0]);
close(p[1]);
exec(“/bin/wc”, argv);
} else {
close(p[0]);
write(p[1], “hello world\n”, 12);
close(p[1]);
}

Hanyang Univ. Youjip Won 37


Hanyang Univ. Youjip Won 38
Hanyang Univ. Youjip Won 39
Hanyang Univ. Youjip Won 40
Hanyang Univ. Youjip Won 41
Pipes
• Four advantages of pipes over temporary files
• 임시 파일을 이용한 방법은 사용자가 임시 파일을 삭제할 때 주의해야 하지만 파이프는 운영체제가 자동적으로 처리한

• File redirection 방식은 임시 파일을 저장하기 위해 디스크를 사용하지만 파이프는 메인 메모리만 사용한다

• 파이프는 병렬 처리가 가능하다


• 파이프는 blocking read와 write를 하기 때문에 non-blocking인 임시 파일보다 효율적이다

Hanyang Univ. Youjip Won 42


File system
• 파일 시스템은 데이터 파일과 디렉터리를 제공한다
• 디렉터리는 트리로 구성되어 있으며, Root 디렉터리부터 시작한다

• 경로가 ‘/’로 시작하면 Root 디렉터리이고, ‘/’로 시작하지 않으면 프로세스의 Current 디렉터리이다
• Current 디렉터리는 chdir 시스템 콜에 의해 변경될 수 있다

chdir(“/a”);
chdir(“b”);
open(“c”, O_RDONLY);

open(“/a/b/c”, O_RDONLY);

Hanyang Univ. Youjip Won 43


File system (Cont.)
• 파일 생성
• mkdir : 새로운 디렉터리를 생성한다

• open with O_CREATE : 새로운 파일을 생성한다


• mknode : 새로운 디바이스 파일을 생성한다

mkdir(“/dir”);
fd = open(“/dir/file”, O_CREAT|O_WRONLY);
close(fd);
mknod(“/console”, 1, 1);

Hanyang Univ. Youjip Won 44


File system (Cont.)

Hanyang Univ. Youjip Won 45


File system (Cont.)
• fstat
• File descriptor가 참조하는 객체의 정보를 가져오는 시스템 콜이다
• fstat으로 가져올 수 있는 정보는 아래와 같다

#define T_DIR 1 // Directory


#define T_FILE 2 // File
#define T_DEV 3 // Device

struct stat {
short type; // Type of file
int dev; // File system's disk device
uint ino; // Inode number
short nlink; // Number of links to file
uint size; // Size of file in bytes
};

Hanyang Univ. Youjip Won 46


File system (Cont.)
• link
• 기존의 파일과 동일한 inode를 참조하는 파일의 다른 이름을 생성한다
• a에 읽고 쓰는 동작은 b에서 읽고 쓰는 것과 같다

• fstat의 결과도 동일하며, 동일한 inode 번호를 반환한다


• 링크 카운트(nlink)는 2로 설정된다

open(“a”, O_CREAT|O_WRONLY);
link(“a”, “b”);

Hanyang Univ. Youjip Won 47


Hanyang Univ. Youjip Won 48
File system (Cont.)
• unlink
• 파일 시스템으로부터 파일의 이름을 제거하는 시스템 콜이다
• inode와 데이터를 가진 디스크 공간은 링크 카운트가 0이 되고, File descriptor가 아무것도 가리키지 않을 때 해제된다

• a의 링크가 끊어져도 b를 통해 접근이 가능하다


• File descriptor를 close하거나 프로세스를 종료했을 때 링크가 끊어진 임시 inode가 만들어지는 과정은 위의 예제와 같다

open(“a”, O_CREAT|O_WRONLY);
link(“a”, “b”);
unlink(“a”);

fd = open(“/tmp/xyz”, O_CREAT|O_RDWR);
unlink(“/tmp/xyz”);

Hanyang Univ. Youjip Won 49


command types in shell
• user program with fork()/exec(): mkdir, ln, rm
• built-in command: cd
• cd는 현재 디렉터리를 변경필요. fork() 후 exec(‘cd’)를 하면 자식 프로세스의 ‘현재디렉터리’가 변경됨.

Hanyang Univ. Youjip Won 50


summary
• process
• memory
• file
• filesystem

• tools xv6

Hanyang Univ. Youjip Won 51

You might also like