LoginSignup
0
1

More than 5 years have passed since last update.

読書メモ: xv6 book - Chapter 0: Operating System Interfaces

Last updated at Posted at 2018-03-22

system call がインターフェースを決める。system call が発行されると、user space から kernel space への切り替えが起きて、操作を実行してまた user space にもどる。user space にいるときは、そのプロセスがアクセスできないメモリはCPU レベルの hardware protection によって保護される

xv6 のサービスは、
- process
- memory
- file descriptor
- pipe
- file system
からなる

process

process は、memory (instruction, data, stack) と、kernel private な状態からなる。各プロセスには pid が割り当てられる

  • fork: メモリをコピーして子プロセスを作る fd table はコピーされ、file offset は共有される
  • exit: プロセスを終了させ、リソースを開放
  • wait: 子プロセスのどれかの exit を待ち、その pid を得る
  • exec: ELF binary を実行. fd table はそのまま

  • sbrk: メモリを n bytes 広げる

file descriptor

  • Refer to a kernel managed object, a process may read from or to.
  • Abstracts files, pipes, devices

fd table は per process. fd -> resource

  • read - fd から読む。file offset は、dup, fork で共有
  • close - fd を後の open で再利用可能にする
  • open - 使われてない一番小さい fd になる. 0 が closed なら 0 -> I/O redirection
  • dup - duplicates an existing fd, and returns new one refering to the same underlying I/O object. Shares file offset.

2>&1 は、2 は 1 の dup であるということ

pipe

pipe とは small kernel buffer.
pipe() は (read end fd, write end fd) を返す

read on a pipe は、write end に書かれるか、write end が close されるまで待つ

A | B | C というシェルコマンドは、

  X
 / \
A   Y
   / \
  B   C

みたいな親子関係のプロセスになる。Y は B, C の終了を待って exit する

file system

  • chdir - change process's current directory. 相対パスのルートを変える
  • mkdir - directory を作る
  • open(..., O_CREATE) - data file を作る
  • mknod - device file を作る device file かは file metadata で区別
  • fstat - file 情報. stat.h は以下からなる
    • type - ファイルタイプ
    • dev - disk device
    • ino - inode. ファイルの実体に対応。パスは複数持てる
    • nlink - #links to file. 複数のパスをもつ場合 > 1
    • size - size in bytes
  • unlink - 名前を消す。link count が 0 かつそれを指す fd がないとき実体を消す
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1