Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between C standard library and C POSIX library

I'm a little confused by "C standard lib" and "C POSIX lib", because I found that, many header files defined in "C POSIX lib" are also part of "C standard lib".

So, I assume that, "C standard lib" is a lib defined by ANSI C organization, and there are different implementation on different platforms (Win32/Unix-like), and "C POSIX lib" is just a implementation for "C standard lib" on Unix-like OSes, right?

But "C POSIX lib" contains some headers not specified in "C standard lib", such as <sys/types.h>, <sys/wait.h>, and <pthread.h>.

Take <pthread.h> as an example, I presume its "C standard lib" counterpart is <threads.h>, then if I want to write a multi-threaded program on Linux, which header file should I include, <pthread.h> or <threads.h>?

like image 444
Alcott Avatar asked Feb 21 '12 11:02

Alcott


People also ask

What are the 3 main libraries of C ++?

The Diagnostics Library. The General Utilities Library.

What is standard library function in C?

C Standard library functions or simply C Library functions are inbuilt functions in C programming. The prototype and data definitions of these functions are present in their respective header files. To use these functions we need to include the header file in our program.

Is the standard C library an API?

The application programming interface (API) of the C standard library is declared in a number of header files. Each header file contains one or more function declarations, data type definitions, and macros.


2 Answers

POSIX is a superset of the standard C library, and it's important to note that it defers to it. If C and POSIX is ever in conflict, C wins.

Sockets, file descriptors, shared memory etc. are all part of POSIX, but do not exist in the C library.

pthread.h is used for POSIX threads and threads.h is a new header for C11 and is part of the C library. Perhaps pthreads will be deprecated sometime in the future in favor of the C ones, however you probably can't count on C11 to have widespread deployment yet. Therefore if you want portability you should prefer pthreads for now. If portability is not a concern, and you have C11 threads available, you should probably use those.

like image 161
Per Johansson Avatar answered Sep 20 '22 04:09

Per Johansson


The C POSIX library is a specification of a C standard library for POSIX systems. It was developed at the same time as the ANSI C standard. Some effort was made to make POSIX compatible with standard C; POSIX includes additional functions to those introduced in standard C.

like image 37
L.ppt Avatar answered Sep 22 '22 04:09

L.ppt