UNIXシステムコール

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

  1. accept
  2. bind
  3. close
  4. connect
  5. creat
  6. dup
  7. execve
  8. fcntl
  9. fork
  10. fstat
  11. listen
  12. lstat
  13. open
  14. openat
  15. poll
  16. read
  17. recv
  18. recvfrom
  19. send
  20. sendto
  21. socket
  22. stat
  23. write

システムコール一覧

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

accept

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

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

bind

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

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

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() に対して行われます。

connect

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

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

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と同じである。

listen

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

int listen(int sockfd, int backlog);

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
読み出すサイズ

recv

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

ssize_t recv(int sockfd, void *buf, size_t len, int flags);

recvfrom

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

ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);

send

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

ssize_t send(int sockfd, const void *buf, size_t len, int flags);

sendto

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

ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);

socket

通信の終端を作成して、そのファイル記述子を戻り値として返す。

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

int socket(int domain, int type, int protocol);
domain
通信のドメインを指定する。
domain
説明
AF_INET IPv4 インターネット・プロトコル
AF_INET6 IPv6 インターネット・プロトコル
int main(int argc, char**argv) {
  int fd;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    perror("socket error");
    return 1;
  }
  close(fd);
  return 0;
}
type
通信セマンティクスを指定する。
type
説明
SOCK_STREAM ストリーム型 (TCP)
SOCK_DGRAM データグラム型 (UDP)
int main(int argc, char **argv) {
  int fd;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0) {
    perror("socket error");
    return 1;
  }
  close(fd);
  return 0;
}

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
書き込むサイズ