天天看點

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:根據傳智播客視訊學習整理得出。

繼續閱讀