欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 焦点 > Linux系统编程 day2

Linux系统编程 day2

2025/4/19 16:58:38 来源:https://blog.csdn.net/weixin_71568456/article/details/147232856  浏览:    关键词: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 ;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词