首页 > 系统相关 >Linux系统编程18-dup和dup2.md

Linux系统编程18-dup和dup2.md

时间:2022-10-14 22:22:08浏览次数:34  
标签:md txt dup2 int 18 fd1 fd include

dup

#include <unistd.h>

int dup(int oldfd);
    作用: 复制一个新的文件描述符,指向同一个文件,
			从空闲的文件描述符表中找一个最小的作为新文件描述符
    参数:
        - oldfd 旧的文件描述符
    返回:
        新描述符
        -1 错误

实例: 复制一个文件描述符,并且为所指向的文件写入数据

dup.c

#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    int fd = open("dup.txt", O_RDWR | O_CREAT, 0664);
    int fd1 = dup(fd);
    if (fd == -1)
    {
        perror("dup err");
        return -1;
    }
    printf("fd : %d, fd1 : %d\n", fd, fd1);
    close(fd);
    char *str = "hello world";
    int ret = write(fd1, str, strlen(str));
    if (ret == -1)
    {
        perror("write err");
        return -1;
    }
    close(fd1);

    return 0;
}

dup2

#include <unistd.h>
int dup2(int oldfd, int newfd);
    作用: 重定向文件描述符
        例如 oldfd 指向 a.txt, newfd 指向 b.txt
        调用函数成功后, newfd 的 b.txt 做 close, newfd指向了a.txt;
        oldfd 必须是一个有效的文件描述符
        如果 oldfd 和 newfd 值相同,相当于什么都没有做
    返回: 与newfd相同的新文件描述符

实例:

dup2.c

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char const *argv[])
{
    //创建dup2.txt
    int fd = open("dup2.txt", O_RDWR | O_CREAT, 0664);
    if (fd == -1)
    {
        perror("open err");
        return -1;
    }
	//创建dup3.txt
    int fd1 = open("dup3.txt", O_RDWR | O_CREAT, 0664);
    if (fd1 == -1)
    {
        perror("open err");
        return -1;
    }
    printf("fd : %d, fd1 : %d\n", fd, fd1);

    // fd1不再指向dup3.txt, 而是指向和fd的同一个文件dup2.txt
    int fd2 = dup2(fd, fd1);
    if (fd2 == -1)
    {
        perror("dup2 err");
        return -1;
    }

    //通过fd1 去写数据
    char *str = "hello, dup2";
    int len = write(fd1, str, strlen(str));
    if (len == -1)
    {
        perror("write err");
        return -1;
    }

    printf("fd : %d, fd1 : %d, fd2 : %d\n", fd, fd1, fd2);
    // fd : 3, fd1 : 4
    // fd : 3, fd1 : 4, fd2 : 4
    close(fd);
    close(fd1);

    return 0;
}

标签:md,txt,dup2,int,18,fd1,fd,include
From: https://www.cnblogs.com/anqwjoe/p/16793207.html

相关文章

  • [NOIP2018 提高组] 铺设道路 贪心证明
    首先,这个是本蒟蒻第一次正经证明贪心,方法肯定有些繁琐(知识有限),仅作纪念。证明:记\(f(x)\)为序列中从第\(1\)到第\(x\)个数满足题意的最小天数。对于非上升序列\(\{a_1,a......
  • 打开cmd的几种方法
    打开cmd窗口的几种方法开始+Windows系统+命令提示符Windows+R,输入cmd,回车(推荐使用)在任意的文件夹下,按住shift键+鼠标右键,打开powershell窗口资源管理器的地址栏前加......
  • DEMO: BAPI_SALESORDER_CREATEFROMDAT2 创建订单
    REPORTzdemo_va01.PARAMETERSp_kunnrTYPEkunnrDEFAULT'1004615'.PARAMETERSp_vkorgTYPEvkorgDEFAULT'S600'.PARAMETERSp_vtwegTYPEvtwegDEFAULT'10'.PARAM......
  • 20201318李兴昕第四章学习笔记
    第四章:并发编程知识点归纳总结:本章论述了并发编程,介绍了并行计算的概念,指岀了并行计算的重要性;比较了顺序算法与并行算法,以及并行性与并发性;解释了线程的原理及其相对......
  • Windows CMD批处理
    需要生成uuid时,可以使用WindowsSDK自带的工具uuidgen.exe如下  如果需要生成一千个,那么使用批处理生成到txt文件中,批处理脚本如下@echoonfor/l%%iin(1,1,100......
  • Codeforces Global Round 18 C
    C.Menorah显然对于每个操作我们是保留一个1所以我们当先是x个1的话做一次就是n+1-x个1并且我们只有这两种数量这样我们就可以特判无解了之后显然对于每两个操作我......
  • 188.best time to buy and sell stock iv 买卖股票的最佳时机IV
    问题描述188.买卖股票的最佳时机IV解题思路123.买卖股票的最佳时机III相当于是本题中\(k=2\)的情形,递推关系的推导与写法参照123.买卖股票的最佳时机III即可。代码#in......
  • 前端 Blob 与 File 捎带 FormData 摘要
      Blob(BinaryLargeObject)表示二进制类型的大对象,通常是图片、视频、文档等如上图所示 一个blob对象 File 接口基于 Blob,继承了blob的功能并将其扩展使......
  • windows bat cmd 创建固定大小文件
    @echooffsetfilenum=1setfilesize=10485760settmppth="c:\tmp"setdespth="z:\test"rmdir%tmppth%/s/qmd%tmppth%md%despth%for/l%%iin(1,1,%file......
  • uni-app 188修复弹框问题
    弹框问题/components/free-ui/free-nav-bar.vue<template><view><view:class="getClass"><!--状态栏--><view:style="'height:'+statusBarHeight+'px......