C库和IO函数工作流程
虚拟地址空间
PCB和文件描述符
cpu 为什么要使用虚拟地址空间与物理地址空间映射?解决了什么样的问题?
1.方便编译器和操作系统安排程序的地址分布。
程序可以使用一系列相邻的虚拟地址来访问物理内存中不相邻的大内存缓冲区。
2.方便进程之间隔离
不同进程使用的虚拟地址彼此隔离。一个进程中的代码无法更改正在由另一进程使用的物理内存。
3.方便OS使用你那可怜的内存。
程序可以使用一系列虚拟地址来访问大于可用物理内存的内存缓冲区。当物理内存的供应量变小时,
内存管理器会将物理内存页(通常大小为 4 KB)保存到磁盘文件。数据或代码页会根据需要在物理内存与磁盘之间移动。
库函数与系统函数的关系
open函数
errno
read、write、close
文件拓展有什么用:比如说你在用迅雷下载一个10g的文件,那么在开始下载的时候,你查看电脑上临时创建的那个文件,发现那个文件的大小就已经是10g了(利于多线程下载,多线程下载时可以下载指定的快大小到指定的文件位置)
stat、lstat
穿透和不穿透:比如说现在是有一个软连接,如果用stat去读取这个软连接的大小,实际上读到的是这个软连接引用的文件的大小,而用lstat去读取的话,读取的是这个软连接的大小
mode_t类型
stat函数用法:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("a.out filename\n");
exit(1);
}
struct stat buf_st;
int ret = lstat(argv[1], &buf_st);
if(ret == -1)
{
perror("stat");
exit(1);
}
printf("file size = %d\n", (int)buf_st.st_size);
return 0;
}
用stat函数实现ls -l的功能:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("./a.out filename\n");
exit(1);
}
struct stat st;
int ret = stat(argv[1], &st);
if(ret == -1)
{
perror("stat");
exit(1);
}
// 存储文件类型和访问权限
char perms[11] = {0};
// 判断文件类型
switch(st.st_mode & S_IFMT)
{
case S_IFLNK:
perms[0] = 'l';
break;
case S_IFDIR:
perms[0] = 'd';
break;
case S_IFREG:
perms[0] = '-';
break;
case S_IFBLK:
perms[0] = 'b';
break;
case S_IFCHR:
perms[0] = 'c';
break;
case S_IFSOCK:
perms[0] = 's';
break;
case S_IFIFO:
perms[0] = 'p';
break;
default:
perms[0] = '?';
break;
}
// 判断文件的访问权限
// 文件所有者
perms[1] = (st.st_mode & S_IRUSR) ? 'r' : '-';
perms[2] = (st.st_mode & S_IWUSR) ? 'w' : '-';
perms[3] = (st.st_mode & S_IXUSR) ? 'x' : '-';
// 文件所属组
perms[4] = (st.st_mode & S_IRGRP) ? 'r' : '-';
perms[5] = (st.st_mode & S_IWGRP) ? 'w' : '-';
perms[6] = (st.st_mode & S_IXGRP) ? 'x' : '-';
// 其他人
perms[7] = (st.st_mode & S_IROTH) ? 'r' : '-';
perms[8] = (st.st_mode & S_IWOTH) ? 'w' : '-';
perms[9] = (st.st_mode & S_IXOTH) ? 'x' : '-';
// 硬链接计数
int linkNum = st.st_nlink;
// 文件所有者
char* fileUser = getpwuid(st.st_uid)->pw_name;
// 文件所属组
char* fileGrp = getgrgid(st.st_gid)->gr_name;
// 文件大小
int fileSize = (int)st.st_size;
// 修改时间
char* time = ctime(&st.st_mtime);
char mtime[512] = {0};
strncpy(mtime, time, strlen(time)-1);
char buf[1024];
sprintf(buf, "%s %d %s %s %d %s %s", perms, linkNum, fileUser, fileGrp, fileSize, mtime, argv[1]);
printf("%s\n", buf);
return 0;
}
一些其他函数
access函数使用
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("a.out filename\n");
exit(1);
}
int ret = access(argv[1], W_OK);
if(ret == -1)
{
perror("access");
exit(1);
}
printf("you can write this file.\n");
return 0;
}
chmod函数
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("a.out filename\n");
exit(1);
}
int ret = chmod(argv[1], 0755);
if(ret == -1)
{
perror("chmod");
exit(1);
}
return 0;
}
chown函数
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("a.out filename!\n");
exit(1);
}
// user->ftp group->ftp
int ret = chown(argv[1], 116, 125);
if(ret == -1)
{
perror("chown");
exit(1);
}
return 0;
}
truncate函数
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char* argv[])
{
if(argc < 3)
{
printf("a.out filename 111\n");
exit(1);
}
long int len = strtol(argv[2], NULL, 10);
int aa = truncate(argv[1], len);
if(aa == -1)
{
perror("truncate");
exit(1);
}
return 0;
}
symlink函数
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
if(argc < 3)
{
printf("a.out oldpath newpath\n");
exit(1);
}
int ret = symlink(argv[1], argv[2]);
if(ret == -1)
{
perror("symlink");
exit(1);
}
return 0;
}
readlink函数
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("a.out softlink\n");
exit(1);
}
char buf[512];
int ret = readlink(argv[1], buf, sizeof(buf));
if(ret == -1)
{
perror("readlink");
exit(1);
}
buf[ret] = 0;
printf("buf = %s\n", buf);
return 0;
}
unlink函数
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(void)
{
int fd = open("tempfile", O_CREAT | O_RDWR, 0755);
if(fd == -1)
{
perror("open");
exit(1);
}
int ret = unlink("tempfile");
if(ret == -1)
{
perror("unlink");
exit(1);
}
char buf[512];
write(fd, "hello", 5);
lseek(fd, 0, SEEK_SET);
int len = read(fd, buf, sizeof(buf));
write(STDOUT_FILENO, buf, len);
close(fd);
return 0;
}
rename函数
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
if(argc < 3)
{
printf("a.out oldName newName\n");
exit(1);
}
int ret = rename(argv[1], argv[2]);
if(ret == -1)
{
perror("rename");
exit(1);
}
return 0;
}
dup、dup2
作用:可以复制文件的描述符
dup使用
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = open("a.txt", O_RDWR);
if(fd == -1)
{
perror("open");
exit(1);
}
printf("file open fd = %d\n", fd);
// 找到进程文件描述表中 ==第一个== 可用的文件描述符
// 将参数指定的文件复制到该描述符后,返回这个描述符
int ret = dup(fd);
if(ret == -1)
{
perror("dup");
exit(1);
}
printf("dup fd = %d\n", ret);
char* buf = "你是猴子派来的救兵吗????\n";
char* buf1 = "你大爷的,我是程序猿!!!\n";
write(fd, buf, strlen(buf));
write(ret, buf1, strlen(buf1));
close(fd);
return 0;
}
dup2使用
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = open("english.txt", O_RDWR);
if(fd == -1)
{
perror("open");
exit(1);
}
int fd1 = open("a.txt", O_RDWR);
if(fd1 == -1)
{
perror("open");
exit(1);
}
printf("fd = %d\n", fd);
printf("fd1 = %d\n", fd1);
int ret = dup2(fd1, fd);
if(ret == -1)
{
perror("dup2");
exit(1);
}
printf("current fd = %d\n", ret);
char* buf = "主要看气质 ^_^!!!!!!!!!!\n";
write(fd, buf, strlen(buf));
write(fd1, "hello, world!", 13);
close(fd);
close(fd1);
return 0;
}
fcntl
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
int fd;
int flag;
// 测试字符串
char *p = "我们是一个有中国特色的社会主义国家!!!!!!";
char *q = "呵呵, 社会主义好哇。。。。。。";
// 只写的方式打开文件
fd = open("test.txt", O_WRONLY);
if(fd == -1)
{
perror("open");
exit(1);
}
// 输入新的内容,该部分会覆盖原来旧的内容
if(write(fd, p, strlen(p)) == -1)
{
perror("write");
exit(1);
}
// 使用 F_GETFL 命令得到文件状态标志
flag = fcntl(fd, F_GETFL, 0);
if(flag == -1)
{
perror("fcntl");
exit(1);
}
// 将文件状态标志添加 ”追加写“ 选项
flag |= O_APPEND;
// 将文件状态修改为追加写
if(fcntl(fd, F_SETFL, flag) == -1)
{
perror("fcntl -- append write");
exit(1);
}
// 再次输入新内容,该内容会追加到旧内容的后面
if(write(fd, q, strlen(q)) == -1)
{
perror("write again");
exit(1);
}
// 关闭文件
close(fd);
return 0;
}