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

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

[root@VM_0_5_centos test2]# more openmax.c 
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
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 <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>

int main(int argc,char *argv[]){
        if(argc!=2){
                printf("./a.out filename\n");
}
        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值