9

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!

5
  • Could you give an example of an external program that you want to invoke? Commented Nov 8, 2015 at 20:25
  • Do you mean fread and fopen or rather open and read? Commented Nov 8, 2015 at 20:27
  • 1
    Yes, system just spawn a shell command to start any binary (or shell builtin) that you ask for. A system call is a call to a "feature" of the kernel, feature that kernel is the only one able to perform (i.e. "really" accessing hardware). Of course a program spawned by system can/will use system calls. I guess here that the goal is to force you to do it all, not relying on an existing binary that will do it for you. Commented Nov 8, 2015 at 20:28
  • @cad open was the one I had in mind but it was more of a general question. Commented Nov 8, 2015 at 20:34
  • Plausibly related, but not exactly the same question: stackoverflow.com/questions/28137628/alternative-to-system/… Commented Nov 8, 2015 at 20:35

3 Answers 3

13

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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

Comments

3

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

2 Comments

I think this clarifies a lot for me. In otherwords, if I perform a system call such as open/read through the C library, it would still be considered 'within the language' correct?
Yes, but check with the person who gave you this assignment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.