当前位置:网站首页>Copy of file descriptor
Copy of file descriptor
2022-07-22 18:22:00 【Lewin~】
Copy of file descriptors
dup and dup2 Two very useful system calls , All descriptors are used to copy a file , Make the new file descriptor also identify the file identified by the old file descriptor .
int dup(int oldfd);
int dup2(int oldfd, int newfd);
dup and dup2 Often used to redirect processes stdin、stdout and stderr.
review :ls > test.txt
Include header file #include <unistd.h>
dup Function introduction
int dup(int oldfd);
- function : Copy oldfd File descriptor , And assign a new file descriptor , The new file descriptor is the smallest available in the calling process file descriptor table File descriptor for .
- Parameters : File descriptor to copy oldfd.
- Return value : success : New file descriptor . Failure : return -1, The error code is stored in errno in .
dup2 Function introduction
int dup2(int oldfd, int newfd)
- function : Copy an open file descriptor oldfd, And assign a new file descriptor newfd,newfd Also identify oldfd The identified file .
- Be careful :newfd Is a non negative integer smaller than the maximum allowable value of the file descriptor , If newfd Is an open file descriptor , The file will be closed , And then copy .
- Parameters : File descriptor to copy oldfd New file descriptor allocated newfd
- Return value : success : return newfd Failure : return -1, The error code is stored in errno in
Case study 1:dup Realization printf Output redirection function
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
// If you need to realize the function of output redirection
// First of all, it looks like printf Functions are operation file descriptors 1 The corresponding file ,
// The default is the operation terminal , As long as you can put 1 The file corresponding to the identification changes , You can achieve output redirection
// So after creating the file descriptor corresponding to the file , take 1 File descriptor closed , Then passed dup
// The new file descriptor copied by the function is 1, such printf Function pair 1 operation , It is written in the file
int fd_file;
fd_file = open("test.txt", O_WRONLY | O_CREAT | O_TRUNC, 0664);
if(fd_file == -1)
{
perror("fail to open");
exit(1);
}
close(1);
int fd = dup(fd_file);
printf("hello world\n");
printf("fd = %d\n", fd);
return 0;
}
Execution results :
Case study 2:dup Realize output reset backward , Also want standard output , How to achieve
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd1;
int fd2;
fd2 = dup(1);
printf("new:fd2 = %d\n",fd2);
fd1 = open("test.txt", O_RDWR | O_CREAT, 0664);
close(1);
int fd3 = dup(fd1);
printf("hello world\n");
printf("fd = %d\n", fd3);
close(1);
int fd4 = dup(fd2);
printf("nihao beijing\n");
printf("fd = %d\n", fd4);
return 0;
}
Execution results :
Case study 3:dup2 Realization printf Output redirection
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void)
{
int fd1;
int fd2;
fd1 = open("test.txt", O_CREAT | O_WRONLY, 0664);
if (fd1 < 0)
{
perror("fail to open");
exit(1);
}
// First close 1 File descriptor , And then fd1 Copy to 1, signify 1 and fd1 All identification test.txt file , The return value follows 1 It's the same
fd2 = dup2(fd1, 1);
printf("hello world\n");
printf("fd2 = %d\n", fd2);
return 0;
}
Execution results :
Case study 4: dup2 Realize output reset backward , Restore the standard output
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd1;
// If you use dup2, You need to assign an initial value to the variable corresponding to the second parameter
int fd2 = 3;
// take 1 Copy one as fd2, therefore fd2 It identifies the standard output
dup2(1, fd2);
printf("fd2 = %d\n", fd2);
fd1 = open("test.txt", O_CREAT | O_RDWR, 0664);
// Output redirection : Close the file descriptor 1, take fd1 Copy one as 1, therefore 1 At this time, the symbol is test.txt file
dup2(fd1, 1);
printf("hello world\n");
// Realize standard output again : Close the file descriptor 1, take fd2 Copy one as 1, therefore 1 At this time, the standard output is marked
dup2(fd2, 1);
printf(" Hello Beijing \n");
return 0;
}
Execution results :
边栏推荐
猜你喜欢
Dokcer运行Nacos容器自动退出问题
QT notes - qudpsocket of network communication
Microsoft SQL Server数据库语言及功能使用(十二)
[exception] generate guid and datetime, import test data to the database
学点编程:防失业“疫苗”
超实用转型攻略:《2022央国企云原生落地实用指南》正式发布
QT notes - unpolish() and polish() of QT dynamic attributes
化繁为简,聊一聊复制状态机系统架构抽象
C#入门系列(二十七) -- LINQ简析
女嘉賓報名
随机推荐
Yunyuanyuan (10) | introduction to kubernetes in kubernetes
Go FMT package details
QT notes - vs generating multiple exe files for a project
Gbase8s database restrictions on set collection
「武汉理工大学 软件工程复习」第六章 | 编码规范
测试员职场最大的危机不是30岁,而是中年被裁
GBase8s数据库MINUS 运算符
线程学习笔记
Gbase8s database set constraints statement
Concurrent model values actor and CSP
水博士
QT笔记——QTableWidget 之 指定某列排序
QT笔记—— VS一个项目生成多个exe文件
QT notes - unpolish() and polish() of QT dynamic attributes
「武汉理工大学 软件工程复习」第三章 | 软件需求
QT笔记——QtXml
A survey of network defense decision-making methods based on attack defense game
QT exe is only allowed to run a single
Super practical transformation strategy: 2022 central state-owned enterprise cloud native landing practical guide was officially released
QT笔记——实现窗体自适应