********** 文件I/O ********** open() #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open (const char *name, int flags); int open (const char *name, int flags, mode_t mode); creat() #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int creat (const char *name, mode_t mode); read() #include <unistd.h> ssize_t read (int fd, void *buf, size_t len); write() #include <unistd.h> ssize_t write (int fd, const void *buf, size_t count); fsync() fdatasync() #include <unistd.h> int fsync (int fd); int fdatasync (int fd); sync() #include <unistd.h> void sync (void); close() #include <unistd.h> int close (int fd); lseek() #include <sys/types.h> #include <unistd.h> off_t lseek (int fd, off_t pos, int origin); pread() pwrite() #define _XOPEN_SOURCE 500 #include <unistd.h> ssize_t pread (int fd, void *buf, size_t count, off_t pos); ssize_t pwrite (int fd, const void *buf, size_t count, off_t pos); ftruncate() truncate() #include <unistd.h> #include <sys/types.h> int ftruncate (int fd, off_t len); int truncate (const char *path, off_t len); select() #include <sys/time.h> #include <sys/types.h> #include <unistd.h> int select ( int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); FD_CLR(int fd, fd_set *set); FD_ISSET(int fd, fd_set *set); FD_SET(int fd, fd_set *set); FD_ZERO(fd_set *set); #include <sys/time.h> struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ }; pselect() #define _XOPEN_SOURCE 600 #include <sys/select.h> int pselect ( int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout, const sigset_t *sigmask); FD_CLR(int fd, fd_set *set); FD_ISSET(int fd, fd_set *set); FD_SET(int fd, fd_set *set); FD_ZERO(fd_set *set); #include <sys/time.h> struct timespec { long tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ }; poll() #include <sys/poll.h> int poll (struct pollfd *fds, unsigned int nfds, int timeout); #include <sys/poll.h> struct pollfd { int fd; /* file descriptor */ short events; /* requested events to watch */ short revents; /* returned events witnessed */ }; ppoll() #define _GNU_SOURCE #include <sys/poll.h> int ppoll ( struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, const sigset_t *sigmask); ? 标准I/O ? 自创用户缓冲 ? 直接系统调用 ********** 标准I/O ********** fopen() #include <stdio.h> FILE* fopen(const char * path, const char * mode); fdopen() #include <stdio.h> FILE * fdopen (int fd, const char *mode); fclose() #include <stdio.h> int fclose (FILE *stream); fcloseall() #define _GNU_SOURCE #include <stdio.h> int fcloseall (void); fgetc() ungetc() #include <stdio.h> int fgetc (FILE *stream); int ungetc (int c, FILE *stream); fgets() #include <stdio.h> char * fgets (char *str, int size, FILE *stream); fread() #include <stdio.h> size_t fread (void *buf, size_t size, size_t nr, FILE *stream); fputc() #include <stdio.h> int fputc (int c, FILE *stream); fputs() #include <stdio.h> int fputs (const char *str, FILE *stream); fwrite() #include <stdio.h> size_t fwrite (void *buf, size_t size, size_t nr, FILE *stream); fseek() #include <stdio.h> int fseek (FILE *stream, long offset, int whence); fsetpos() #include <stdio.h> int fsetpos (FILE *stream, fpos_t *pos); fgetpos() #include <stdioh.h> int fgetpos (FILE *stream, fpos_t *pos); rewind() #include <stdio.h> void rewind (FILE *stream); ftell() #include <stdio.h> long ftell (FILE *stream); fflush() #include <stdio.h> int fflush (FILE *stream); ? 用户空间缓冲区 ? 内核空间缓冲区 ? ferror() #include <stdio.h> int ferror (FILE *stream); feof() #include <stdio.h> int feof (FILE *stream); clearerr() #include <stdio.h> void clearerr (FILE *stream); fileno() #include <stdio.h> int fileno (FILE *stream); setvbuf() #include <stdio.h> int setvbuf (FILE *stream, char *buf, int mode, size_t size); ********** 锁机制 ********** flockfile() funlockfile() #include <stdio.h> void flockfile (FILE *stream); void funlockfile (FILE *stream); ftrylockfile() #include <stdio.h> int ftrylockfile (FILE *stream); unlock 标准I/O #define _GNU_SOURCE #include <stdio.h> int fgetc_unlocked (FILE *stream); char *fgets_unlocked (char *str, int size, FILE *stream); size_t fread_unlocked (void *buf, size_t size, size_t nr,FILE *stream); int fputc_unlocked (int c, FILE *stream); int fputs_unlocked(const char *str, FILE *stream); size_t fwrite_unlocked (void *buf, size_t size, size_t nr, FILE *stream); int fflush_unlocked (FILE *stream); int feof_unlocked (FILE *stream); int ferror_unlocked (FILE *stream); int fileno_unlocked (FILE *stream); void clearerr_unlocked (FILE *stream); ********** 高级文件I/O ********** readv() #include <sys/uio.h> ssize_t readv (int fd, const struct iovec *iov, int count); writev() #include <sys/uio.h> ssize_t writev (int fd, const struct iovec *iov, int count); #include <sys/uio.h> struct iovec { void *iov_base; size_t iov_len; }; epoll_create() #include <sys/epoll.h> int epoll_create (int size) epoll_ctl() #include <sys/epoll.h> int epoll_ctl (int epfd, int op, int fd, struct epoll_event *event); struct epoll_event { __u32 events; /* events */ union { void *ptr; int fd; __u32 u32; __u64 u64; } data; }; #include <sys/epoll.h> epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout) #include <sys/mman.h> void * mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset) int munmap (void *addr, size_t len); //POSIX #include <unistd.h> long sysconf (int name); //LINUX #include <unistd.h> int getpagesize (void); //宏 <asm/pages.h> PAGE_SIZE #define _GNU_SOURCE #include <unistd.h> #include <sys/mman.h> void * mremap (void *addr, size_t old_size, size_t new_size, unsigned long flags); #include <sys/mman.h> int mprotect (const void *addr, size_t len, int prot); #include <sys/mman.h> int msync (void *addr, size_t len, int flags); #include <sys/mman.h> int madvise (void *addr, size_t len, int advice); #include <fcntl.h> int posix_fadvise (int fd, off_t offset, off_t len, int advice); #include <fcntl.h> ssize_t readahead (int fd, off64_t offset, size_t count); #include <aio.h> /* asynchronous I/O control block */ struct aiocb { int aio_filedes; /* file descriptor */ int aio_lio_opcode; /* operation to perform */ int aio_reqprio; /* request priority offset */ volatile void *aio_buf; /* pointer to buffer */ size_t aio_nbytes; /* length of operation */ struct sigevent aio_sigevent; /* signal number and value */ /* internal, private members follow... */ }; int aio_read (struct aiocb *aiocbp); int aio_write (struct aiocb *aiocbp); int aio_error (const struct aiocb *aiocbp); int aio_return (struct aiocb *aiocbp); int aio_cancel (int fd, struct aiocb *aiocbp); int aio_fsync (int op, struct aiocb *aiocbp); int aio_suspend (const struct aiocb * const cblist[], int n, const struct timespec *timeout); ********** 文件与目录管理 ************ #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int stat (const char *path, struct stat *buf); int fstat (int fd, struct stat *buf); int lstat (const char *path, struct stat *buf); struct stat { dev_t st_dev;/* ID of device containing file */ ino_t st_ino;/* inode number */ mode_t st_mode;/* permissions */ 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; /* blocksize for filesystem I/O */ blkcnt_t st_blocks; /* number of blocks allocated */ time_t st_atime;/* last access time */ time_t st_mtime;/* last modification time */ time_t st_ctime;/* last status change time */ }; #include <sys/types.h> #include <sys/stat.h> int chmod (const char *path, mode_t mode); int fchmod (int fd, mode_t mode); #include <sys/types.h> #include <unistd.h> int chown (const char *path, uid_t owner, gid_t group); int lchown (const char *path, uid_t owner, gid_t group); int fchown (int fd, uid_t owner, gid_t group); #include <sys/types.h> #include <attr/xattr.h> ssize_t getxattr (const char *path, const char *key, void *value, size_t size); ssize_t lgetxattr (const char *path, const char *key, void *value, size_t size); ssize_t fgetxattr (int fd, const char *key, void *value, size_t size); #include <sys/types.h> #include <attr/xattr.h> int setxattr (const char *path, const char *key, const void *value, size_t size, int flags); int lsetxattr (const char *path, const char *key, const void *value, size_t size, int flags); int fsetxattr (int fd, const char *key, const void *value, size_t size, int flags); #include <sys/types.h> #include <attr/xattr.h> ssize_t listxattr (const char *path, char *list, size_t size); ssize_t llistxattr (const char *path, char *list, size_t size); ssize_t flistxattr (int fd, char *list, size_t size); #include <sys/types.h> #include <attr/xattr.h> int removexattr (const char *path, const char *key); int lremovexattr (const char *path, const char *key); int fremovexattr (int fd, const char *key); #include <unistd.h> char * getcwd (char *buf, size_t size); #include <unistd.h> int chdir (const char *path); int fchdir (int fd); #include <sys/stat.h> #include <sys/types.h> int mkdir (const char *path, mode_t mode); #include <unistd.h> int rmdir (const char *path); #include <sys/types.h> #include <dirent.h> DIR * opendir (const char *name); #include <sys/types.h> #include <dirent.h> struct dirent * readdir (DIR *dir); #include <sys/types.h> #include <dirent.h> int closedir (DIR *dir); #include <unistd.h> #include <linux/types.h> #include <linux/dirent.h> #include <linux/unistd.h> #include <errno.h> /* * Not defined for user space: need to * use the _syscall3( ) macro to access. */ int readdir (unsigned int fd, struct dirent *dirp, unsigned int count); int getdents (unsigned int fd, struct dirent *dirp, unsigned int count); #include <unistd.h> int link (const char *oldpath, const char *newpath); #include <unistd.h> int symlink (const char *oldpath, const char *newpath); #include <unistd.h> int unlink (const char *pathname); #include <stdio.h> int rename (const char *oldpath, const char *newpath); #include <sys/ioctl.h> int ioctl (int fd, int request, ...); #include <inotify.h> int inotify_init (void); #include <inotify.h> int inotify_add_watch (int fd, const char *path, int32_t mask); #include <inotify.h> struct inotify_event { int wd; /* watch descriptor */ uint32_t mask; /* mask of events */ uint32_t cookie; /* unique cookie */ uint32_t len; /* size of ’name’ field */ char name[]; /* null-terminated name */ }; ********** 进程管理 ********** #include <sys/types.h> #include <unistd.h> pid_t getpid (void); pid_t getppid (void); exec #include <unistd.h> int execl (const char *path, const char *arg, ...); int execlp (const char *file, const char *arg, ...); int execle (const char *path, const char *arg, ..., char * const envp[]); int execv (const char *path, char *const argv[]); int execvp (const char *file, char *const argv[]); int execve (const char *filename, char *const argv[], char *const envp[]); fork #include <sys/types.h> #include <unistd.h> pid_t fork (void); pid_t vfork (void); #include <stdlib.h> void exit (int status); #include <unistd.h> void _exit (int status); #include <stdlib.h> void _Exit (int status); #include <stdlib.h> int atexit (void (*function)(void)); #include <stdlib.h> int on_exit (void (*function)(int , void *), void *arg); #include <sys/types.h> #include <sys/wait.h> pid_t wait (int *status); pid_t waitpid (pid_t pid, int *status, int options); #include <sys/wait.h> int waitid (idtype_t idtype, id_t id, siginfo_t *infop, int options); #include <sys/types.h> #include <sys/time.h> #include <sys/resource.h> #include <sys/wait.h> pid_t wait3 (int *status, int options, struct rusage *rusage); pid_t wait4 (pid_t pid, int *status, int options, struct rusage *rusage); #define _XOPEN_SOURCE /* if we want WEXITSTATUS, etc. */ #include <stdlib.h> int system (const char *command); ********** 内存管理 ********** #include <stdlib.h> void *malloc (size_t size); #include <stdlib.h> void *calloc (size_t nr, size_t size); #include <stdlib.h> void *realloc (void *ptr, size_t size); #include <stdlib.h> void free (void *ptr); /* one or the other -- either suffices */ #define _XOPEN_SOURCE 600 #define _GNU_SOURCE #include <stdlib.h> int posix_memalign (void **memptr, size_t alignment, size_t size); #include <unistd.h> int brk (void *end); void * sbrk (intptr_t increment); #include <sys/mman.h> void * mmap (void *start, size_t length, int prot, int flags, int fd, off_t offset); int munmap (void *start, size_t length); #include <malloc.h> int mallopt (int param, int value); #include <malloc.h> size_t malloc_usable_size (void *ptr); #include <malloc.h> int malloc_trim (size_t padding); #include <malloc.h> struct mallinfo mallinfo (void); struct mallinfo { int arena; /* 使用的数据段的大小malloc */ int ordblks; /* 空闲块的个数 */ int smblks; /* fast bin 的个数 */ int hblks; /* 匿名映射的个数 */ int hblkhd; /* 匿名映射的大小 */ int usmblks; /* 最大已分配值 */ int fsmblks; /* 可用的fast的大小bin */ int uordblks; /* 所有的被分配的空间*/ int fordblks; /* 可用的块大小 */ int keepcost; /* 可消去的多与空间的大小 */ }; #include <malloc.h> void malloc_stats (void); #include <alloca.h> void * alloca (size_t size); #define _GNU_SOURCE #include <string.h> char * strdupa (const char *s); char * strndupa (const char *s, size_t n); #include <string.h> void * memset (void *s, int c, size_t n); #include <strings.h> void bzero (void *s, size_t n); #include <string.h> int memcmp (const void *s1, const void *s2, size_t n); #include <string.h> void * memmove (void *dst, const void *src, size_t n); #include <string.h> void * memcpy (void *dst, const void *src, size_t n); #include <string.h> void * memccpy (void *dst, const void *src, int c, size_t n); #define _GNU_SOURCE #include <string.h> void * mempcpy (void *dst, const void *src, size_t n); #include <string.h> void * memchr (const void *s, int c, size_t n); #define _GNU_SOURCE #include <string.h> void * memrchr (const void *s, int c, size_t n); #define _GNU_SOURCE #include <string.h> void * memmem (const void *haystack, size_t haystacklen, const void *needle, size_t needlelen); #define _GNU_SOURCE #include <string.h> void * memfrob (void *s, size_t n); #include <sys/mman.h> int mlock (const void *addr, size_t len); #include <sys/mman.h> int mlock (const void *addr, size_t len); #include <sys/mman.h> int mlockall (int flags); #include <sys/mman.h> int munlock (const void *addr, size_t len); int munlockall (void); #include <unistd.h> #include <sys/mman.h> int mincore (void *start, size_t length, unsigned char *vec);