Sysctl system call

Source: Internet
Author: User

I have never used this system call before. I recently saw it when I checked the source code. By the way, I learned about it.

I will not describe much about its role. If you are familiar with Linux, you should understand that there is a sysfs in Linux, which is used to obtain or modify information from sysfs.

 

The prototype of the function provided in Man 2 sysctl is

Int _ sysctl (struct _ sysctl_args * ARGs );

It is not easy to use. It also has an underline ....

Do not use this. Check/usr/include/sys/sysctl. H (note that it is not/usr/include/Linux/sysctl. h In man)

There is a handy function prototype.

 

Extern int sysctl (int * _ name, int _ nlen, void * _ oldval,

Size_t * _ oldlenp, void * _ newval, size_t _ newlen) _ Throw;

Parameter introduction:

 

_ Name: an integer array representing the path from the sysfs root directory to the target file,

_ NLE: Number of array elements in parameter 1

_ Oldva: a buffer. The kernel saves the information in the buffer and returns it to the user.

_ Oldlenp: the size of the passed _ oldval buffer, Which is overwritten by the length of information returned by the kernel using practice.

_ Newval: If you want to modify the variable, input the new value from here.

_ Newlen :__ length of newval.

All the following four parameters are null and 0.

_ Name: how to select this parameter? What is path?

Let's take a look at the enumerated values in Linux/sysctl. h.

 

Enum

{

Ctl_kern = 1,/* General kernel info and control */

Ctl_vm = 2,/* VM management */

Ctl_net = 3,/* networking */

Ctl_proc = 4,/* removal breaks strace (1) Compilation */

Ctl_fs = 5,/* filesystems */

Ctl_debug = 6,/* debugging */

Ctl_dev = 7,/* devices */

Ctl_bus = 8,/* busses */

Ctl_abi = 9,/* binary emulation */

Ctl_cpu = 10,/* CPU stuff (speed scaling, etc )*/

Ctl_arlan = 254,/* Arlan wireless driver */

Ctl_s390dbf = 5677,/* s390 debug */

Ctrochelle SunRPC = 7249,/* SunRPC debug */

Ctl_pm = 9899,/* frv power management */

Ctl_frv = 9898,/* frv specific sysctls */

};

Let's look at the file Pei @ Pei-desktop in/proc/sys :~ $ Ls/proc/syscrypto debug Dev FS kernel net VM each enumeration value above represents the following directory. Some of them are not because sysfs is dynamic and depends on the options you compile the kernel, the header file is static and must contain all possible sysfs values. Therefore, the header file contains more projects. With this in mind, the following example shows how to read the content of the/proc/sys/NET/IPv4/tcp_mem file: Pei @ Pei-desktop :~ $ CAT/proc/sys/NET/IPv4/tcp_mem 457926105691584 remember its path, and then we write down the following program # Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <unistd. h> <br/> # include <sys/sysctl. h> <br/> int main () <br/>{< br/> struct {<br/> int low, pressure, high; <br/>} Buf; <br/> size_t Len = sizeof (BUF); <br/> int name [] = {ctl_net, net_ipv4, net_tcp_mem}; <br/> If (sysctl (name, sizeof (name)/sizeof (INT), & Buf, & Len, null, 0) <0) {<br/> perror ("sysctl "); <br/> exit (exit_failure); <br/>}< br/> printf ("tcp_mem: Low = % d, pressure = % d, high = % d/N ", Buf. low, Buf. pressure, Buf. high); <br/> return 0; <br/>}< br/>Note that the above name value nt name [] = {ctl_net, net_ipv4, net_tcp_mem} is exactly the same as the path of/proc/sys/NET/IPv4/tcp_mem. Finally, this function is exclusive to Linux, so it is better to write a program with good portability.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.