当前位置:网站首页>Introduction to system programming
Introduction to system programming
2022-06-30 04:25:00 【Tra220123】
One 、 What is system programming ?
System programming is the use of system calls system call Programming
Two 、 System programming features ?
1. Unable to cross platform (OS)
2. Slow speed , Switching from user space to internal control space takes time
3. It's much lower , The interface is more complex
3、 ... and 、Linux Programming
1. Standard library functions and system calls
①fopen(3)
fopen(2) Open the specified file , Returns a file descriptor ( It's just one. int Type number ), Allocate one FILE Structure , It contains the descriptor of the file ,I/O Buffer and current read / write location , Go back to this FILE The address of the structure .
②fgetc(3)
Through the introduction of FILE * Parameter to find the descriptor of the file 、I/O Buffer and current read / write location , Judge whether or not from I/O Next character read in buffer , If you can read it, you can directly return this character , Otherwise, call read(2), Pass in the file descriptor , Let the kernel read the data of this file to I/O buffer , Then return to the next character .
③fputc(3)
Judge the file I/O Whether the buffer has space for another character , If there is space, save it directly in I/O Buffer and return , If I/O Call when the buffer is full write(2), Let the kernel I/O The contents of the buffer are written back to the file .
#include <stdio.h>
int main(void) {
int i;
for (int i = 0; i < 1025; i++) {
fputc('A', stdout);
}
return 0;
}
④fclose(4)
If I/O There is still data in the buffer that has not been written back to the file , Just call write(2) Write back the file , And then call close(2) Close file , Release FILE Structure and I/O buffer .
2. Standard library functions and system calls
Each process is in Linux There is one in the kernel task_struct Structure to maintain process related information , Called process control block (PCB,Process Control Block).taks_struct There is a pointer to files_struct Structure , It is called descriptor table , Each of these entries contains a pointer to an open file , User programs cannot directly access the descriptor table of files in the kernel , Only the index of the file descriptor table can be used , These indexes are called file descriptors , use int Type variable saving .
Handle idea .
Three files will be opened automatically when the program starts :
① The standard input ② standard output ③ Standard error output .
stay C Used separately in the standard library FILE * The pointer stdin,stdout and stderr Express . The descriptors of these three files are 0,1,2, Save in the corresponding FILE In the structure . The header file unistd.h There is a macro definition of if in to represent this file descriptor :
#include <stdio.h>
#include <unistd.h>
int main(void) {
printf("%d\n",STDIN_FILENO);
printf("%d\n",STDOUT_FILENO);
printf("%d\n",STDERR_FILENO);
return 0;
}
边栏推荐
- Ora-00907: missing right parenthesis problem supplement
- 基于海康EhomeDemo工具排查公网部署出现的视频播放异常问题
- Quick sort & merge sort
- Use of thread pool
- What is the difference between synchronized and lock
- el-upload上傳文件(手動上傳,自動上傳,上傳進度)
- 【WEBRTC】ADM: rtc_ include_ internal_ audio_ Device triggers RTC_ Dcheck (ADM) assertion
- El upload Upload file (Manual upload, Automatic upload, upload progress)
- 487-3279(POJ1002)
- Maya Calendar(POJ1008)
猜你喜欢
How to solve the problem of link hyperlinks when trying to link the database?
在大厂外包呆了三年,颠覆了我的认知!
Day 12 advanced programming techniques
How the FortiGate firewall rejects a port by using the local in policy policy
Myrpc version 2
el-upload上传文件(手动上传,自动上传,上传进度)
《机器人SLAM导航核心技术与实战》第1季:第0章_SLAM发展综述
Es2017 key summary
Myrpc version 6
两个月拿到N个offer,什么难搞的面试官在我这里都不算事
随机推荐
Redis cache avalanche, breakdown and penetration
Es2018 key summary
Error in conditional filter (if) syntax in sum function in SQL Server2005
QT creator 8 beta2 release
Everyone, Flink 1.13.6, mysql-cdc2.2.0, the datetime (6) class extracted
各位大佬,flink 1.13.6,mysql-cdc2.2.0,抽取上来的datetime(6)类
Es2016 key summary
FortiGate firewall modifies the default timeout of a session
Myrpc version 1
JS deconstruction assignment
Default value of JS parameter
Named pipes for interprocess communication
Qt Creator 8 Beta2发布
Qt 6.3.1Conan软件包发布
Five methods to clear floating and their advantages and disadvantages
Anonymous pipeline for interprocess communication
FortiGate firewall filters the specified session and cleans it up
输入输出及中断技术——微机第六章学习笔记
网络层详解
Implementation steps of dynamic proxy