Linux 系統編程筆記

Linux/Unix經常使用底層庫

unistd.h unix standard  
stdlib.h standard library
fcntl.h file control
sys.h system
stdio.h standard input/output
dirent.h directory entries
string.h string operation
errno.h system error numbers
pwd.h password structure
sys/resource.h definitions for XSI resource operations
limits.h implementation-dependent constants
termios.h define values for termios 定義POSIX規範中定義的的標準接口termios的數據結構和相關函數

文件操做

底層文件訪問

write()

#include <unistd.h>

size_t write(int fildes, const void *buf, size_t nbytes);

/**
fildes : file descriptor
buf : buffer
nbytes : number of bytes
**/

把緩衝區buf前的nbytes個字節寫入與文件描述符fildes關聯的文件中,並返回實際寫入的字節,不然返回0或者-1。node

read()

#include <unistd.h>

size_t read(int fildes, void *buf, size_t nbytes);

/**
fildes : file descriptor
buf : buffer
nbytes : number of bytes
**/

open()

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

int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);

/**
*path : path of file or device
oflag : flag of action to open the file, detail in below

oflag list:
O_RDONLY : open with read only mode
O_WRONLY : opne with write only mode
O_RDWR : open with read and write mode
O_APPEND : append the content follow the tail of file 
O_TRUNC : make the file content truncated
O_CREAT : create the file if need
O_EXCL : 

mode : mode of invoke open() when arg oflags is O_CREAT, detail in below
S_IRUSR : access to read for file owner
S_IWUSR : access to write for file owner
S_IXUSR : access to execute for file owner
S_IRGRP : access to read for file group
S_IWGRP : access to write for file group
S_IXGRP : access to execute for file group
S_IROTH : access to read for file other user
S_IWOTH : access to write for file other user
S_IXOTH : access to execute for file other user
**/

close()

#include <unistd.h>
int close(int fildes);

/**
fildes : file descriptor
**/

ioctl()

#include <unistd.h>
int ioctl(int fildes, int cmd, ...)

/**
fildes : file descriptor
cmd : command
**/

lseek()

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

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

/**
fildes : file descriptor
offset : the offset position of read/write cursor relate to the cursor of fildes
whence : define the function of offset

whence list:
SEEK_SET : 
SEEK_CUR :
SEEK_END :  
**/

fstat(),stat(),lstat()

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

int fstat(int fildes, struct stat *buf);
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);

/**
fildes : file descriptor
buf : buffer
path : file path

members list in struct stat:
st_mode : file access and mode info 
st_ino : inode relate to file
st_dev : device the file be stored
st_uid : user id of file owner
st_gid : group id of file owner
st_atime : latest access time 
st_ctime : latest file attributes change time
st_mtime : latest content modify time
st_nlink : number of hard link
**/

dup(),dup2()

#include <unistd.h>

int dup(int fildes);
int dup2(int fildes, int fildes2);

/**
fildes : file descriptor
**/

標準I/O庫

fopen()

#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);

/**
filename : file name
mode : mode open file

mode list:
"r" "rb" : read only
"w" "wb" : write only 
"a" "ab" : append
"r+" "rb" "r+b" : refresh with read and write
"w+" "wb" "w+b" : refresh with write
"a+" "ab" "a+b" : refresh with append 
**/

fread()

#include <stdio.h>

size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);

/**
*ptr : pointer which data buffer refer in file stream
size : length every record
nitems : number of transfer record
stream : file stream
**/

數據管理

內存管理

# include <stdlib.h>
void *malloc(size_t size);

# include <stdlib.h>
void free(void *ptr_to_memory);

#include <stdlib.h>
void *calloc(size_t number_of_elements, size_t element_size);
void *realloc(void *existing_memory, size_t new_size);

文件鎖定

#include <fcntl.h>
int fcntl(int fildes, int command, struct flock *flock_structure);

/**
fildes : file description
command
flock_structure : structure of file lock

command list:
F_GETLK : get lock info of fildes
F_SETLK : set lock or unlock the file which fildes refer to
F_SETLKW : wait until getting availability to lock the fildes
**/

參考資料:

Linux程序設計 第4版(Beginning Linux Programming,4th Edition),Neil Matthew & RIchard Stonesios

相關文章
相關標籤/搜索