Learn about various system calls to help you become a Linux programmer

What is a system call?

A set of subroutines for implementing various system functions are set in the Linux kernel, called system calls. Users can invoke them in their own applications through system call commands. From a certain point of view, system calls and ordinary function calls are very similar. The only difference is that the system call is provided by the operating system kernel and runs in the core state; ordinary function calls are provided by the function library or the user, and run in the user mode.

With the Linux kernel also provides a number of C language libraries, these libraries on the system call some packaging and expansion, because these library functions and system calls are very close, so these functions are also commonly referred to as system calls.

Why use system calls?

In fact, many of the C language standard functions that we have become accustomed to, are implemented on the Linux platform by means of system calls. Therefore, if you want to have an in-depth understanding of the underlying principles of the system, it is an initial requirement to master various system calls. . Further, if we want to become a Linux programming expert, which is often referred to as Hacker, one of its hallmarks is to have a thorough understanding of various system calls.

Even if you remove the above reasons, in normal programming you will find that in many cases, system calls are a simple and effective way to achieve your ideas, so if possible you should try to master as many system calls as possible. The programming process brings unexpected help.

How does a system call work?

In general, processes cannot access the kernel. It cannot access the kernel's memory space or call kernel functions. The CPU hardware determines these (this is why it is called "protected mode"). System calls are an exception to these rules. The principle is that the process first fills the register with the appropriate value, and then calls a special instruction that jumps to a location in a pre-defined kernel (of course, this location is readable but not writeable by the user process). In the Intel CPU, this is achieved by interrupt 0x80. The hardware knows that once you jump to this location, you are not the user running in restricted mode, but the operating system kernel - so you can do whatever you want.

The kernel location to which the process can jump is called sysem_call. This process checks the system call number, which tells the kernel process which service to request. Then, it looks at the system call table (sys_call_table) to find the kernel function entry address that was called. Then, we call the function, wait for the return, do some system checks, and finally return to the process (or to other processes if the process runs out of time).

The specific process is shown in the figure below:

How to use system calls?

Let's look at an example first:

This is because in time.h, the time system call is actually implemented in the form of a library function, which saves us from calling the _syscall1 macro to get the function prototype.

Most system calls are implemented in a variety of C language libraries, so under normal circumstances, we can call system calls just like ordinary library functions. We only have the opportunity to use them in rare cases. To _syscall*() these macros.

Call performance issues

The system call needs to fall into the kernel space from the user space. After processing, it needs to return to the user space. In addition to the actual time-consuming of the system call service routine, the trap/return process and the system call handler (checking the system call table, storing/restoring the user's site) also need to spend some time, and these times add up to a system call. responding speed. The system call is not as bad as other user programs. It has very strict performance requirements because it needs to be executed in the kernel, so it requires the code to be concise and perform quickly as other kernel programs. Fortunately, Linux has incredible context switching speeds, making its access to the kernel optimized and efficient; and all Linux system call handlers and each system call itself is also very simple.

In most cases, Linux system call performance is acceptable, but for applications with very high performance requirements, they want to use the services of the system call, but they want to speed up the corresponding speed, avoid trapping / return and system The overhead incurred by calling the handler, so the system calls the service routine directly from the kernel, the best example is HTTPD—it calls the system call service routines such as sockets from the kernel to avoid overhead.

Linux system call list

· Process control

Fork creates a new process

Clone creates child processes on specified conditions

Execve run executable file

Exit abort the process

_exit abort the current process immediately

The maximum number of files that can be opened by the getdtablesize process

Getpgid Gets the specified process group ID number

Setpgid set the specified process group flag

Getpgrp Get current process group ID number

Setpgrp sets the current process group flag

Getpid Get the process ID number

Getppid Get the parent process identification number

Getpriority Get scheduling priority

Setpriority sets the scheduling priority

Modify_ldt local description table for the read/write process

Nanosleep makes the process sleep for a specified time

Nice change the priority of the time-sharing process

Pause hang process, wait signal

Personality sets the process run domain

Prctl performs specific operations on the process

Ptrace process trace

Sched_get_priority_max gets the upper limit of static priority

Sched_get_priority_min Get the lower limit of static priority

Sched_getparam Get process scheduling parameters

Sched_getscheduler gets the scheduling policy of the specified process

Sched_rr_get_interval Gets the time slice length of the real-time process scheduled by the RR algorithm

Sched_setparam set process scheduling parameters

Sched_setscheduler sets the scheduling policy and parameters for the specified process

The sched_yield process voluntarily leaves the processor and waits for itself to queue at the end of the queue

Vfork creates a child process for executing new programs, often used with execve etc.

Wait waits for the child process to terminate

Wait3 See wait

Waitpid waits for the specified child process to terminate

Wait4 See waitpid

Capget get process permissions

Capset setting process permissions

Getsid Get the meeting identification number

Setsid set meeting identification number

· File system control

1 file read and write operations

Fcntl file control

Open open file

Creat create new file

Close closes the file descriptor

Read read file

Write write file

Readv read data from file into buffer array

Writev writes data in buffer array to file

Pread read random files

Pwrite writes random files

Lseek move file pointer

_llseek moves file pointers in the 64-bit address space

Dup copy open file descriptor

Dup2 Copy the file description words according to the specified conditions

Flock file plus/unlock

Poll I/O multiplexing

Truncate truncated file

Ftruncate See truncate

Umask set file permissions mask

Fsync writes the portion of the file in memory to disk

2. File system operation

Access determines file accessibility

Chdir changes the current working directory

Fchdir See chdir

Chmod change file mode

Fchmod See chmod

Chown changes the owner or user group of a file

Fchown See chown

Lchown See chown

Chroot changes the root directory

Stat file status information

Lstat See stat

Fstat See stat

Statfs fetch file system information

Fstatfs See statfs

Readdir reads the directory entry

Getdents read directory entry

Mkdir create directory

Mknod creates an inode

Rmdir delete directory

Rename file renamed

Link Create link

Symlink creates symbolic links

Unlink delete link

Readlink reads the value of a symbolic link

Mount mount file system

Umount remove the file system

Ustat fetch file system information

Utime change file access modification time

Utimes See utime

Quotactl controls disk quotas

· System Control

Ioctl I/O total control function

_sysctl read/write system parameters

Acct Enables or disables process accounting

Getrlimit Get system resource cap

Setrlimit set system resource cap

Getrusage get system resource usage

Uselib selects the binary library to use

Ioperm sets port I/O permissions

Iopl changes process I/O privilege level

Outb low-level port operation

Reboot restart

Swapon opens swap files and devices

Swapoff closes swap files and devices

Bdflush controls the bdflush daemon

Sysfs fetches core supported file system types

Sysinfo Get system information

Adjtimex adjust system clock

Alarm setting process alarm

Getitimer get timer value

Setitimer sets the timer value

Gettimeofday take time and time zone

Settimeofday setting time and time zone

Stime Set system date and time

Time Get system time

Times take process running time

Uname Gets the current UNIX system name, version, and host information

Vhangup hangs the current terminal

Nfsservctl controls NFS daemons

Vm86 enters analog 8086 mode

Create_module creates a loadable module entry

Delete_module deletes a loadable module item

Init_module initialization module

Query_module query module information

*get_kernel_syms gets the core symbol, which has been replaced by query_module

· Memory Management

Brk changes the allocation of data segment space

Sbrk See brk

Mlock memory page lock

Munlock memory page unlock

Mlockall calls the process all memory pages locked

Munlockall calling process to unlock all memory pages

Mmap Maps Virtual Memory Pages

Munmap removes memory page maps

Mremap remaps virtual memory addresses

Msync writes mapped memory to disk

Mprotect sets memory image protection

Getpagesize Get page size

Sync writes memory buffer data back to hard disk

Cacheflush writes the contents of the specified buffer back to disk

· Network Management

Getdomainname fetch domain name

Setdomainname set domain name

Gethostid Get host identification number

Sethostid set host identification number

Gethostname Get this hostname

Sethostname set host name

Socket control

Socketcall socket system call

Socket establish socket

Bind binding socket to port

Connect to connect to a remote host

Accept responds to a socket connection request

Send to send information via socket

Sendto sends UDP information

Sendmsg See send

Recv receives information through socket

Recvfrom receives UDP information

Recvmsg see recv

Listen listen socket port

Select polling multiple synchronous I/Os

Shutdown closes the connection on the socket

Getsockname Get local socket name

Getpeername Get the peer name of the communication partner

Getsockopt take port settings

Setsockopt set port parameters

Sendfile transfers data between files or ports

Socketpair creates a pair of connected unnamed sockets

· User Management

Getuid get user identification number

Setuid set the user flag

Getgid get group ID number

Setgid set group flag

Getegid get valid group ID number

Setegid sets the valid group ID number

Geteuid get valid user identification number

Seteuid sets a valid user identification number

Setregid sets the real and valid group ID respectively

Setreuid sets true and valid user IDs, respectively

Getresgid Get true, valid, and saved group ID numbers, respectively

Setresgid sets true, valid, and saved group ID numbers, respectively

Getresuid obtains real, valid, and saved user identification numbers, respectively

Setresuid sets true, valid, and saved user IDs, respectively

Setfsgid Sets the group ID number used for file system checking

Setfsuid sets the user identification number used for file system checking

Getgroups Get list of post-requisition groups

Setgroups set up after group flag list

Inter-process communication

Ipc interprocess communication total control call

signal

Sigaction Sets the processing method for the specified signal

Sigprocmask Performs blocking/unblocking on signals in the signal set according to parameters

Sigpending sets the queue for the specified blocked signal

Sigsuspend hang process waiting for a specific signal

Signal See signal

Kill signals a process or process group

*sigblock adds a signal to the blocked signal mask, which has been replaced by sigprocmask

*siggetmask Gets an existing blocking signal mask, which has been replaced by sigprocmask

*sigsetmask Replaces the existing blocking signal mask with the given signal mask, which has been replaced by sigprocmask

*sigmask converts the given signal to a mask, which has been replaced by sigprocmask

*Sigpause function with sigsuspend, has been replaced by sigsuspend

Sigvec Signal processing functions for BSD-compatible functions similar to sigaction

Ssetmask ANSI C signal processing function, similar to sigaction

news

Msgctl message control operation

Msgget get message queue

Msgsnd Send a message

Msgrcv take message

pipeline

Pipe Create pipe

signal

Semctl semaphore control

Semget gets a set of semaphores

Semop semaphore operation

Shared memory

Shmctl controls shared memory

Shmget get shared memory

Shmat connection shared memory

Shmdt dismounting shared memory

Ejector Header Connector

Ejector Header Connector,Dual Row Ejector Header Connector,Single Row Ejector Header Connector,Gold Plated Ejector Header Connector

Shenzhen Jinyicheng Electronci Technology Co.,Ltd. , https://www.jycconnector.com