API, ABI, and system call
Tags: linux, api, abi, systemcalls
Published: 2019-09-22 00:51:01

---
title: API, ABI, and system call
published: true
description: This article describe what API, ABI, and system calls are, and the difference between these words
tags: Linux,API,ABI,system calls
---

# Preface

This article a brief introduction of what API, ABI, and system calls are, and the difference between these words.

These words are well known among the engineers used to the low-level programming languages like C, which must consider the binary expression of programs. However, recently, most developers usually don't know this kind of knowledge since they use high-level languages like Python in general.

I guess API means Web API for most developers. In addition, they've heard ABI and system calls, but don't know what these are. To ease this situation, I'll try to explain the meaning of these words as easy as possible. 

## What is API?

API is the acronym of Application Programming Interface. It's a specification of the functions (includes methods in OOPL) at a source code level, what is this function, what kind of arguments should be set, and what is the return value. It doesn't in charge of the binary expression of these.

The example is Web API as I described in the first section. It defines what URL we should access and what kind of data should be set to get services from websites. In these days, both API and Web API are used without distinguishing these, now we understood Web API is the API of web services. In other words, API is the broader word than Web API.

I'll show one more example of API with the following C source code.

```c
int plus(int x, int y) {
  return x + y;
}

int main(void) {
  int a = 1;
  int b = 2;
  ..
  plus(a, b):
  ..
}
```

In this source code, the API of plus() function is as follows.

- Returns sum of the first argument and the second one
- The type of both argument and the return value is int

Here API doesn't care the more detail like how many bytes does int type is.

## What is ABI?

ABI comes from Application Binary Interface. It defines the binary expression of the functions and the data. It defines the more detailed specification than API. It's in charge of how many bytes do an argument takes and where the data should be set, for example, the top of stack or CPU's registers. In some cases, it also defines the endian of the data.

If you have a basic knowledge about x86_64 CPU architecture, please also see the following URL.

- [`calling convention` page of Wikipedia](https://en.wikipedia.org/wiki/Calling_convention)
- [System V Application Binary Interface](https://www.uclibc.org/docs/psABI-x86_64.pdf) -> "3.2 Function Calling Sequence"

For example, You should consider ABI when calling binary library like C library from non-C languages

## What's the difference between API and system call

Understanding the difference between API and system call is a bit difficult. However, now you can understand it since you know what API and ABI are.

I'll explain the difference between POSIX C API, Linux's C API, and Linux's system calls. First of all, POSIX is the international standard for Unix like operating systems. POSIX API define many things like POSIX C API that is the C API to access the operating system services.

Linux's C API is the C API to access Linux's services. Linux tries to provide POSIX C API as much as possible to be compatible with POSIX C API. However, these are not the same as follows.

- Some of the functions in Linux's C API doesn't compatible with POSIX
- Linux doesn't provide some of POSIX C API
- Linux provides many functions which don't exist in POSIX

System calls are the interface of operating systems. Linux provides many system calls to developers and also provides many C APIs to use these system calls. In addition, essentially, we must execute special CPU instructions to use system calls with CPU dependent system call ABI. If you're interested in Linux's system call API, please refer to  [this document](https://www.uclibc.org/docs/psABI-x86_64.pdf)'s A.2.1 Calling Conventions. It defines the system call ABI of x86_64 CPUs.

It's interesting that there are many functions in Linux's C APIs that don't use the system calls that have the same name with these functions. For example, Linux provides `fork()` function, but this function is implemented by `clone()` system call.

# Conclusion

This article described the following things.

- What API, ABI, and system calls are
- What's the difference between these words
- What's the difference between POSIX C API, Linux's C API, and system calls

In these days, this kind of knowledge is not crucial for many developers. However, one day, there are situations that you must struggle with the low-layer programming. Then this article would help you very much.