システムコールとはLinuxやUnixが提供する機能をC言語から使うためのAPI関数です。その一覧と使い方をご紹介します。
| システムコール | 説明 | 
|---|---|
| bind | ソケットをバインドする。 | 
| close | ファイルディスクリプタを閉じる。 | 
| creat | ファイルを作成する。 | 
| dup | ファイルディスクリプタを複製する。 | 
| execve | プログラムを実行する。 | 
| fork | 子プロセスを作成する。 | 
| stat | パス名で指定したファイルの状態を取得する。指定したファイルがシンボリックリンクの場合は、リンクが参照しているファイルの状態を返す。 | 
| fcntl | ファイルディスクリプタの操作を行う。 | 
| fstat | ファイルディスクリプタで指定したファイルの状態を取得する。 | 
| lstat | パス名で指定したファイルの状態を取得する。指定したファイルがシンボリックリンクの場合は、リンクが参照しているファイルではなく、シンボリックリンク自身の状態を返す。 | 
| open | ファイルを開く。 | 
| openat | ファイルを開く。 | 
| poll | ファイルディスクリプタ上の何らかのイベントを待つ。 | 
| read | ファイルディスクリプタから読み出す。 | 
| socket | 通信のエンドポイントを作成する。 | 
| write | ファイルディスクリプタへ書き込む。 | 
#include <sys/types.h>
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);#include <sys/types.h>
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);closeはファイルディスクリプタを閉じるシステムコールである。
#include <unistd.h>
int close(int fd);ファイルディスクリプタのクローズに成功すると、openは0を返す。クローズに失敗すると-1を返し、errnoにエラー番号がセットされる。
fd は有効なオープンファイル記述子ではありません。close() の呼び出しがシグナルによって中断されました。#include <sys/types.h>
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);forkは子プロセスを作成するシステムコールである。
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);fstatはファイルの状態を取得するシステムコールである。
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int fstat(int fd, struct stat *statbuf);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と同じである。
プロセスへシグナルを送る。
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, ing sig);#include <sys/types.h>
#include <sys/socket.h>
int listen(int sockfd, int backlog);lstatはファイルの状態を取得するシステムコールである。
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int lstat(const char *pathname, struct stat *statbuf);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
};メモリからファイルやデバイスをアンマップする。
#include <sys/mmah.h>
int munmap(void *addr, size_t length);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);| フラグ | 説明 | 
|---|---|
| O_APPEND | アペンド(追加)モードでファイルを開く。 | 
| O_CREAT | もしpathnameで指定したファイルが存在しなければ、ファイルを作成する。 | 
| O_DIRECTORY | もしpathnameで指定したパスがディレクトリでなければ、ファイルのオープンに失敗する。 | 
ファイルのオープンに成功した場合、ファイル記述子を返す。ファイルのオープンに失敗した場合、-1 を返す。
readはファイルディスクリプタから読み出すシステムコールである。
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);#include <sys/types.h>
#include <sys/socket.h>
ssize_t recv(int sockfd, void *buf, size_t len, int flags);#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);#include <sys/types.h>
#include <sys/socket.h>
ssize_t send(int sockfd, const void *buf, size_t len, int flags);#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);通信の終端を作成して、そのファイル記述子を戻り値として返す。
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);| 値 | 説明 | 
|---|---|
| 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;
}| 値 | 説明 | 
|---|---|
| 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はファイルの状態を取得するシステムコールである。
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *statbuf);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はファイルディスクリプタへ書き込むシステムコールである。
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);