Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the relationship between POSIX and the C language?

Tags:

c

linux

posix

I understand that the C language is an ISO standard, and I can see from Wikipedia that the standard includes 29 header files, and that conforming to these header files, a C application is theoretically 'portable'.

In practice, however, I recently tried doing a tutorial on a simple C HTTP server that uses header files that aren't part of the C standard. So in this case, the simplest of applications that I can think of - a C application comprising a single int main(void) function, and that is less than 100 lines, with the aim of listening on a network interface goes beyond the C standard?

In this case what is the relationship between the C language as a specification and (assuming I'm writing an application for Linux) the POSIX specification as a language?

As far as I can tell, "man7.org" provides a list of the C header files that define the API of all Unix/Linux systems (I'm assuming this is the same as 'POSIX') systems, as well as a list of system calls for the Linux platform.

This includes 82 header files, of which the 29 C standard library headers are a subset, and some 10 000 system calls (at least I assume that everything in this list that is NOT a head file is a system call).

I would assume that any reasonably functional program written in C would go beyond the standard library and make use of OS specific header files. Would it not be more accurate to say that programming an application to run on Linux would actually be "POSIX programming"?

I guess it would also be possible to stick to the standard library, and define custom header files for portable logic implementation across POSIX & non-POSIX systems (including platform-specific assembly routines). Is this ever done?

like image 636
Zach Smith Avatar asked May 17 '19 14:05

Zach Smith


2 Answers

POSIX is not a specification for a language, it is a specification for an operating system, just one part of which is the wider C library specification and additional restrictions on to how the C language itself needs to be implemented on such operating systems.

There are many popular cross-platform libraries. One popular library that concerns the areas that the POSIX C specification is mostly concerned with is the Apache Portable Runtime:

The mission of the Apache Portable Runtime (APR) project is to create and maintain software libraries that provide a predictable and consistent interface to underlying platform-specific implementations. The primary goal is to provide an API to which software developers may code and be assured of predictable if not identical behaviour regardless of the platform on which their software is built, relieving them of the need to code special-case conditions to work around or take advantage of platform-specific deficiencies or features.

APR includes things like the sockets and threads and processes and can be used to compile the same application for various operating systems - many unix-like ones and Windows - with minimal changes.

like image 138

POSIX is not a standard but a family of standards specifying an entire operating system.

The POSIX C standard is a superset of the standard C library, the relationship between these two is well described in this other question

like image 21
istepaniuk Avatar answered Nov 14 '22 20:11

istepaniuk