Linux dup dup2函数理解 |
您所在的位置:网站首页 › dup和dup2函数 › Linux dup dup2函数理解 |
在linux中,我们需要复制文件描述符,下面是我对文件描述符的理解
int dup(int fd)// 复制一个已经存在的文件描述符,如果成功,返回复制成功后的文件描述符,失败返回-1
int dup(int fd, int fd2)// 复制一个文件描述符,fd表示已经存在的打开的文件描述符,fd2是指定新的文件描述符,如果fd2等于fd,则直接返回,如果fd2存在并且打开,则先close(fd2)后,重新打开,这样fd2和fd就指向了同一个文件(共享打开的文件),如果fd2不存在或者没有打开,则打开fd2,并且指向fd所指向的文件。函数的返回值和fd2一致。该函数可以实现文件的重定位。
每个进程都有一个文件描述符表,每个描述符占用一个描述符项,每个文件描述符可以描述成这样
struct fileDescription {
int index
void *pointer
}
除了整形,还有一个指针,指向文件表,内核为所有打开文件维持一张文件表,每个文件表项包含:
1)文件状态标志(读、写、添写、同步和非阻塞等)
2)当前文件的偏移量
3)指向该文件v节点表项的指针
v节点表中包含了文件所有者、文件长度、文件所在的设备、指向文件实际数据块在磁盘上所在位置的指针等等
下面给出一个例子:
#include iostream
#include stdio.h
#include fcntl.h
#include sys/stat.h
#include unistd.h
#include string.h
using namespace std int main()
{
cout "Hello world!" endl
int fd = open("a.txt", O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
if (fd 0)
{
printf("open a.txt failed, fd = %d\n", fd)
return -1
}
printf("open a.txt success, fd = %d\n", fd)
fflush(stdout)
// 复制标准输出的文件描述符
int nfd = dup(STDOUT_FILENO)
int fileFd = dup2(fd, STDOUT_FILENO)
if (fileFd 0)
{
printf("dup2 stdout_fileno failed, fileFd = %d\n", fileFd)
return -1
}
printf("重定向标准输出到a.txt, fileFd = %d\n", fileFd)
// 将STDOUT_FILENO复制到fd后,fd并没有发生变化,依然可以通过它写入到a.txt中
const char *pStr = "write string through fd\n"
write(fd, pStr, strlen(pStr))
fileFd = dup2(nfd, fileFd)
if (fileFd 0)
{
printf("dup2 reback stdout_fileno failed\n")
return -1
}
printf("print back to stdout standard, fileFd = %d\n", fileFd)
// 上面使用dup2的时候,STDOUT_FILENO是存在并打开的,我们来测试下不存在的文件描述符的情况
fileFd = dup2(fd, 20)
if (fileFd 0)
{
printf("dup2不存在的文件描述符失败, fileFd = %d\n", fileFd)
}
else
{
printf("dup2不存在的文件描述符成功, fileFd = %d\n", fileFd)
const char *pStr = "write string through fileFd\n"
write(fileFd, pStr, strlen(pStr))
}
close(nfd)
close(fileFd)
return 0
}
文件指针是关键,标志两个文件描述符是否一致,看文件指针是否一致即可 ,如果两个或者多个文件描述符指向同一个文件表,那么对他们的操作是对同一个文件进行操作 用途:不挂断地运行命令。
语法:nohup Command [ Arg … ] [ ]
无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中。
如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中。
如果没有文件能创建或打开以用于追加,那么 Command 参数指定的命令不可调用。
退出状态:该命令返回下列出口值:
126 可以查找但不能调用 Command 参数指定的命令。
127 nohup 命令发生错误或不能查找由 Command 参数指定的命令。
否则,nohup 命令的退出状态是 Command 参数指定命令的退出状态。
用途:在后台运行
一般两个一起用
3.为什么呢 21 要写在后面?
首先是command file将标准输出重定向到file中, 21 是标准错误拷贝了标准输出的行为,也就是同样被重定向到file中,最终结果就是标准输出和错误都被重定向到file中。
21 标准错误拷贝了标准输出的行为,但此时标准输出还是在终端。file 后输出才被重定向到file,但标准错误仍然保持在终端。
用strace可以看到:
这个命令中实现重定向的关键系统调用序列是:
open(file) == 3
dup2(3,1)
dup2(1,2) 这个命令中实现重定向的关键系统调用序列是:
dup2(1,2)
open(file) == 3
dup2(3,1) 注意:0 是标准输入(STDIN),1 是标准输出(STDOUT),2 是标准错误输出(STDERR)。
这里的2和之间不可以有空格,2是一体的时候才表示错误输出。 Linux关于管道 原创2018-09-14 12:22:41
Gaodes 码龄5年 关注 管道的概念 管道是Unix中最古老的进程间通信的形式。 我们把从一个进程连接到另一个进程的一个数据流称为一个“管道” 我们通常把是把一个进程的输出连接或“管接”(经过管道来连接)到另一个进程的输入。 管道特点 管道是半双工的,数据只能向一个方向流动;需要双方通信时,需要建立起两个管道 只能用于父子进程或者兄弟进程之间(具有亲缘关系的进程)进行通信;通常,一个管道由一个进程创建,然后该进程调用fork,此后父、子进程之间就可应用该管道。 pipe函数 包含头文件unistd.h功能:创建一无名管道 原型 int pipe(int file_descriptor[2]) 参数 file_descriptor:文件描述符数组,其中file_descriptor[0]表示读端,file_descriptor[1]表示写端 返回值:成功返回0,失败返回错误代码 示例代码: #includestdio.h #includeunistd.h #includestdlib.h #includesignal.h #includestring.h
int main(int argc,char *argv[]) { int fd[2] printf("f[0]=%d,f[1]=%d\n",fd[0],fd[1]) pipe(fd) printf("f[0]=%d,f[1]=%d\n",fd[0],fd[1])
char buf[1024]={0} int fid = fork() if(fid 0) { read(fd[0],buf,1024) printf("read data %s\n",buf) } else if(fid == 0) { write(fd[1],"helloworld",strlen("helloworld"))
} else { perror("fork error") } return 0 } 打印结果 管道读写规则:如果试图从管道写端读取数据,或者向管道读端写入数据都将导致错误发生 当没有数据可读时,read调用就会阻塞,即进程暂停执行,一直等到有数据来到为止。 如果管道的另一端已经被关闭,也就是没有进程打开这个管道并向它写数据时,read调用就会阻塞 复制文件描述符dup #includestdio.h #includestdlib.h #includestring.h #includesignal.h
int main() { int fd = dup(1)
printf("file fd= %d\n",fd) write(fd,"helloworld",strlen("helloworld")) return 0 } 打印结果: 1为输入到终端 shell管道的实现 原理通过把发的fd[1]写复制到shell的1(标准输入),fd[0]复制到shell的2(标准输出) 以下是代码: #includestdio.h #includestdlib.h #includefcntl.h #includeunistd.h #includestring.h #includesignal.h
int main() { int fd[2] char buf[1024] ={0} pipe(fd) int pid = fork()
if(pid 0) { read(fd[0],buf,1024) printf(buf) } else if(pid == 0) { dup2(fd[1],1) close(fd[0]) close(fd[1]) execlp("ls","ls","-al",NULL) } else { } return 0 } 实现结果: popen函数 作用:允许一个程序把另外一个程序当作一个新的进程来启 动,并能对它发送数据或接收数据 FILE* popen(const char *command,const char *open_mode) command:待运行程序的名字和相应的参数 open_mode:必须是“r”或“w” 如果操作失败,popen会返回一个空指针 以下代码: #includestdio.h #includestdlib.h #includesys/types.h #includesys/stat.h #includefcntl.h #includestring.h
int main() { FILE *file = popen("ls -al","r") char buf[1024] = {0} fread(buf,1,1024,file) fclose(file) FILE *wcfile = popen("wc","w") fwrite(buf,1,strlen(buf),wcfile) fclose(wcfile) return 0 } 代码结果: 命名管道破裂测试 我们首先要知道命名管道,要读段和写段同时开启,才能向文件读写数据。 贴上代码来理解命名管道的规则 首先是读端: #includestdio.h #includeunistd.h #includestdlib.h #includesignal.h #includestring.h #includefcntl.h
int main(int argc,char *argv[]) { printf("open before\n") int fd = open("/home/gao/tmp/fifo",O_RDONLY) printf("open after\n")
//休眠5秒,读端退出 sleep(5) return 0 } 接下来是写端: #includestdio.h #includeunistd.h #includestdlib.h #includesignal.h #includestring.h #includefcntl.h
void handle(int signo) { printf("cat signale = %d\n",signo) }
int main(int argc,char *argv[]) { signal(SIGPIPE,handle) printf("open before\n") int fd = open("/home/gao/tmp/fifo",O_WRONLY) printf("open after\n") //命名管道规则,如果写入读断被中断,写入会返回-1,并且管道会破裂,产生信号(SIGPIPE) while(1) { int wrsize = write(fd,"helloworld",strlen("helloworld")) printf("size data:%d\n",wrsize) sleep(1) } } 执行写端: 它在等待另一端的开启,才能向里面写入数据 此时我们开启读端: 马上可以看到写段可以写数据 而执行5秒后,我们可以看到写的时候返回-1,并且获取到管道破裂的信息(SIGPIPE) 所以这里就是我们所注意的点,当我们写客户端和服务器进行管道传输的时候,如果客户端一旦退出来,就会使管道破裂,所以我们必须通过捕捉信号,来避免这种事情发生。 欢迎分享,转载请注明来源:内存溢出 原文地址:https://outofmemory.cn/yw/6119985.html |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |