当前位置:网站首页>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;
}边栏推荐
- Configure specific source IP in SLA detection of FortiGate sdwan
- lego_loam 代码阅读与总结
- Technology sharing | broadcast function design in integrated dispatching
- Code cloud fatal: authentication failed for
- JS generator
- A solution to the problem of "couldn't open file /mnt/repodata/repomd.xml"
- JS proxy
- win10系统使用浏览器下载后,内容无故移动或删除
- The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure
- Clients accessing the daytime service (TCP)
猜你喜欢

Myrpc version 2
![Blue Bridge Cup: magic cube rotation [Vocational group]](/img/ba/aeae2744f3aaa1052b5af452f990e2.jpg)
Blue Bridge Cup: magic cube rotation [Vocational group]

尝试链接数据库时出现链接超时报错,如何解决?

Share an example of a simple MapReduce method using a virtual machine

Es2017 key summary

基于SSM框架茶叶商城系统【项目源码+数据库脚本+报告】

Sql语句遇到的错误,求解

Configure specific source IP in SLA detection of FortiGate sdwan

深度融合云平台,对象存储界的“学霸”ObjectScale来了

Anonymous pipeline for interprocess communication
随机推荐
Error in conditional filter (if) syntax in sum function in SQL Server2005
FortiGate firewall configuration link detection link monitor and status query
Iterator of JS
JS import and export
Error Nova missingauthplugin: an auth plugin is required to determine endpoint URL
Detailed explanation of data link layer
数据链路层详解
【WEBRTC】ADM: rtc_include_internal_audio_device 触发 RTC_DCHECK(adm) 断言
基于servlet+jsp+mysql实现的工资管理系统【源码+数据库】
SQL server2005中SUM函数中条件筛选(IF)语法报错
Day 11 script and game AI
SQL error caused by entity class: Oracle "ora-00904" error: possible case of invalid identifier
SQLyog导入数据库时报错,求帮解决!
Node red series (28): communication with Siemens PLC based on OPC UA node
Junior students summarize JS advanced interview questions
Qt Creator 8 Beta2发布
Thinkphp5 implements import function
Blue Bridge Cup: magic cube rotation [Vocational group]
RPC correction based on arcpy API
MySQL DDL change