group在C语言,cgroup 应用

cgroup 介绍

CGroup 是 Control Groups 的缩写,是 Linux 内核提供的一种可以限制、记录、隔离进程组 (process groups) 所使用的物力资源 (如 cpu memory i/o 等等) 的机制。CGroup 提供了一个 CGroup 虚拟文件系统,作为进行分组管理和各子系统设置的用户接口。要使用 CGroup,必须挂载 CGroup 文件系统。这时通过挂载选项指定使用哪个子系统。

cgroup 功能

Resource limitation: 限制资源使用,比如内存使用上限以及文件系统的缓存限制。

Prioritization: 优先级控制,比如:CPU利用和磁盘IO吞吐。

Accounting: 一些审计或一些统计,主要目的是为了计费。

Control: 挂起进程,恢复执行进程。

cgroup 子系统介绍

blkio 设置限制每个块设备的输入输出控制。例如:磁盘,光盘以及usb等等。

cpu 使用调度程序为cgroup任务提供cpu的访问。

cpuacct 产生cgroup任务的cpu资源报告。

cpuset 如果是多核心的cpu,这个子系统会为cgroup任务分配单独的cpu和内存。

devices 允许或拒绝cgroup任务对设备的访问。

freezer 暂停和恢复cgroup任务。

memory 设置每个cgroup的内存限制以及产生内存资源报告。

net_cls 标记每个网络包以供cgroup方便使用。

ns 名称空间子系统。

perf_event 增加了对每group的监测跟踪的能力,即可以监测属于某个特定的group的所有线程以及运行在特定CPU上的线程,此功能对于监测整个group非常有用。

cgroup 使用

安装

sudo yum install libcgroup

启动关闭服务

The cgconfig(control group config) service is used to create cgroups and manage subsystems.

sudo service cgconfig start/stop

start up at the boot time.

chkconfig cgconfig on

配置

The cgroup configuration file is /etc/cgconfig.conf. Depending on the contents of the configuration file, cgconfig can create hierarchies, mount necessary file systems, create cgroups, and set subsystem parameters (resource limits) for each cgroup.

/etc/cgconfig 的格式

mount {

= ;

...

}

controller:内核子系统的名称

path:该子系统的挂载点

group {

[]

{

= ;

}

}

name: 指定cgroup的名称

permissions:可选项,指定cgroup对应的挂载点文件系统的权限,root用户拥有所有权限。

controller:子系统的名称

param name 和 param value:子系统的属性及其属性值#### 实例

选择cgroup的处理对象

Cgred (control group rules engine daemon) is a service that moves tasks into cgroups according to parameters set in the /etc/cgrules.conf file.

/ect/cgrules的格式

user subsystems control_group

or

user:command subsystems control_group

user refers to a username or a groupname prefixed with the "@" character.

subsystems refer to a comma-separated list of subsystem names.

control_group represents a path to the cgroup,

command stands for a process name or a full command path of a process.

特殊字符的意义

@ — indicates a group instead of an individual user. For example, @admin indicates all users in the admin group.

* — represents "all". For example, * in the user field represents all users.

% — represents an item the same as the item in the line above.

除了通过配置文件/etc/cgrules 和 /etc/cgconfig ,同样可以使用命令行进行配置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值