UNIXシステムコール

システムコールとはLinuxやUnixが提供する機能をC言語から使うためのAPI関数です。その一覧と使い方をご紹介します。

システムコール一覧

Unixのシステムコール一覧
システムコール 説明
bind ソケットをバインドする。
close ファイルディスクリプタを閉じる。
creat ファイルを作成する。
dup ファイルディスクリプタを複製する。
execve プログラムを実行する。
fork 子プロセスを作成する。
stat パス名で指定したファイルの状態を取得する。指定したファイルがシンボリックリンクの場合は、リンクが参照しているファイルの状態を返す。
fcntl ファイルディスクリプタの操作を行う。
fstat ファイルディスクリプタで指定したファイルの状態を取得する。
lstat パス名で指定したファイルの状態を取得する。指定したファイルがシンボリックリンクの場合は、リンクが参照しているファイルではなく、シンボリックリンク自身の状態を返す。
open ファイルを開く。
openat ファイルを開く。
poll ファイルディスクリプタ上の何らかのイベントを待つ。
read ファイルディスクリプタから読み出す。
socket 通信のエンドポイントを作成する。
write ファイルディスクリプタへ書き込む。

close

closeはファイルディスクリプタを閉じるシステムコールである。

#include <unistd.h>

int close(int fd);

引数

fd
クローズするファイルディスクリプタ

戻り値

ファイルディスクリプタのクローズに成功すると、openは0を返す。クローズに失敗すると-1を返し、errnoにエラー番号がセットされる。

エラー

EBADF
fd は有効なオープンファイル記述子ではありません。
EINTR
close() の呼び出しがシグナルによって中断されました。
EIO
I/Oエラーが発生しました。
ENOSPC, EDQUOT
NFSでは、これらのエラーは、通常、利用可能な書き込みを超えた最初の書き込みに対して報告されません。ストレージスペースではなく、その後の write(2), fsync(2), close() に対して行われます。

fork

forkは子プロセスを作成するシステムコールである。

#include <sys/types.h>
#include <unistd.h>

pid_t fork(void);

fstat

fstatはファイルの状態を取得するシステムコールである。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int fstat(int fd, struct stat *statbuf);
fd
状態を取得するファイルディスクリプタ
statbuf
stat構造体を返す。stat構造体は以下のフィールドを含んでいる。
struct stat {
    dev_t     st_dev;         /* ID of device containing file */
    ino_t     st_ino;         /* Inode number */
    mode_t    st_mode;        /* File type and mode */
    nlink_t   st_nlink;       /* Number of hard links */
    uid_t     st_uid;         /* User ID of owner */
    gid_t     st_gid;         /* Group ID of owner */
    dev_t     st_rdev;        /* Device ID (if special file) */
    off_t     st_size;        /* Total size, in bytes */
    blksize_t st_blksize;     /* Block size for filesystem I/O */
    blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

    /* Since Linux 2.6, the kernel supports nanosecond
       precision for the following timestamp fields.
       For the details before Linux 2.6, see NOTES. */

    struct timespec st_atim;  /* Time of last access */
    struct timespec st_mtim;  /* Time of last modification */
    struct timespec st_ctim;  /* Time of last status change */

#define st_atime st_atim.tv_sec      /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};

ファイルディスクリプタで指定する以外は、statと同じである。

lstat

lstatはファイルの状態を取得するシステムコールである。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int lstat(const char *pathname, struct stat *statbuf);
pathname
状態を取得するファイルをパス名で指定する。
statbuf
stat構造体を返す。stat構造体は以下のフィールドを含んでいる。
struct stat {
    dev_t     st_dev;         /* ID of device containing file */
    ino_t     st_ino;         /* Inode number */
    mode_t    st_mode;        /* File type and mode */
    nlink_t   st_nlink;       /* Number of hard links */
    uid_t     st_uid;         /* User ID of owner */
    gid_t     st_gid;         /* Group ID of owner */
    dev_t     st_rdev;        /* Device ID (if special file) */
    off_t     st_size;        /* Total size, in bytes */
    blksize_t st_blksize;     /* Block size for filesystem I/O */
    blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

    /* Since Linux 2.6, the kernel supports nanosecond
       precision for the following timestamp fields.
       For the details before Linux 2.6, see NOTES. */

    struct timespec st_atim;  /* Time of last access */
    struct timespec st_mtim;  /* Time of last modification */
    struct timespec st_ctim;  /* Time of last status change */

#define st_atime st_atim.tv_sec      /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};

open

openはファイルを開くシステムコールである。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
pathname
オープンするファイルのパス名
flags
フラグ
フラグ 説明
O_APPEND アペンド(追加)モードでファイルを開く。
O_CREAT もしpathnameで指定したファイルが存在しなければ、ファイルを作成する。
O_DIRECTORY もしpathnameで指定したパスがディレクトリでなければ、ファイルのオープンに失敗する。
mode
ファイルモードを指定する。

戻り値

ファイルのオープンに成功した場合、ファイル記述子を返す。ファイルのオープンに失敗した場合、-1 を返す。

エラー

EACCES
要求されたファイルへのアクセスが許可されていない。

read

readはファイルディスクリプタから読み出すシステムコールである。

#include <unistd.h>

ssize_t read(int fd, void *buf, size_t count);
fd
読み出すファイルディスクリプタ
buf
バッファ
count
読み出すサイズ

stat

statはファイルの状態を取得するシステムコールである。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *pathname, struct stat *statbuf);
pathname
状態を取得するファイルのパス名
statbuf
取得した状態を格納するstat構造体のポインタを指定する。stat構造体は以下のフィールドを含んでいる。
struct stat {
    dev_t     st_dev;         /* ID of device containing file */
    ino_t     st_ino;         /* Inode number */
    mode_t    st_mode;        /* File type and mode */
    nlink_t   st_nlink;       /* Number of hard links */
    uid_t     st_uid;         /* User ID of owner */
    gid_t     st_gid;         /* Group ID of owner */
    dev_t     st_rdev;        /* Device ID (if special file) */
    off_t     st_size;        /* Total size, in bytes */
    blksize_t st_blksize;     /* Block size for filesystem I/O */
    blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

    /* Since Linux 2.6, the kernel supports nanosecond
       precision for the following timestamp fields.
       For the details before Linux 2.6, see NOTES. */

    struct timespec st_atim;  /* Time of last access */
    struct timespec st_mtim;  /* Time of last modification */
    struct timespec st_ctim;  /* Time of last status change */

#define st_atime st_atim.tv_sec      /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};

write

writeはファイルディスクリプタへ書き込むシステムコールである。

#include <unistd.h>

ssize_t write(int fd, const void *buf, size_t count);
fd
書き込むファイルディスクリプタ
buf
バッファ
count
書き込むサイズ

エラー番号

システムコールを呼び出した際にエラーが発生すると、外部変数 errno にエラー番号が格納される。

同じ意味のエラーでも、プラットフォームによりエラー番号が異なることがある。

標準Cライブラリーのperror関数で、エラー番号に対応するシステムエラーメッセージを標準出力に出力することができる。

EPERM

Operation not permitted

操作は許可されていない。

EPERMのエラー番号
プラットフォームエラー番号
Solaris1
Tru64 UNIX1
Microsoft Windows1
Ubuntu1

ENOENT

No such file or directory

ファイルまたはディレクトリが存在しません。

ENOENTのエラー番号
プラットフォームエラー番号
Solaris2
Tru64 UNIX2
Microsoft Windows2

ESRCH

No such process

プロセス、LWP、またはスレッドが存在しません。

ESRCHのエラー番号
プラットフォームエラー番号
Solaris3
Tru64 UNIX3
Microsoft Windows3

EINTR

Interrupted system call

システムコールに割り込みが発生しました。

EINTRのエラー番号
プラットフォームエラー番号
Solaris4
Tru64 UNIX4
Microsoft Windows4

EIO

I/O error

EIOのエラー番号
プラットフォームエラー番号
Solaris5
Tru64 UNIX5
Microsoft Windows5

ENXIO

No such device or address

ENXIOのエラー番号
プラットフォームエラー番号
Solaris6
Tru64 UNIX6
Microsoft Windows6

E2BIG

Arg list too long

E2BIGのエラー番号
プラットフォームエラー番号
Solaris7
Tru64 UNIX7
Microsoft Windows7

ENOEXEC

Exec format error

ENOEXEC のエラー番号
プラットフォームエラー番号
Solaris8
Tru64 UNIX8
Microsoft Windows8

EBADF

Bad file number

EBADF のエラー番号
プラットフォームエラー番号
Solaris9
Tru64 UNIX9
Microsoft Windows9

ECHILD

No child processes

ECHILD のエラー番号
プラットフォームエラー番号
Solaris10
Tru64 UNIX10
Microsoft Windows10

EAGAIN / EWOULDBLOCK

Resource temporarily unavailable

EAGAIN/EWOULDBLOCK のエラー番号
プラットフォームエラー番号
Solaris11
Tru64 UNIX35
Microsoft Windows11

ENOMEM

Not enough space

ENOMEM のエラー番号
プラットフォームエラー番号
Solaris12
Tru64 UNIX12
Microsoft Windows12

EACCES

Permission denied

EACCES のエラー番号
プラットフォームエラー番号
Solaris13
Tru64 UNIX13
Microsoft Windows13

EFAULT

Bad address

EFAULT のエラー番号
プラットフォームエラー番号
Solaris14
Tru64 UNIX14
Microsoft Windows14

ENOTBLK

Block device required

ENOTBLK のエラー番号
プラットフォームエラー番号
Solaris15
Tru64 UNIX15
Microsoft Windows-

EBUSY

Device busy

EBUSY のエラー番号
プラットフォームエラー番号
Solaris16
Tru64 UNIX16
Microsoft Windows16

EEXIST

File exists

EEXIST のエラー番号
プラットフォームエラー番号
Solaris17
Tru64 UNIX17
Microsoft Windows17

EXDEV

Cross-device link

EXDEV のエラー番号
プラットフォームエラー番号
Solaris18
Tru64 UNIX18
Microsoft Windows18

ENODEV

No such device

ENODEV のエラー番号
プラットフォームエラー番号
Solaris19
Tru64 UNIX19
Microsoft Windows19

ENOTDIR

Not a directory

ENOTDIR のエラー番号
プラットフォームエラー番号
Solaris20
Tru64 UNIX20
Microsoft Windows20

EISDIR

Is a directory

EISDIR のエラー番号
プラットフォームエラー番号
Solaris21
Tru64 UNIX21
Microsoft Windows21

EINVAL

Invalid argument

不正な引数です。

EINVALのエラー番号
プラットフォームエラー番号
Solaris22
Tru64 UNIX22
Microsoft Windows22

ENFILE

File table overflow

ENFILE のエラー番号
プラットフォームエラー番号
Solaris23
Tru64 UNIX23
Microsoft Windows23

EMFILE

Too many open files

EMFILE のエラー番号
プラットフォームエラー番号
Solaris24
Tru64 UNIX24
Microsoft Windows24

ENOTTY

Inappropriate ioctl for device

ENOTTY のエラー番号
プラットフォームエラー番号
Solaris25
Tru64 UNIX25
Microsoft Windows25

ETXTBSY

Text file busy

ETXTBSY のエラー番号
プラットフォームエラー番号
Solaris26
Tru64 UNIX26
Microsoft Windows-

EFBIG

File too large

EFBIG のエラー番号
プラットフォームエラー番号
Solaris27
Tru64 UNIX27
Microsoft Windows27

ENOSPC

No space left on device

デバイスに空き領域がありません。

ENOSPCのエラー番号
プラットフォームエラー番号
Solaris28
Tru64 UNIX28
Microsoft Windows28

write(2)システムコールは、直接ディスクに書き込むわけではありません。書き込みをスケジュールするだけです。ディスクの空き領域が足りない場合でも、ENOSPCでエラーになるとは限りません。書き込みに成功したかどうかは、fsync(2)を呼び出したうえ、close(2)の戻り値を確認する必要があります。

ESPIPE

Illegal seek

ESPIPE のエラー番号
プラットフォームエラー番号
Solaris29
Tru64 UNIX29
Microsoft Windows29

EROFS

Read-only file system

EROFS のエラー番号
プラットフォームエラー番号
Solaris30
Tru64 UNIX30
Microsoft Windows30
EMLINK のエラー番号
プラットフォームエラー番号
Solaris31
Tru64 UNIX31
Microsoft Windows31

EPIPE

EPIPE のエラー番号
プラットフォームエラー番号
Solaris32
Tru64 UNIX32
Microsoft Windows32

EDOM

EDOM のエラー番号
プラットフォームエラー番号
Solaris33
Tru64 UNIX33
Microsoft Windows33

ERANGE

ERANGE のエラー番号
プラットフォームエラー番号
Solaris34
Tru64 UNIX34
Microsoft Windows34

ENOMSG

ENOMSGのエラー番号
プラットフォームエラー番号
Solaris35
Tru64 UNIX80
Microsoft Windows-

EIDRM

EIDRMのエラー番号
プラットフォームエラー番号
Solaris36
Tru64 UNIX81
Microsoft Windows-

ECHRNG

ECHRNG のエラー番号
プラットフォームエラー番号
Solaris37
Tru64 UNIX-
Microsoft Windows-

EL2NSYNC

EL2NSYNC のエラー番号
プラットフォームエラー番号
Solaris38
Tru64 UNIX-

EL3HLT

EL3HLT のエラー番号
プラットフォームエラー番号
Solaris39
Tru64 UNIX-

EL3RST

EL3RST のエラー番号
プラットフォームエラー番号
Solaris40
Tru64 UNIX-

ELNRNG

ELNRNG のエラー番号
プラットフォームエラー番号
Solaris41
Tru64 UNIX-

EUNATCH

EUNATCH のエラー番号
プラットフォームエラー番号
Solaris42
Tru64 UNIX-

ENOCSI

ENOCSI のエラー番号
プラットフォームエラー番号
Solaris43
Tru64 UNIX-

EL2HLT

EL2HLT のエラー番号
プラットフォームエラー番号
Solaris44
Tru64 UNIX-

EDEADLK

EDEADLK のエラー番号
プラットフォームエラー番号
Solaris45
Tru64 UNIX11
Microsoft Windows36

ENOLCK

ENOLCK のエラー番号
プラットフォームエラー番号
Solaris46
Tru64 UNIX77
Microsoft Windows39

ECANCELED

ECANCELED のエラー番号
プラットフォームエラー番号
Solaris47
Tru64 UNIX94

ENOTSUP

ENOTSUP のエラー番号
プラットフォームエラー番号
Solaris48
Tru64 UNIX99

EDQUOT

EDQUOT のエラー番号
プラットフォームエラー番号
Solaris49
Tru64 UNIX69

EBADE

EBADE のエラー番号
プラットフォームエラー番号
Solaris50
Tru64 UNIX-