Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between API and System Call

According to the text book " Operating Systems Concepts " 9th edition , pages 63-65 :

Behind the scenes, the functions that make up an API typically invoke the actual system calls on behalf of the application programmer.

And

For most programming languages, the run-time support system (a set of functions built into libraries included with a compiler) provides a systemcall interface that serves as the link to system calls made available by the operating system. The system-call interface intercepts function calls in the API and invokes the necessary system calls within the operating system. Typically, a number is associated with each system call, and the system-call interface maintains a table indexed according to these numbers. The system call interface then invokes the intended system call in the operating-system kernel and returns the status of the system call and any return values. The caller need know nothing about how the system call is implemented or what it does during execution. Rather, the caller need only obey the API and understand what the operating system will do as a result of the execution of that system call. Thus, most of the details of the operating-system interface are hidden from the programmer by the API and are managed by the run-time support library. The relationship between an API, the system-call interface, and the operating system is shown in Figure 2.6, which illustrates how the

enter image description here

The author then introduced this as an example :

enter image description here

So I just need to check my understanding. We actually use an API to access the actual system call(s), this means that the functions read(), write() and open() are not actual system calls, instead they invoke the system call(s) for us with the help of the system call interface ? Is that correct ? What confuses me is that many sources refer to read(), write() and open() as system call, but according to the textbook they are not the "actual" system calls, instead they are just API that invoke the "real" system calls. Please correct me if I am wrong.

like image 245
AAA Avatar asked Mar 13 '26 14:03

AAA


1 Answers

A system call is a specific kind of mechanism which switches the CPU from user mode into kernel mode, loads and saves a bunch of registers, and jumps to a pre-set address. It's similar to but not the same as a normal function call, which does not switch to kernel mode, saves and loads only the current instruction address, and jumps to any address you like. It serves a similar purpose as a function call, but it calls a function in the kernel, not a function in your program.

On x86_64-type CPUs there is the syscall instruction designed specifically for this purpose. As you can see from the linked page, it does indeed load and save a handful of registers and jump to a pre-set address by loading the RIP register. (Switching to kernel mode is part of loading the CS register, I think)

There are other ways to system calls could work, such as the software interrupt instruction int. Of course, you (the programmer) can't just pick one, unless you are programming an operating system; you have to use whichever one your operating system wants you to use. Just like you can't make up function addresses and call them with the call instruction - they have to be the addresses of functions.


"API" is a quite general term. It refers to simply a set of things a program can do, and how to do them. They may be system calls or function calls, but web developers also speak of web APIs, where you can do things by sending HTTP requests to specific addresses.

An API designed for C programs to use will consist of a bunch of functions; an API designed for C++ may consist of classes, namespaces, functions, templates and so on. An API designed for the web consists of HTTP addresses, headers and parameters.

Your book is talking about a C API which is made up of functions. Some of these functions make system calls. There's no syntax you can write in C that will make a system call, so instead, you make function calls to these functions like read, and the function read makes the system call.

We also call the system call read, because that makes sense, but to be pedantic, there is nothing in the system call that actually says it's called read. Linux knows that you want this system call because you put the number 0 in the RAX register before using the syscall instruction, not because of the word "read".

like image 183
user253751 Avatar answered Mar 15 '26 07:03

user253751



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!