Linux 系统编程笔记

Linux/Unix常用底层库

unistd.hunix standard 
stdlib.hstandard library
fcntl.hfile control
sys.hsystem
stdio.hstandard input/output
dirent.hdirectory entries
string.hstring operation
errno.hsystem error numbers
pwd.hpassword structure
sys/resource.hdefinitions for XSI resource operations
limits.himplementation-dependent constants
termios.hdefine 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。

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 Stones

 

转载于:https://my.oschina.net/u/2438417/blog/729394

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值