Linux dup and dup2

Linux dup and dup2 functions, what are the differences between them, what scenarios will they be used in, and what precautions should be taken when using them dup and dup2 are both system calls in Linux systems, used to copy file descriptors. Their main difference is how new file descriptors are specified and how new […]

dup, dup2, F_DUPFD, dup3, F_DUPFD_CLOEXEC: examples

This example redirects the output and error output to the file outlog.txt. dup implementation Test code: #include <sys/types.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <getopt.h> #include <sys/epoll.h> #define _DEBUG_INFO #ifdef _DEBUG_INFO #define DEBUG_INFO(format, …) printf(“%s:%d $$ ” format “\ ” \ ,__func__,__LINE__ \ , ##__VA_ARGS__) #else […]

C language file descriptor operation under Linux (dup / dup2 / sendfile / splice / tee)

The philosophy of Linux is that everything is a file, and files are manipulated through file descriptors. This article sorts out the operation of the dup / dup2 / sendfile / splice / tee function on the file descriptor. Directory 1. dup 2.dup2 3. sendfile 4.splice 5. tee 1.dup #include int dup(int fd); Duplicate an […]

linux(stat-readdir-dup2)04-virtual address space, stat function, file, directory, errno description, dup2 and dup

01 Learning Objective 1. Master the use of stat/lstat function 2. Understand the use of functions related to file attributes 3. Understand the use of functions related to directory operations 4. Master the use of directory traversal related functions 5. Master the use of dup and dup2 functions 6. Master the use of fcntl function […]

Linux system programming 1. open function 2. read reads device files, network files, and pipeline files will block 3. lseek re-establishes the offset of read write 4. dup2 redirection 5. Source code implementation of common commands (ls cp mv)

1. open function #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <string.h> int main(int argc, char *argv[]) { int fd = open(“./mydir”, O_WRONLY); printf(“fd = %d, errno = %d\\ “, fd, errno); if (fd == -1) { printf(“%s\\ “, strerror(errno)); } \t int fd1 = close(fd); printf(“fd1 = %d\\ “, […]

The difference between file descriptors and FILE* (open, write, read), redirection (dup2)

Article directory FILE* file descriptor fd system call file note open() read() write() redirect dup2() Examples of fork and buffers. FILE* Do the pointer type returned by calling the library function fopen(). FILE *fopen(const char *path, const char *mode); In order to manage files, the operating system defines a data structure file to manage files. […]