Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System calls vs C/C++ system calls

Tags:

c++

c

linux

system

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!

like image 504
Geebs Avatar asked Nov 08 '15 20:11

Geebs


People also ask

What is the difference between system call and system program?

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.

What is a system call in C++?

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".

What happens when a system call is called?

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.

What is the use of system call in Linux?

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.


2 Answers

Linux-based operating systems expose functionality in two ways:

  • command-line tools through a shell
  • system calls through the C language

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.

like image 155
SirDarius Avatar answered Oct 07 '22 04:10

SirDarius


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++.

like image 5
Some programmer dude Avatar answered Oct 07 '22 06:10

Some programmer dude