Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

System call in operating systems

  1. Sep 1, 2015 #1
    In an operating system, system call is an interface to provide services to the user. Services include I/O operations, process management etc.
    Further, the C library provides me API above these system calls to make life easier. So printf, scanf internally use these system calls in the end.
    Q.) Am I right till now?

    So when I write a program say something like this:
    Code (C):
    #include <stdio.h>

    int main()
        int x = 1;
        int y = 2;
        printf("%d\n", x+y);
        return 0;
    I understand that printf is an API that helps me display stuff on the screen. Internally it uses some API.
    But the lines "int x = 1" and "int y = 2", these also help me allocate memory and then assign some value to that memory. So in a way it is providing me service to access memory.
    Q.) So is it also an API or a system call?
  2. jcsd
  3. Sep 1, 2015 #2


    User Avatar
    Science Advisor
    Homework Helper
    2017 Award

    Hi Avichal,

    Before your program runs, a compiler and linker (or an interpreter) go to work to prepare the working area, do the allocations etcetera. So at run time there is no longer a statement int x = 1 executed. All that happens is that the printf service gets passed the address of x .

    API stands for application programming Interface. Ususally we mean something at a higher level than hard-core system services (more oriented towards the application).
    But a lot of it boils down to procedure calls, for the one as well as for the other.
  4. Sep 1, 2015 #3
    The statement "int x = 1" allocates some memory and then assigns value 1 to it. This will of course require some system call to access the memory. So in a way, the statement "int x = 1" acts as a simpler way to access memory and assigning some value. This is similar to what an API does.
    So isn't the statement "int x = 1" an API in a way.

    I guess my confusions lies in the difference between statement, system call and API.
  5. Sep 1, 2015 #4


    Staff: Mentor

    No. After your program has been successfully compiled and linked, the executable that is produced contains startup code that executes before the first statement of your program is executed. Part of what happens in the startup code, I believe, is that the operating system makes several chunks of memory available to your program, such as stack memory and heap memory. In addition, programs use CPU registers, which can be considered another kind of memory.

    The term "statement" can be broken down into several types, including declaration statements (e.g., int x = 1;), assignment statements (e.g., x = 3;), control statements (e.g., if (x > 2) ... ), function statements (e.g., printf("Done"); ), and others.

    Function statements, such as the call to printf() above, can use standard library functions such as printf(), scanf(), sqrt(), and so on, or can use functions that you define in your code. These functions can be considered APIs, but assignment statements, control statements, aren't considered APIs, since they are basic constituents of the programming language in question.

    A declaration and initialization statement (such as int x = 1;) doesn't require any system calls, I don't believe. Depending on where in the program this statement is, an appropriate sized block of memory in static memory or automatic (i.e., stack) memory is set aside, and is associated with the variable name, x. A string of bytes of the appropriate size containing the value 1 is copied to that memory location using nothing more complicated than a MOV instruction.
    A typical C program is an application that operates in user mode. These kinds of programs can make use of system services by using the sys() standard library function. Device drivers, on the other hand, sometimes need to access kernel memory space. To do this, the drivers need to use system APIs to perform such tasks as creating and opening a file, and reading from or writing to a file.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook