ANSI C vs POSIX

can somebody explain about the ANSI C vs POSIX. say i was using open and fopen, i know that open is POSIX, and fopen is ANSI C. i read that that POSIX is a system call and ANSI C is like a standard library function. wouldn't the fopen function has to call on open function anyway to open any kind of file? what's the difference between using open and fopen?
I guess i'm not sure what system call mean.
any article about the difference between the two will be very helpful.
thanks.

System calls (open, creat, unlink, read etc) as the name suggests are calls to the system - in case of UNIX system, the UNIX kernel...

Part of the kernel consists of a collection of low level procedures through which processes can access resources. These procedures are called system calls and they are the primary means for a program to interact with other parts of the system. System calls are listed in section 2 of the manual pages. Any time that a program wishes to do anything beyond its own process environment, it must use system calls...

Many programs use (and must use sometimes) system calls... you can see what system calls are made by a common UNIX program using "trace" command (strace on linux), eg. "trace ls"..

since system calls are the only way to "talk" to the kernel, even the library functions doing system interactions also have to use them.. the purpose of library functions is two fold...

  • Because they form the lowest level of interaction, system calls are very simplistic and generic. It would be very tedious to have to write all of your programs using only system calls. Thus procedures and functions (like fopen) for the most common tasks have already been written and stored in libraries - for example fopen() for open(), scanf(), getchar() for read().

  • system calls are platform / implementation specific, also you are not assured to find the same naming for system calls on all platforms... but the ANSI standard library functions assure you to have the same functional interface on every platform - UNIX or otherwise...

Cheers!
Vishnu.

Ansi C has to work just about everywhere. So it assumes only a simple view of its environment. Posix on the other hand assumes a very powerful operating system.

As one example, look at signals. With posix you can send a signal to another process via the kill() system call. You specify which process gets the signal by supplying a pid. Ansi c only has a wimpy raise() which lets a process send a signal to itself. This is because Ansi C does not assume that it is even possible to run several processes at once. You won't find a pid in any Ansi C call. Posix demands multiple processes. Ansi C does not.