fcntl设置FD_CLOEXEC标志作用
来源:互联网 发布:腾讯java工程师面试题 编辑:程序博客网 时间:2024/04/27 04:06
通过fcntl设置FD_CLOEXEC标志有什么用?
close on exec, not on-fork, 意为如果对描述符设置了FD_CLOEXEC,使用execl执行的程序里,此描述符被关闭,不能再使用它,但是在使用fork调用的子进程中,此描述符并不关闭,仍可使用。
eg:
jamie@jamie-laptop:~$ cat test.c
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
int fd,pid;
char buffer[20];
fd=open("wo.txt",O_RDONLY);
printf("%d/n",fd);
int val=fcntl(fd,F_GETFD);
val|=FD_CLOEXEC;
fcntl(fd,F_SETFD,val);
pid=fork();
if(pid==0)
{
//子进程中,此描述符并不关闭,仍可使用
char child_buf[2];
memset(child_buf,0,sizeof(child_buf) );
ssize_t bytes = read(fd,child_buf,sizeof(child_buf)-1 );
printf("child, bytes:%d,%s/n/n",bytes,child_buf);
//execl执行的程序里,此描述符被关闭,不能再使用它
char fd_str[5];
memset(fd_str,0,sizeof(fd_str));
sprintf(fd_str,"%d",fd);
int ret = execl("./exe1","exe1",fd_str,NULL);
if(-1 == ret)
perror("ececl fail:");
}
waitpid(pid,NULL,0);
memset(buffer,0,sizeof(buffer) );
ssize_t bytes = read(fd,buffer,sizeof(buffer)-1 );
printf("parent, bytes:%d,%s/n/n",bytes,buffer);
}
jamie@jamie-laptop:~$ cat exe1.c
#include <fcntl.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
int main(int argc, char **args)
{
char buffer[20];
int fd = atoi(args[1]);
memset(buffer,0,sizeof(buffer) );
ssize_t bytes = read(fd,buffer,sizeof(buffer)-1);
if(bytes < 0)
{
perror("exe1: read fail:");
return -1;
}
else
{
printf("exe1: read %d,%s/n/n",bytes,buffer);
}
return 0;
}
jamie@jamie-laptop:~$ gcc -o exe1 exe1.c
jamie@jamie-laptop:~$ gcc -o test test.c
jamie@jamie-laptop:~$ cat wo.txt
this is a test
jamie@jamie-laptop:~$ ./test
3
child, bytes:1,t //子进程中可使用fd
exe1: read fail:: Bad file descriptor //execl调用的程序中不能使用fd
parent, bytes:14,his is a test //父进程中当然能使用fd
close on exec, not on-fork, 意为如果对描述符设置了FD_CLOEXEC,使用execl执行的程序里,此描述符被关闭,不能再使用它,但是在使用fork调用的子进程中,此描述符并不关闭,仍可使用。
eg:
jamie@jamie-laptop:~$ cat test.c
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
int fd,pid;
char buffer[20];
fd=open("wo.txt",O_RDONLY);
printf("%d/n",fd);
int val=fcntl(fd,F_GETFD);
val|=FD_CLOEXEC;
fcntl(fd,F_SETFD,val);
pid=fork();
if(pid==0)
{
//子进程中,此描述符并不关闭,仍可使用
char child_buf[2];
memset(child_buf,0,sizeof(child_buf) );
ssize_t bytes = read(fd,child_buf,sizeof(child_buf)-1 );
printf("child, bytes:%d,%s/n/n",bytes,child_buf);
//execl执行的程序里,此描述符被关闭,不能再使用它
char fd_str[5];
memset(fd_str,0,sizeof(fd_str));
sprintf(fd_str,"%d",fd);
int ret = execl("./exe1","exe1",fd_str,NULL);
if(-1 == ret)
perror("ececl fail:");
}
waitpid(pid,NULL,0);
memset(buffer,0,sizeof(buffer) );
ssize_t bytes = read(fd,buffer,sizeof(buffer)-1 );
printf("parent, bytes:%d,%s/n/n",bytes,buffer);
}
jamie@jamie-laptop:~$ cat exe1.c
#include <fcntl.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
int main(int argc, char **args)
{
char buffer[20];
int fd = atoi(args[1]);
memset(buffer,0,sizeof(buffer) );
ssize_t bytes = read(fd,buffer,sizeof(buffer)-1);
if(bytes < 0)
{
perror("exe1: read fail:");
return -1;
}
else
{
printf("exe1: read %d,%s/n/n",bytes,buffer);
}
return 0;
}
jamie@jamie-laptop:~$ gcc -o exe1 exe1.c
jamie@jamie-laptop:~$ gcc -o test test.c
jamie@jamie-laptop:~$ cat wo.txt
this is a test
jamie@jamie-laptop:~$ ./test
3
child, bytes:1,t //子进程中可使用fd
exe1: read fail:: Bad file descriptor //execl调用的程序中不能使用fd
parent, bytes:14,his is a test //父进程中当然能使用fd
- fcntl设置FD_CLOEXEC标志作用
- fcntl设置FD_CLOEXEC标志作用
- fcntl设置FD_CLOEXEC标志作用
- fcntl设置FD_CLOEXEC标志作用
- fcntl设置FD_CLOEXEC标志作用
- fcntl设置FD_CLOEXEC标志作用
- fcntl设置FD_CLOEXEC标志作用
- fcntl设置FD_CLOEXEC标志作用
- fcntl设置FD_CLOEXEC标志作用
- fcntl设置FD_CLOEXEC标志有什么用
- 通过fcntl设置FD_CLOEXEC标志有什么用?
- 通过fcntl设置FD_CLOEXEC标志有什么用?
- fcntl设置FD_CLOEXEC
- fcntl设置FD_CLOEXEC
- fcntl(nListenId, F_SETFD, FD_CLOEXEC) 的作用
- fcntl FD_CLOEXEC
- fcntl & FD_CLOEXEC
- fcntl的FD_CLOEXEC标志位与exec族
- 大规模WEB服务之实用算法(一)
- 下载verycd的方法下载电驴资源隐藏资源的最新可用方法
- 在iframeset中从某个iframe跳转到另一个iframe
- copy 命令
- 对JavaScript原理的理解
- fcntl设置FD_CLOEXEC标志作用
- jq live的ajax函数调用
- sass学习——SassScript
- 戴尔私有化新变数:伊坎黑石加入导致三方竞购
- 黑马程序员—Set接口
- LINUX系统性能调谐
- sql server脚本笔记
- ASE : Display Available Device Space - Free Disk Space
- ioctl的相关理解