Based on the number of system calls the operating system is requesting, the interface between the operating system and the user programs is defined. To understand exactly what the operating system is doing, we need to look closely at this interface. The system calls provided by the interface vary from one operating system to another. In the following sections, we will examine some of the most commonly used POSIX system calls, or more specifically, the library procedures that make these system calls. POSIX has approximately 100 system calls. Some of them, the most important ones, will be listed below, the group for clarity in 4 categories. The system call is the fundamental interface between an application and the Linux kernel.
We will talk about the system calls: syscall, strace, and fcntl.
1. syscall
Name: syscall – indirect system call
To invoke a system call manually, you can use syscall. Syscall () launches the system call whose assembly language interfaces has the specified number with the specified arguments. Symbolic constants for system calls can be found in the header file < sys/syscall .h>. Return value: The invoked system call defines the returned value. In return, the return value in the case of success is 0, and the value -1 indicates an error, and an error code is stored in error.
2. strace
Name: strace – tracks system calls and signals
The strace command follows the execution of another program, listing any system call made by the program and any signals it receives. To track system calls and signals in a system, you only need to invoke strace, followed by the program and the arguments in the command line. For example, to track system calls initiated by the hostname command, use the command:% strace hostname.
In the resulting output, each line corresponds to a single system call for each call, its name is listed, followed by its arguments (or their abbreviations if they are very long) and the return value.
Where possible strace conveniently displays symbolic names in place of numeric values for arguments and return values, and displays the fields of the structures passed by a pointer to the system call. The output of the hostname strace shows the execve system call invoking hostname: execve (“/ bin / hostname”, [“hostname”], [/ * 49 vars * /]) = 0
The first argument is the name of the program to run; the second is the list of arguments, which consists of only one element; and the third is the list of variables. The next approximately 30 lines are part of the standard C library loading mechanism from a shared library file. Finally, there are other system calls that contribute to the functionality of the program in question.
3. fcntl
Locks and other file operations. The fcntl system call is an access point for multiple advanced file descriptor operations. The first argument of fcntl is an open file descriptor, and the second is an open value that indicates what operation is to be performed. For some operations, fcntl receives an additional argument. The most important operation of fcntl is blocking files. The fcntl system boot allows a program to place a read or write lock on a file, similar to mutex blocking. A read lock is placed on the reader’s descriptor of a file, and a write lock is placed on the write descriptor of the file. Several processes can place a write lock on the same file at the same time, but a single process can place a write lock on a file at a time, and the same file can not have both read and write blocks at the same time write lock.
Placing a lock on a file does not prevent other processes from opening that file, reading it, or writing in it unless they are also blocked by fcntl.
To place a lock on a file, you must first create and isolate a flock structure variable. The structure l_type field is set to F_RDLCK for a read lock or F_WRLCK for a write lock. Then fcntl is called by a descriptor, the operation code F_SETLCW, and a pointer to the struct flock variable. If another process has placed a lock that prevents the new lock from being blocked, fcntl locks until that lock is released.