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!
The system call creates an interface between the user program and the services of the operating system. On the other hand, the system program defines the user interface of the operating system. The system program also creates a suitable environment for a program to develop and execute.
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".
Using this system call means that the file is no longer required by the program and so the buffers are flushed, the file metadata is updated and the file resources are de-allocated.
The user program uses the system call to connect two processes, two users or systems. This system call creates a mechanism that controls the process access to the resource of the system. These system programs are used for modifying the file stored on the hard disk or other storage devices.
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.
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++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With