LINUX命令基础记录七(系统API和库函数:stat、access、truncate、link、symlink、readlink、unlink)

1、系统能同时打开最大的文件数

[root@VM_0_5_centos test2]# more openmax.c 
#include 
#include 
#include 
#include
#include
int main(){
        int num=3;
        char filename[128]={0};
        while(1){
                sprintf(filename,"temp_%04d",num++);
                        if(open(filename,O_RDONLY|O_CREAT,0666) < 0){
                                perror("open err");
                                break;
                        }
        }
        printf("num=%d\n",num);
        return 0;
}

[root@VM_0_5_centos test2]# gcc openmax.c

[root@VM_0_5_centos test2]# ./a.out

open err: Too many open files

num=100002

2、stat函数

获取文件信息

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

   struct stat {

               dev_t     st_dev;     /* ID of device containing file */ 设备编号

               ino_t     st_ino;     /* inode number */inode节点

               mode_t    st_mode;    /* protection */类型与权限

               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 512B blocks allocated */块数

               time_t    st_atime;   /* time of last access */最后一次访问时间

               time_t    st_mtime;   /* time of last modification */最后修改时间

               time_t    st_ctime;   /* time of last status change */最后状态更改时间

           };

[root@VM_0_5_centos linux]# pwd

/usr/src/kernels/3.10.0-514.26.2.el7.x86_64/include/uapi/linux

[root@VM_0_5_centos linux]# more time.h

struct timespec {

        __kernel_time_t tv_sec;   /* seconds */ 当前时间到1970.1.1  0:0:0的秒数

        long            tv_nsec;                /* nanoseconds */纳秒

};

1s=1000ms 毫秒

1ms=1000us 微秒

1us=1000ns 纳秒

stat函数参数

pathname 文件名

struct stat *buf传出参数,定义struct stat sb;

返回值

成功返回0,失败返回-1,设置errno

stat例子

[root@VM_0_5_centos test2]# more stat.c 
#include 
#include 
#include 
#include
#include

int main(int argc,char *argv[]){
        if(argc!=2){
                printf("./a.out filename\n");
}
        

你可能感兴趣的:(C语言,LIUNX)