天天看点

Linux中fork的使用(05)---父子进程共享文件描述符

环境:Vmware Workstation;CentOS-6.4-x86_64

说明:

1、父进程和子进程可以共享打开的文件描述符。

2、父子进程共享文件描述符的条件:在fork之前打开文件。

3、对于两个完全不相关的进程,文件描述符不能共享。

4、父子进程文件描述符是共享的,但是关闭的时候可以分别关闭,也可以同时在公有代码中关闭。

准备文件a.txt:

This is some words.
           

makefile文件:

.SUFFIXES:.c .o

CC=gcc

SRCS=main.c
OBJS=$(SRCS:.c=.o)
EXEC=main

start: $(OBJS)
	$(CC) -o $(EXEC) $(OBJS)
	@echo "-----------------------------OK-----------------------"

.c.o:
	$(CC) -Wall -o [email protected] -c $<

clean:
	rm -rf $(EXEC) $(OBJS)
           

程序1:父子进程的文件描述符可以共享,但是要在fork之前打开,并在公有代码中关闭文件。

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

int main(int argc, char *args[])
{
	// 打开文件描述符:在fork之前打开才能让父进程和子进程共享
	int fd = open("a.txt", O_RDONLY);
	// 判断文件打开是否成功
	if (fd == -1)
	{
		printf("File open failed : %s\n", strerror(errno));
	}
	
	// 执行fork函数
	pid_t pd = fork();
	// 判断fork是否成功
	if (pd == -1)
	{
		printf("fork failed : %s\n", strerror(errno));
	}
	
	// 通过if...else...执行父子进程的特有代码
	if (pd > 0)
	{
		printf("father fd = %d\n", fd);
	}
	else
	{
		printf("son fd = %d\n", fd);
	}
	close(fd);	
	return 0;
}
           

编译程序并执行:

[[email protected] mycode]$ make
gcc -Wall -o main.o -c main.c
gcc -o main main.o
-----------------------------OK-----------------------
[[email protected] mycode]$ ./main
father fd = 3
[[email protected] mycode]$ son fd = 3
           

程序2:在父进程中读取文件信息,并在父子进程中分别关闭文件描述符。

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

int main(int argc, char *args[])
{
	// 打开文件描述符:在fork之前打开才能让父进程和子进程共享
	int fd = open("a.txt", O_RDONLY);
	// 判断文件打开是否成功
	if (fd == -1)
	{
		printf("File open failed : %s\n", strerror(errno));
	}
	
	// 定义读取文件的缓冲区
	char buf[1024];
	
	// 执行fork函数
	pid_t pd = fork();
	// 判断fork是否成功
	if (pd == -1)
	{
		printf("fork failed : %s\n", strerror(errno));
	}
	
	// 通过if...else...执行父子进程的特有代码
	if (pd > 0)
	{
		// 清空缓冲区内存
		memset(buf, 0, sizeof(buf));
		// 读取文件内容
		read(fd, buf, sizeof(buf));
		// 将读取到的内容显示到屏幕上
		printf("%s", buf);
		printf("father fd = %d\n", fd);
		close(fd);	
	}
	else
	{
		printf("son fd = %d\n", fd);
		close(fd);	
	}
	
	return 0;
}
           

编译并执行程序:

[[email protected] mycode]$ make
gcc -Wall -o main.o -c main.c
gcc -o main main.o
-----------------------------OK-----------------------
[[email protected] mycode]$ main
This is some words.
father fd = 3
[[email protected] mycode]$ son fd = 3
           

程序3:在子进程中读取文件信息。

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

int main(int argc, char *args[])
{
	// 打开文件描述符:在fork之前打开才能让父进程和子进程共享
	int fd = open("a.txt", O_RDONLY);
	// 判断文件打开是否成功
	if (fd == -1)
	{
		printf("File open failed : %s\n", strerror(errno));
	}
	
	// 定义读取文件的缓冲区
	char buf[1024];
	
	// 执行fork函数
	pid_t pd = fork();
	// 判断fork是否成功
	if (pd == -1)
	{
		printf("fork failed : %s\n", strerror(errno));
	}
	
	// 通过if...else...执行父子进程的特有代码
	if (pd > 0)
	{		
		printf("father fd = %d\n", fd);
		close(fd);	
	}
	else
	{
		// 清空缓冲区内存
		memset(buf, 0, sizeof(buf));
		// 读取文件内容
		read(fd, buf, sizeof(buf));
		// 将读取到的内容显示到屏幕上
		printf("%s", buf);
		printf("son fd = %d\n", fd);
		close(fd);	
	}
	
	return 0;
}
           

编译并执行程序:

[[email protected] mycode]$ make
gcc -Wall -o main.o -c main.c
gcc -o main main.o
-----------------------------OK-----------------------
[[email protected] mycode]$ main
father fd = 3
[[email protected] mycode]$ This is some words.
son fd = 3
           

PS:根据传智播客视频学习整理得出。

继续阅读