Reputation: 125
I'm currently writing a C program and one of the constraints is that I cannot invoke external programs using system
. Instead, I need to work within the idiom of the language using system calls from within the C/C++ library. I'm having some troubles understanding the difference between "system" calls and "C/C++ system" calls.
Is system
simply platform dependent while "C system" calls builds ontop of system
and automatically changes its execution based on the platform being used?
Hope my question is clear. Thanks in advance!
Upvotes: 9
Views: 32384
Reputation: 42889
Linux-based operating systems expose functionality in two ways:
For example, to create a directory:
Using the shell, the mkdir
command is used, see http://linux.die.net/man/1/mkdir.
The system
C function invokes a shell to call such a command:
system("mkdir foo");
The corresponding system call is also called mkdir
, now see http://linux.die.net/man/2/mkdir instead.
It is used directly in C like this:
mkdir("foo", 0755);
The benefit of using the latter call is that it is easier to check for error conditions, and that no forking takes place to delegate the work to a subprocess, which makes this solution faster and lighter in memory usage, among other things.
Upvotes: 12
Reputation: 104514
The system function itself basically spawns a shell process and launches that program as if the user had just typed it from the command line himself.
"System calls" in the other sense are what the runtime libraries do to invoke the operating system to do something a program code couldn't do on it's own to. For example: opening a file, starting another process, and any type of I/O operations. On Linux, most of these system calls are exposed as C function APIs that your program can invoke do these operations (e.g. open, read, etc...). All the Linux system calls are listed on the manual page here: http://man7.org/linux/man-pages/man2/syscalls.2.html
Upvotes: 3
Reputation: 409176
The term "system call" is for the operating systems native functions, like pipe
or fork
or write
(on POSIX platforms like Linux), it has nothing to do with the function system
. Then you have the standard library which is specified in the C (or C++) specifications, and usually builds upon the operating systems native "system calls".
Read e.g. this Wikipedia "system call" article, or this about standard libraries for more information.
Also, no operating system call, or C (or C++) standard library function actuall calls the system
function, in fact the system
function is implemented using lower-level system calls (like fork
and wait
on Linux). The system
function is part of the standard library in C and C++.
Upvotes: 6