這是我在網上找到的原話:
一般來說,子行程呼叫close()不會發生什么。close()函式關閉檔案時,并不是在任何情況下都直接關閉檔案,而是找出struct file結構體中f_count成員,執行自減操作;直到f_count為0,才是真正的關閉檔案。這就是著名的技術——參考計數。
現在有下面的代碼
int main()
{
int fd = -1;
pid_t pid = -1;
fd = open("1.txt", O_RDWR | O_TRUNC);
pid = fork();
if (pid > 0){
printf("parent.\n");
write(fd, "hello\n", 6);}
if (pid == 0){
printf("child.\n");
write(fd, "world\n", 6);}
close(fd);
exit(0);
}
按照上面的說法,不管父子行程誰先close(fd),都不會影響另外行程中對檔案fd的寫操作,但為什么我執行檔案會得到只列印hello或者world的結果?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/82283.html
標籤:應用程序開發區
上一篇:高速線性能測驗
下一篇:求DTI批量預處理代碼~
