linux - 为什么有孔的文件比没有孔的文件具有更小的磁盘 block ?

标签 linux unix

#include <fcntl.h>
#include <unistd.h>

char buf1[] = "abcdefghij";
char buf2[] = "ABCDEFGHIJ";
char buf3[10];

int
main(void)
{
  int fd;

  if ((fd = creat("file.hole", FILE_MODE)) < 0) {
    err_sys("creat error");
  }

  if (write(fd, buf1, 10) != 10) {                // offset is now = 10
    err_sys("buf1 write error");
  }

  if (lseek(fd, 16380, SEEK_SET) == -1) {         // offset now = 16380
    err_sys("lseek error");
  }

  if (write(fd, buf2, 10) != 10) {                // offset now = 16390
    err_sys("buf2 write error");
  }
  close(fd);

  if ((fd = open("file.hole", O_RDWR)) == -1) {
    err_sys("failed to re-open file");
  }

  ssize_t n;
  ssize_t m;
  while ((n = read(fd, buf3, 10)) > 0) {
    if ((m = write(STDOUT_FILENO, buf3, 10)) != 10) {
      err_sys("stdout write error");
    }
  }

  if (n == -1) {
    err_sys("buf3 read error");
  }
  exit(0);
}

我是unix系统编程的新手

有带孔的打码文件。

输出结果为:

$ls -ls file.hole file.nohole
8 -rw-r--r-- 1 sar 16394 time file.hole
20 -rw-r--r-- 1 sar 16394 time file.nohole

为什么有洞的文件比没有洞的文件有更少的磁盘 block ?

在我看来,没有空洞的文件需要更小的磁盘 block

因为有孔的文件比没有孔的文件传播得更多..

来自“UNIX 环境高级编程 3rd-Stevens Rago,示例 3.2”

最佳答案

为什么你认为没有孔的文件占用的空间更小?这恰恰相反。 如果文件有漏洞,则没有必要为该空间保留磁盘 block 。 磁盘 block 的数量与文件的传播无关,而是与你在文件中写入的数据大小直接相关。

关于linux - 为什么有孔的文件比没有孔的文件具有更小的磁盘 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26775062/

相关文章:

c - 我想编译一个libcurl程序

mysql - 如何使用 shell 脚本在 Linux 中存储 mysql 数据库

python - 使用 os.chmod 删除特定权限

arrays - 计算数组中的数字

linux - 自动下载脚本

linux - 回显到文件时错误重定向到输出流失败。为什么?

bash - Matlab 和 gnu 并行

linux - 优化一行 find + exec 命令

c++ - 接收数据包发送后,等待功能等待

python - 如何让我的 Python 模块在 Linux 系统范围内可用?