System call in operating systems

In summary: A system call is a specific request for a service from the operating system, whereas an API is a more general term that refers to a set of protocols and tools for building software applications. In summary, the system calls are the low-level interface between the application and the operating system, while APIs are higher-level interfaces that provide access to system calls and other services to make application development easier.
  • #1
Avichal
295
0
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:
#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?
 
Technology news on Phys.org
  • #2
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.
 
  • #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.
 
  • #4
Avichal said:
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.
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.
Avichal said:
I guess my confusions lies in the difference between statement, system call and API.
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.
 

Related to System call in operating systems

What is a system call?

A system call is a programmatic request made by a process to the operating system for a specific service or resource, such as accessing hardware, executing a file, or creating a new process.

What is the purpose of a system call?

The purpose of a system call is to provide a safe and structured way for processes to interact with the operating system, which acts as an intermediary between the hardware and the user programs. This allows for efficient and controlled access to system resources.

How does a system call work?

When a process makes a system call, it first switches to kernel mode, which gives it access to privileged instructions and system resources. The operating system then performs the requested task and returns control to the process in user mode.

What are some common types of system calls?

Some common types of system calls include process control, file management, device management, information maintenance, and communication. Examples of specific system calls within these categories include fork(), open(), read(), ioctl(), and getpid().

What are the benefits of using system calls?

Using system calls allows for the development of portable and efficient code, as the same system call can be used on different operating systems. It also provides a secure way for processes to access system resources, preventing unauthorized access and ensuring stability of the system.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
4
Views
916
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
696
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
917
  • Programming and Computer Science
Replies
5
Views
950
  • Programming and Computer Science
Replies
5
Views
3K
Back
Top