Skip to Content
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
Explore Skills
Cloud Computing
Microsoft Azure
Amazon Web Services (AWS)
Google Cloud
Cloud Migration
Cloud Deployment
Cloud Platforms
Data Engineering
Data Warehouse
SQL
Apache Spark
Microsoft SQL Server
MySQL
Kafka
Data Lake
Streaming & Messaging
NoSQL Databases
Relational Databases
Data Science
Pandas
R
MATLAB
SAS
D3
Power BI
Tableau
Statistics
Exploratory Data Analysis
Data Visualization
AI & ML
Generative AI
Machine Learning
Artificial Intelligence (AI)
Deep Learning
Reinforcement Learning
Natural Language Processing
TensorFlow
Scikit-Learn
Hyperparameter Tuning
MLOps
Programming Languages
Java
JavaScript
Spring
Python
Go
C#
C++
C
Swift
Rust
Functional Programming
Software Architecture
Object-Oriented
Distributed Systems
Domain-Driven Design
Architectural Patterns
IT/Ops
Kubernetes
Docker
GitHub
Terraform
Continuous Delivery
Continuous Integration
Database Administration
Computer Networking
Operating Systems
IT Certifications
Security
Network Security
Application Security
Incident Response
Zero Trust Model
Disaster Recovery
Penetration Testing / Ethical Hacking
Governance
Malware
Security Architecture
Security Engineering
Security Certifications
Design
Web Design
Graphic Design
Interaction Design
Film & Video
User Experience (UX)
Design Process
Design Tools
Business
Agile
Project Management
Product Management
Marketing
Human Resources
Finance
Team Management
Business Strategy
Digital Transformation
Organizational Leadership
Soft Skills
Professional Communication
Emotional Intelligence
Presentation Skills
Innovation
Critical Thinking
Public Speaking
Collaboration
Personal Productivity
Confidence / Motivation
Features
All features
Verifiable skills
AI Academy
Courses
Certifications
Interactive learning
Live events
Superstreams
Answers
Insights reporting
Radar Blog
Buy Courses
Plans
Sign In
Try Now
O'Reilly Platform
book
LINUX系統程式設計 第二版
by
Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from
LINUX系統程式設計 第二版
檔案
I/O
|
49
關閉檔案
當程式使用完檔案描述器之後,它可以經由
close()
系統呼叫取消檔案描述器與相對應
檔案的映射關係:
#include <unistd.h>
int close (int fd);
叫用
close()
可以取消已開啟之檔案描述器
fd
的映射關係,以及讓行程與相對應檔案分
離。因為所指定的檔案描述器不再有效,所以核心可以釋放它,讓它得以再次成為後續
open()
或
creat()
呼叫的傳回值。
close()
呼叫會在執行成功時傳回
0
,以及在發生錯誤
時傳回
-1
並且為
errno
設定適當的值。它的用法很簡單:
if (close (fd) ==
−
1)
perror ("close");
請注意,當檔案已出清到磁碟時,開閉檔案的動作毫無作用。如果應用程式想要在關閉
檔案之前先把檔案提交給磁碟,它需要利用稍早在第
2
章〈同步
I/O
〉一節所討論到的
其中一個同步選項。
但是,關閉檔案會產生若干副作用。當參照到檔案的「最後一個已開啟之檔案描述器」
被關閉時,核心內部代表該檔案的資料結構會被釋放。當此資料結構被釋放時,與該檔
案相對應之
inode
於記憶體中的副本會被移除。如果記憶體中已無該
inode
之副本,該
inode
也會從記憶體中被釋放(核心為了效能的理由將該
inode
放在快取中,但是已無此
需要)。如果你從磁碟移除一個檔案的連結,但是移除該檔案的連結之前,它仍舊維持
開啟的狀態,它實際上並不會被移除,除非它被被關閉並且它的
inode
已被從記憶體中
移除。因此,叫用
close() ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial
You might also like
優雅的SciPy|Python科學研究的美學
Juan Nunez-Iglesias, Stéfan van der Walt, Harriet Dashnow
成為卓越程式設計師的38項必修法則
Pete Goodliffe
深入理解運算原理|從簡單的機器到無所不能的程式
Tom Stuart
C++语言导学(原书第2版)
本贾尼 斯特劳斯特鲁普
Publisher Resources
ISBN: 9789862769812