[Linux] Talking about the relationship between C language library functions and system call interfaces

foreword

We give a hierarchical graph of such a computer:

insert image description here
It can be seen that the entire computer system is divided into hardware layer and software layer

In the software layer, we have the kernel layer and the user layer.

The operating system is a piece of software between hardware and user software, which manages various hardware from the bottom and provides services for the user software on the top. We can compare like this

user headmaster
operating system counselor
hardware student

If we want to manage the hardware well, we need someone who understands the hardware to help us manage the hardware, that is the operating system

However, in order to ensure security, the operating system will only open specific interfaces to people. Only through specific interfaces, people can perform specific management of hardware. Such interfaces are called系统调用接口

However, the system call interface is relatively complicated, and it is too annoying to use the system call interface to manage hardware every time. Therefore, some people with ulterior motives have carried out the system call interface 二次封装and encapsulated some more friendly, Humanized interface for people to call, for example:

  • C language library function
  • command line interpreter
  • Graphical interface

All in all, this hierarchy diagram explains it all:

insert image description here

Next, let's take the Linux operating system as an example to see how the system call interface and the secondary encapsulated C language library functions 读/写operate.

1. System call interface

1. Investigate the use of functions such as open, read, write, lseek, close, etc., and understand file descriptors
2. Requirements:
2.1 Use the code to open the "bite" file in the current path (if the file does not exist, create a file), and send the file to the file. Write "i like linux!" in it.
2.2 Read the contents of the file from the file and print it to the standard output; close the file descriptor

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main() {
    
    
  int fd = open("bite", O_WRONLY|O_CREAT, 0644);
  const char* buffer = "i like linux\n";
  write(fd, buffer, strlen(buffer));
  close(fd);
  fd = open("bite", O_RDONLY, 0644);
  char output[128] = {
    
    '\0'};
  read(fd, output, 127);
  write(1, output, strlen(output));
  close(fd);
  return 0;
}

insert image description here

2. C language library functions

1. Investigate the use of fopen, fread, fwrite, fseek, fclose and other functions
2. Requirements:
2.1 Use the code to open the "bite" file in the current path (if the file does not exist, create a file), and write "linux so" to the file easy!".
2.2 Read the contents of the file from the file and print it to the standard output; close the file stream pointer

#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main() {
    
    
  FILE* fp = fopen("bite", "w");
  const char* buffer = "linux so easy\n";
  fwrite(buffer, 1, strlen(buffer), fp);
  fclose(fp);
  fp = fopen("bite", "r");
  char output[128] = {
    
    0};
  fread(output, 1, 128, fp);
  fprintf(stdout, "%s", output);
  fclose(fp);
  return 0;
}

insert image description here

3. Summary

We can see that both the system call interface and the C language library functions can complete read and write operations. In general:

  1. From the development point of view, the operating system appears as a whole to the outside world, but it will expose some of its own interfaces for upper-level development. This part of the interface provided by the operating system is called system calls.
  2. In terms of use, system calls have relatively basic functions and relatively high requirements for users. Therefore, willing developers can appropriately encapsulate some system calls to form libraries. With libraries, it is very beneficial to upper-level users or The developer conducts secondary development.

insert image description here

Guess you like

Origin blog.csdn.net/m0_52640673/article/details/123172350