【Linux基础IO】重定向以及原理分析

我们先来看下面一个情况:

cpp 复制代码
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define filename "text.txt"
  int main()
  {
    close(1);//关闭了Liunx操作系统为我们默认打开的fd = 1的文件流stdout
    int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
    if(fd < 0)
    {
      perror("open");
      return 1;
    }
    const char* arr = "hello fileoperation\n";
    int cnt = 3;
    while(cnt)
    {
      write(1, arr, strlen(arr));
      --cnt;
    }
    close(fd);
  
    return 0;
  }                       

为什么我们把fd=1的显示器文件流stdin关闭了,向fd=1的显示器文件中写入文件就会写入到text.txt里呢?这就发生了重定向,常见的重定向有:>, >>, < 。 首先得弄清楚操作系统对于文件描述符的分配规则:在task_struct里存储的指针指向的files_struct数组当中,找到当前没有被使用的最小的一个下标,作为新的文件描述符。

dup2函数

利用dup2函数进行重定向:

cpp 复制代码
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #define filename "text.txt"
   int main()
   {
    //close(1);//关闭了Liunx操作系统为我们默认打开的fd = 1的文件流stdout                                                                              
    int fd = open(filename, O_RDONLY);
    if(fd < 0)
    {
      perror("open");
      return 1;
    }
    dup2(fd, 0);
    close(fd);//关掉与不关掉都可以,因为fd已经有了一份拷贝
    char buffer[100];
    ssize_t a = read(0, buffer, sizeof(buffer)-1);
    if(a > 0)
    {
      buffer[a] = '\0';
      printf("buffer:%s\n", buffer);
    }
  
    return 0;
  }

可以看到发生了重定向。

命令行中实现重定向:

stdout与stderr

cpp 复制代码
#include <stdio.h>
   int main()
   {
     fprintf(stdout, "hello normal message\n");
     fprintf(stdout, "hello normal message\n");
     fprintf(stderr, "hello error message\n");
     fprintf(stderr, "hello error message\n");                                                                                                         
     return 0;
   }

重定向的原理分析

重定向的本质是对描述进程的task_struct里的成员指针指向的文件描述符表进行修改。

相关推荐
xiaoxiangsiyan4 小时前
LNMP + Redis Sentinel 高可用架构部署手册(续)
运维·网络·数据库·redis·缓存·架构·sentinel
AIgorithmGEEK4 小时前
Linux 权限管理:从「Permission Denied」到「畅行无阻」
linux·权限·chmod·粘滞位·umask·文件权限的构成
BullSmall5 小时前
scanoss-engine 本地最简部署 & 扫描命令(Linux/Anolis OS)
linux·运维·服务器
陈聪.5 小时前
Keepalived 高可用集群:从原理到企业级实践
运维
bellus-5 小时前
Ubuntu 26.04 Miniforge3 安装与配置完整指南
linux·运维·ubuntu
575775 小时前
户外净水器过滤效果怎么看:对照孔径、除菌率和认证三项再下单
运维·服务器
瞬间&永恒~5 小时前
【Docker】(四)Namespace 详解
运维·docker·容器
RD_daoyi6 小时前
Google核心算法不再通知!全年持续滚动更新
大数据·服务器·前端·网络·搜索引擎·.net
爱喝水的木子6 小时前
自建RustDesk私有服务器全攻略
运维·服务器
jieyucx6 小时前
【服务器特性】结合服务器/CMS:解析漏洞实战
运维·服务器·web安全·文件上传