当前位置:网站首页>The C programming language (version 2) notes / 8 UNIX system interface / 8.1 file descriptor
The C programming language (version 2) notes / 8 UNIX system interface / 8.1 file descriptor
2022-06-12 16:24:00 【M rookie M】
8 UNIX system interface
UNIX The operating system provides services through a series of system calls
These system calls are actually functions within the operating system , They can be called by user programs
This chapter will show you how to C Some important system calls are used in language programs
If the reader is using UNIX, This chapter will be of direct help to you
This is because , We often need to rely on system calls for maximum efficiency , Or access some functions that are not available in the standard library
however , Even if the reader is using on other operating systems C Language , The examples in this chapter will also help you understand C Have a deeper understanding of language programming
The code in different systems is similar , There are only differences in some details
because ANSI C The standard library is based on UNIX Based on the system , therefore , Learning the procedures in this chapter will also contribute to a better understanding of the standard library
The contents of this chapter include 3 Major parts , Input / Output 、 File system and storage allocation
among , The contents of the first two parts require the reader to understand UNIX Have a certain understanding of the external characteristics of the system
The first 7 Input introduced in Chapter / The output interface is the same for any operating system
In any particular system , The implementation of standard library functions must be realized through the functions provided by the host system
The next few sections will cover UNIX System calls in the system for input and output , It also introduces how to implement the standard library through them
8.1 File descriptor
stay UNIX Operating system , All the peripherals ( Including keyboard and monitor ) Are regarded as files in the file system
therefore , All the input / Output should be done by reading or writing files
in other words , All communication between peripheral devices and programs can be handled through a single interface
Usually , Before reading or writing a file , This intention must first be notified to the system , This process is called opening a file
If it's writing a file , You may need to create the file first , You may also need to discard the pre-existing contents of the file
The system checks your power ( Does the file exist ? Whether you have access to it ?)
If everything goes well , The operating system will return a small nonnegative integer to the program , This integer is called the file descriptor
Input to the file at any time / The output is identified by the file descriptor , Instead of identifying the file by its name
A file descriptor is similar to a file pointer or... In a standard library MS-DOS File handle in
The system is responsible for maintaining all the information of the opened file , User programs can only reference files through file descriptors
Because most of the input / Output is achieved through keyboard and display , For convenience ,UNIX Special arrangements have been made for this
When Command interpreter ( namely shell) When running a program
Will open the 3 File , The corresponding file descriptors are 0、1、2, The standard input is represented in turn , Standard output and standard error
If the program from the file 0 Chinese Reading , Yes 1 and 2 Write , You can lose / Output without having to worry about opening the file
The user of the program can use < and > Redirector's I/O:
prog <infile >outfile
In this case ,shell Put the file descriptor 0 and 1 The default assignment of is changed to the specified file
Usually , File descriptor 2 Still associated with the display , such , The error message will be output to the display
Pipeline related inputs / The output has similar characteristics
In any case , The change of file assignment is not completed by the program , But by the shell Accomplished
As long as the program uses files 0 As input , file 1 and 2 As the output , It won't know where the program's input comes from , And output to where
边栏推荐
- 【DSP视频教程】DSP视频教程第8期:DSP库三角函数,C库三角函数和硬件三角函数的性能比较,以及与Matlab的精度比较(2022-06-04)
- acwing 801. Number of 1 in binary (bit operation)
- 深入理解 Go Modules 的 go.mod 與 go.sum
- MySQL - server configuration related problems
- Super detailed dry goods! Docker+pxc+haproxy build a MySQL Cluster with high availability and strong consistency
- acwing 790. The cubic root of a number (floating-point number in half)
- What is fintech? How fintech can help small businesses succeed
- [thinking about the process of structure optimization] how to build the evaluation ability of the impact of technical solutions
- The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.2 低级 I/O(read 和 write)
- Global and Chinese markets of automatic glue applicators 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

批量--04---移动构件

大规模实时分位数计算——Quantile Sketches 简史

acwing 801. Number of 1 in binary (bit operation)

MySQL - server configuration related problems

超详细干货!Docker+PXC+Haproxy搭建高可用强一致性的MySQL集群

acwing 803. 区间合并

acwing 803. Interval merging

Multimix: small amount of supervision from medical images, interpretable multi task learning

acwing 801. 二进制中1的个数(位运算)
![[browser principle] variable promotion](/img/19/f6b26d97c6024893a21dd40e2bbc47.jpg)
[browser principle] variable promotion
随机推荐
Batch --04--- moving components
acwing 802. 区间和 (离散化)
Acwing 797 differential
使用 .NET 升级助手将NET Core 3.1项目升级为.NET 6
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.6 实例(目录列表)
Gopher to rust hot eye grammar ranking
Postgresql源码(53)plpgsql语法解析关键流程、函数分析
MySQL面试整理
HEMA is the best representative of future retail
34- [go] golang channel knowledge points
acwing795 前缀和(一维)
Browsercontext class of puppeter
Cookie 和 Session
acwing 高精度乘法
统计机器学习代码合集
Project training of Shandong University rendering engine system (II)
Analysis of global and Chinese shipbuilding market in 2021: the global shipbuilding new orders reached 119.85 million dwt, with China, Japan and South Korea accounting for 96.58%[figure]
acwing 802. Interval sum (discretization)
[DSP video tutorial] DSP video tutorial Issue 8: performance comparison of DSP library trigonometric function, C library trigonometric function and hardware trigonometric function, and accuracy compar
Sha6 of D to large integer