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 $@ -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;
}

編譯程序並執行:

[negivup@negivup mycode]$ make
gcc -Wall -o main.o -c main.c
gcc -o main main.o
-----------------------------OK-----------------------
[negivup@negivup mycode]$ ./main
father fd = 3
[negivup@negivup 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;
}

編譯並執行程序:

[negivup@negivup mycode]$ make
gcc -Wall -o main.o -c main.c
gcc -o main main.o
-----------------------------OK-----------------------
[negivup@negivup mycode]$ main
This is some words.
father fd = 3
[negivup@negivup 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;
}

編譯並執行程序:

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


PS:根據傳智播客視頻學習整理得出。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章