《深入理解linx内核》学习笔记3---子进程共享父进程打开的文件描述符

子进程共享父进程打开的文件描述符

    当使用fork()创建一个新进程后,对于父进程已经打开,在调用fork()时,未关闭的文件描述符,子进程均将获得一份副本,这里所说的副本是位于内核地址空间中,指向已打开的文件的文件描述符的结构体。子进程至少会获得3个父进程的打开的文件描述符的副本,stdin,stdout,stderr。对于这3个描述符,子进程中可以直接使用,父进程不需要通过参数传递或自定义环境变量的方式传递给子进程。但,对于父进程打开的普通文件,如父进程打开的一个当前目录先的output.txt文件,那么父进程需要通过参数或自定义环境变量的方式,将此文件描述符传递给子进程,这样,子进程才能对其进行访问。否则,虽然在fork时,子进程已经在内核地址空间中,获得了此文件描述结构体,但是,没有对应的int型的fd,仍然无法对其进行访问。下面的代码展示了,父进程通过传递参数的方式,将父进程打开的文件的fd传递给子进程,然后,子进程对此文件进行访问。

demo1

父进程代码

<span style="font-size:18px;">fork-exec.c</span>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<string.h>


void main()
{
	pid_t pid ;
	int fd = 0 ;
	char fdstr[16] = {0} ;

	fd = open("./output.txt" , O_RDWR , O_APPEND) ;
	printf("parent:%d\n" , fd) ;

	write(fd , "parent\n" , strlen("parent\n")) ;
	sprintf(fdstr , "%d" , fd) ; // 将父进程打开的文件描述符,转换成字符串。

 	if((pid = fork()) < 0)
		printf("error:fork\n") ;
	else if(pid == 0)
	{
		if(execl("./a" , "a" ,fdstr , (char*)0) < 0 ) // 将文件描述转换成的字符串以参数的方式传递给子进程
			printf("error:execl\n") ;
	}
	
	if(waitpid(pid , NULL , 0) < 0)
		printf("error:wait\n") ;
	
	printf("retval of write in parent:%d\n" , write(fd ,"in parent , after waitpid()\n" , strlen("in parent , after waitpid()\n")) ) ;
}

子进程代码

a.c

<span style="font-size:18px;">/*************************************************************************
	> File Name: a.c
	> Author: ma6174
	> Mail: ma6174@163.com 
	> Created Time: Fri 27 Jun 2014 08:35:05 PM CST
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include <string.h>
void main(int argc , char* argv[])
{
	int fd = 0 ;
	
	printf("hello\n") ;
	printf("argc:%d\n" , argc) ;
	printf("argv[1]:%s\n" , argv[1]) ;
	
	sscanf(argv[1] , "%d" , &fd) ; // 将父进程传递的文件描述符的fd由字符串转换成为相应的int型。

	printf("child:%d\n" , fd) ;
	write(fd , "child\n" , strlen("child\n")) ; // 向文件中写入数据
	
	exit(0) ;
}
</span>

编译运行:

touch output.txt

gcc fork-exec.c -o fork-exec

gcc a.c -o a

./fork-exec

cat output.txt

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


void main()
{
	pid_t pid ;
	int fd = 0 ;
	char fdstr[16] = {0} ;

	fd = open("./output.txt" , O_RDWR , O_APPEND) ;
	printf("parent:%d\n" , fd) ;

	write(fd , "parent\n" , strlen("parent\n")) ;
	sprintf(fdstr , "%d" , fd) ; // 将父进程打开的文件描述符,转换成字符串。

 	if((pid = fork()) < 0)
		printf("error:fork\n") ;
	else if(pid == 0)
	{
		if(execl("./a" , "a" ,fdstr , (char*)0) < 0 ) // 将文件描述转换成的字符串以参数的方式传递给子进程
			printf("error:execl\n") ;
	}
	
	if(waitpid(pid , NULL , 0) < 0)
		printf("error:wait\n") ;
	
	printf("retval of write in parent:%d\n" , write(fd ,"in parent , after waitpid()\n" , strlen("in parent , after waitpid()\n")) ) ;
}

运行结果:

parent
child
从文件内容可以看出,子进程在父进程的文件中写入了字符串。

demo2
调用close仅能关闭父子进程各自的文件描述符,并未关闭文件
父进程代码不变,在子进程中,当写完“child”后,关闭文件描述fd。然后,子进程结束,父进程中,继续向文件中写入字符串。
父进程代码:
<span style="font-size:18px;">#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<string.h>


void main()
{
	pid_t pid ;
	int fd = 0 ;
	char fdstr[16] = {0} ;

	fd = open("./output.txt" , O_RDWR , O_APPEND) ;
	printf("parent:%d\n" , fd) ;

	write(fd , "parent\n" , strlen("parent\n")) ;
	sprintf(fdstr , "%d" , fd) ; // 将父进程打开的文件描述符,转换成字符串。

 	if((pid = fork()) < 0)
		printf("error:fork\n") ;
	else if(pid == 0)
	{
		if(execl("./a" , "a" ,fdstr , (char*)0) < 0 ) // 将文件描述转换成的字符串以参数的方式传递给子进程
			printf("error:execl\n") ;
	}
	
	if(waitpid(pid , NULL , 0) < 0)
		printf("error:wait\n") ;
	
	printf("retval of write in parent:%d\n" , write(fd ,"in parent , after waitpid()\n" , strlen("in parent , after waitpid()\n")) ) ; // 虽然子进程将其自己的文件描述符结构体销毁了,但并未关闭打开的文件,因此,父进程仍然可以向其中写入字符串。
}
</span>


子进程代码:
a.c
<span style="font-size:18px;">/*************************************************************************
	> File Name: a.c
	> Author: ma6174
	> Mail: ma6174@163.com 
	> Created Time: Fri 27 Jun 2014 08:35:05 PM CST
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include <string.h>
void main(int argc , char* argv[])
{
	int fd = 0 ;
	
	printf("hello\n") ;
	printf("argc:%d\n" , argc) ;
	printf("argv[1]:%s\n" , argv[1]) ;
	
	sscanf(argv[1] , "%d" , &fd) ; // 将父进程传递的文件描述符的fd由字符串转换成为相应的int型。

	printf("child:%d\n" , fd) ;
	write(fd , "child\n" , strlen("child\n")) ; // 向文件中写入数据
	close(fd) ;  // 此处,只关闭了子进程的文件描述符,销毁了内核中,为子进程创建的描述符结构体,并未关闭父进程打开的文件。
	exit(0) ;
}
</span>
执行结果:







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值