C言語再入門、ふつうのLinuxプログラミング

5章:ストリームにかかわるシステムコール

主要なシステムコールは4つ

  • read
    • ストリームからバイト列を読み込む
  • write
    • ストリームにバイト列を書き込む
  • open
    • ストリームをつくる
  • close
    • 用済みのストリームを始末する

ファイルディスクリプタ

  • プログラムから見ると、ただの整数値
  • カーネルが持っているストリームを番号と対応付け、プロセスには番号でストリームを指定させる

perror(3)

#include

void perror(const char *s);

ファイルオフセット

  • 同じfdに対して、何度もread()を呼ぶと必ず前回の続きが帰ってくる

lseek(2)

#include
#include

off_t lseek(int fk, off_t offset, int whence);

  • df内部のファイルオフセットを指定した位置offsetに移動する。

dup(2),dup2(2)

#include

int dup(int oldfd);
int dup2(int oldfd, int newfd);

  • oldfdを複製する

ioctl(2)

#include

int ioctl(int fd, int request, ..)

  • open(),read(),write(),close()という統一インタフェースからはみ出した部分は、すべてioctl()に集められている。
  • requestのリストは man ioctl_listで。

fcntl(2)

  • ioctlからfdに関する操作だけでも分離しようと作られたのがfcntl()


備忘録