What is system call in Operating System?

A system call is a request made by a program to the operating system to perform a specific task. It is the way that a program requests a service from the kernel (the core of the operating system) and it is typically used to access system resources, such as memory or the file system.

For example, the system call open() is used to open a file. In the following C code, the open() function is used to open the file "example.txt" for reading:

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int fd; fd = open("example.txt", O_RDONLY); // do something with the file descriptor return 0; }

The open() function takes two arguments: the name of the file to open and the mode in which to open the file (in this case, O_RDONLY for read-only). The function returns a file descriptor, which is a small integer that the program can use to refer to the file in subsequent system calls.

Another common system call is read(). The following example reads up to 100 bytes from the file that was opened in the previous example and stores them in a buffer called buf:

char buf[100]; int n; n = read(fd, buf, 100);

The read() function takes three arguments: the file descriptor, a pointer to the buffer where the read data will be stored, and the number of bytes to read. The function returns the number of bytes that were actually read.

As you see these are the example of System call for file management, Another example include System calls for process management like fork(), wait() which will allow you to create and manage processess. And in general lot of other system calls exists like for networking, memory management and other hardware management.

Post a Comment

0 Comments