A system call is a mechanism that allows user-level programs to request services from the operating system kernel. It provides a way for programs to interact with the underlying hardware and resources, such as I/O devices, memory, and processors, that are managed by the operating system.
There are generally five categories of system calls:
-
Process Control: These system calls are used to create, terminate, and manage processes. Examples include fork(), exec(), wait(), and exit().
-
File Management: These system calls are used to create, delete, read, and write files. Examples include open(), close(), read(), write(), and unlink().
-
Device Management: These system calls are used to manage I/O devices such as printers, disks, and terminals. Examples include ioctl(), read(), write(), and open().
-
Information Maintenance: These system calls are used to retrieve and update information about the system and its resources. Examples include time(), getpid(), and getuid().
-
Communication: These system calls are used to establish communication between processes and to synchronize their activities. Examples include pipe(), msgget(), and semop().
Here are some examples of each category of system calls:
-
Process Control: The fork() system call creates a new process that is a copy of the calling process. The exec() system call replaces the current process image with a new process image, while the wait() system call waits for a child process to terminate. The exit() system call terminates the calling process.
-
File Management: The open() system call is used to open a file and obtain a file descriptor that can be used to read from or write to the file. The close() system call closes a file descriptor, while the read() and write() system calls are used to read from or write to a file.
-
Device Management: The ioctl() system call is used to perform device-specific operations on an open file descriptor, such as changing the baud rate of a serial port. The read() and write() system calls are also used for I/O device management.
-
Information Maintenance: The time() system call is used to retrieve the current time in seconds since the Unix epoch (January 1, 1970). The getpid() system call returns the process ID of the calling process, while the getuid() system call returns the user ID of the calling process.
-
Communication: The pipe() system call creates a pipe, which is a unidirectional communication channel that can be used to pass data between processes. The msgget() system call creates a message queue, while the semop() system call is used to perform semaphore operations to synchronize access to shared resources.