Professional Documents
Culture Documents
Class1 PDF
Class1 PDF
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
• fork
• wait
• exit
char *argv[3];
argv[0] = “echo”;
argv[1] = “hello”;
argv[2] = 0;
exec(“/bin/echo”, argv);
printf(“exec error\n”);
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를 복제할 수 있다
char buf[512];
int n;
• exec()가 호출되면 새 프로세스의 메모리가 이전 프로세스의 메모리에 덮어 쓰여지지만 File descriptor table은 유지
• 이러한 동작들은 fork, reopen, exec을 실행 했을 때도 I/O redirection을 가능하게 한다
if(fork() == 0) {
write(1, “hello “, 6);
exit();
} else {
wait();
write(1, “world\n”, 6);
}
• 예를 들어 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);
}
fd = dup(1);
write(1, “hello “, 6);
write(fd, “world\n”, 6);
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;
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]);
}
• 경로가 ‘/’로 시작하면 Root 디렉터리이고, ‘/’로 시작하지 않으면 프로세스의 Current 디렉터리이다
• Current 디렉터리는 chdir 시스템 콜에 의해 변경될 수 있다
chdir(“/a”);
chdir(“b”);
open(“c”, O_RDONLY);
open(“/a/b/c”, O_RDONLY);
mkdir(“/dir”);
fd = open(“/dir/file”, O_CREAT|O_WRONLY);
close(fd);
mknod(“/console”, 1, 1);
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
};
open(“a”, O_CREAT|O_WRONLY);
link(“a”, “b”);
open(“a”, O_CREAT|O_WRONLY);
link(“a”, “b”);
unlink(“a”);
fd = open(“/tmp/xyz”, O_CREAT|O_RDWR);
unlink(“/tmp/xyz”);
• tools xv6