当前位置: 首页 > news >正文

Linux系统编程 day2

系统调用

由操作系统实现并提供给外部应用程序的编程接口(API)。是应用程序同系统之间数据交换的桥梁。

文件IO

函数

open/close函数

头文件
#include<fcntl.h>
#include<unistd.h>int open(const char*pathname , int flags)
int open(const char*pathname , int flags , mode_t mode)#mode 执行权限 777 0等
int create(const char *pathname , mode_t mode)flags: 文件打开方式
O_RDONLY | O_WRONLY | O_RDWR    O_CREAT | O_APPEND | O_TRUNC | O_EXCL |O_NONBLOCK

创建文件的权限还受umask影响

错误处理函数:  与error相关

printf("xxx error:%d\n",error);
char* strerror(int errnum);
print("xxx error:%s\n",strerror(errno))

read函数

ssize_t read(int fd , void *buf , size_t count);fd: 文件描述符
buf: 存数据的缓冲区
count: 缓冲区的大小
返回值:成功:读到的字节数   返回0的时候到达文件结尾
失败 -1 设置errno
-1 , 并且errno=EAGIN or EWOULDBLOCK 说明不是读写失败而是read在以非阻塞方式读一个设备或网络文件,并且文件无数据。

write函数(同上)

ssize_t write(int fd , const void *buf , size_t count);
buf: 待写出数据的缓冲区
count: 数据的大小返回值: 成功 写入的字节数
失败 -1 设置errno

写一个自己的cp函数 , 写系统函数调用的时候都要加上调试信息 也就是 判断是否会出错。


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<fcntl.h>int main(int argc , char *argv[])
{char buf[1024];int fd1 = open(argv[1] , O_RDONLY); // readif(fd1 == -1){perror("open argv1 error");exit(1);}int fd2 = open(argv[2],O_RDWR | O_CREAT | O_TRUNC , 0664);if(fd2 == -1){perror("open argv2 error");exit(1);}int n = 0 ;while((n = read(fd1 , buf , 1024))!= 0 ){if(n < 0 ){perror("read error");break;}write(fd2 , buf , n);}close(fd1);close(fd2);return 0 ;
}

 优先使用库函数。

 文件描述符

PCB:进程控制块 ,本质是结构体

成员: 文件描述表

文件描述符:0、1、2、、1023  表中可用最小的

0--STDIN_FILENO

1--STDOUT_FILENO

2--STDER_FILENO

阻塞和非阻塞 

产生阻塞的场景:读设备文件 读网络文件(读常规文件无阻塞概念)

是设备网络文件的属性 

/dev/tty  --终端文件。  

fcntl函数

改变一个已经打开文件的访问控制属性 

int flgs = fcntl(fd , F_GETFL);
flgs |= O_NONBLOCK
fcntl(fd , F_SETFL , flgs); //设置为非阻塞状态
获取文件状态:F_GETFL
设置文件状态:F_SETFL

lseek函数

off_t lseek(int fd , off_t offset , int whence);fd: 文件描述符
offset: 偏移量
whence: 起始偏移位置: SEEK_SET/CUR/END
返回值:  成功:较起始位置偏移量-1

文件的读和写使用同一偏移位置

应用场景:文件的读写使用同一偏移位置

                  使用lseek获取、拓展文件大小:要想使文件真正拓展,必须要引起文件IO操作。

还可以使用truncate函数直接拓展 int ret = truncate("文件名", len);

ret = 0 拓展成功。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<fcntl.h>int main(int argc , char *argv[])
{int fd = open(argv[1] , O_RDWR);if(fd == -1){perror("open error");exit(1);}int len = lseek(fd , 0 , SEEK_END);// int len = lseek(fd , 111 , SEEK_END); // 拓展文件大小printf("file size : %d\n" , len);// write(fd , "a" , 1);close(fd);return 0 ;
}

理解就是将文件指针放到文件末尾,然后设置偏移量为0,返回的就是文件起始位置到文件指针的距离。

传入参数:

        1:指针作为函数参数
        2:通常带有const关键字修饰
        3:指针指向有效区域,在函数内部做读操作
传出参数:

        1:指针作为函数参数
        2:在函数调用之前,指针指向的空间可以无意义,但必须有效
        3:在函数内部,做写操作
        4:函数调用结束后,充当函数返回值
传入传出参数:

        1:指针作为函数参数
        2:在函数调用之前,指针指向的空间要有实际意义
        3:在函数内部,先做读操作,后做写操作
        4:函数调用结束后,充当函数返回值

文件系统

inode

其本质为结构体,存储文件的属性信息。

dentry

目录项,其本质也是结构体 包含文件名和inode

 stat函数/lstat函数

int stat(const char *path , struct stat *buf)
成功返回0
失败返回-1  设置errno

获取文件属性,从inode中获取 , 文件属性将通过传出参数struct stat *buf返回给调用者。

获取文件大小 要包含头文件#include<sys/stat.h>  

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/stat.h>int main(int agrc , char* argv[])
{struct stat sbuf ; int ret = stat(argv[1] , &sbuf);if(ret == -1){perror("stat error");exit(1);}printf("file size:%ld\n" , sbuf.st_size);return 0;
}

区别:stat会穿透符号链接,也就是软连接 ,输出的是原始,而不是输出符号链接, lstat不会

link函数/unlink函数

可以实现mv功能

unlink函数特征:并不是调完删除文件就没了,而是要等到所有打开该文件的进程关闭该文件,系统才会挑时间释放。

文件、目录权限

实现ls

DIR* opendir(char *name):

int closedir(DIR* dp)

struct dirent *readdir(DIR* dp)

struct dirent{五种主要记住两种  inode,char name[255]};


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<dirent.h>int main(int argc , char *argv[])
{DIR *dp;struct dirent *sdp;dp = opendir(argv[1]);if(dp==NULL){perror("opendir error");exit(1);}while((sdp = readdir(dp))!=NULL){printf("%s\t" , sdp->d_name);};printf("\n");closedir(dp);return 0 ;
}

相关文章:

  • 4月15日星期二今日早报简报微语报早读
  • Etcd 推荐配置(生产环境)
  • 路由重定向:redirect
  • PowerBI 度量值分组文件夹管理
  • Python之机器学习入门
  • QuickAPI 核心功能解析:Web 化数据库管理工具的革新与实践
  • ubuntu上SSH防止暴力破解帐号密码
  • Linux网络协议之SSH
  • 【AI提示词】业务开发经理
  • 660 中值定理
  • 黑神话悟空像素版 中文单机版
  • GPIO _OUTPUT-NORMAL 模式
  • Ubuntu和Debian 操作系统的同与异
  • Vue 高级技巧深度解析
  • 【星闪模组开发板WS8204SLEBLEModule】星闪数据收发测试
  • 信息系统项目管理师-工具名词解释(下)
  • STM32 TDS+温度补偿
  • MySQL——存储
  • Redis 分布式锁+秒杀异步优化
  • android11 DevicePolicyManager浅析
  • 明查|美军“杜鲁门”号航空母舰遭胡塞武装打击已退役?
  • 人民日报读者点题·共同关注:今天,我们需要什么样的企业家?
  • 秦洪看盘|A股缩量窄幅震荡,短线或延续有韧性、无弹性走势
  • “不可见社会”:一周城市生活
  • 马上评丨敦煌网美国爆火,“市场之腿”总能跨越关税壁垒
  • 外交部介绍中印尼“2+2”机制首次部长级会议将讨论的议题