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

本文介绍了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 */纳秒

};

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值