如何将子 shell 添加到 cgroup? 子外壳在休眠时表现不同其实第一个适合我,我该怎么办?

问题描述

子外壳在休眠时表现不同

  1. 创建父 cgroup 和子 cgroup
pg := cgroups.New(
    cgroups.V1,cgroups.StaticPath("test-panda"),...,)
cg := pg.New(
    "panda-plugins-test.slice",&specs.LinuxResources{
        CPU: &specs.LinuxCPU{
            Quota:  &quota,Period: &period,},Memory: &specs.LinuxMemory{
            Limit:            &mem,DisableOOMKiller: &c.disableoom,)
  1. 创建cmd进程并将pid添加到子cgroup
cmd := exec.Command(p.opts.Path,p.opts.Args...)
cmd.Path = "/tmp/plugins/test.sh"
cmd.Dir = "/tmp/plugins/"

pid := cmd.Process.Pid
cg.Add(cgroups.Process{Pid: pid})
  1. 执行并等待cmd进程
go func() {
    defer func() {
        close(ch)
        p.exit()
    }()

    wait := func() (int,error) {
        err := p.cmd.Wait()
        if err != nil {
            exitErr,ok := err.(*exec.ExitError)
            if !ok {
                return -1,err
            }

            status,ok := exitErr.Sys().(syscall.WaitStatus)
            if !ok {
                return -1,err
            }

            return status.ExitStatus(),err
        }
        return 0,nil
    }

    p.exitCode,p.exitErr = wait()
    p.log.Infof("process %s exited with %d | %v",p.opts.Name,p.exitCode,p.exitErr)
}()

运行这段代码 [root@st1-tool-web-3] /tmp/test$ ./cgroup_demo

  1. 期间查看 cgroup

    4.1 当 test.sh 内容为

    #!/bin/bash
    
    sleep 5
    
    /bin/bash ./a.sh
    

    查看 cgroup 是

    [root@st1-tool-web-3] /home/ansible$ systemd-cgls
    ├─1 /usr/lib/systemd/systemd --system --deserialize 21
    ├─test-panda
    │ ├─28869 ./cgroup_demo
    │ └─panda-plugins-test.slice
    │   ├─28874 /bin/bash /tmp/plugins/test.sh
    │   ├─28894 /bin/bash ./a.sh
    │   └─28895 sleep 20
    

    4.2 当 test.sh 内容为

    #!/bin/bash
    
    /bin/bash ./a.sh
    

    cgroup 是

    [root@st1-tool-web-3] /home/ansible$ systemd-cgls
    ├─1 /usr/lib/systemd/systemd --system --deserialize 21
    ├─test-panda
    │ ├─22620 ./cgroup_demo
    │ ├─22626 /bin/bash ./a.sh
    │ ├─22628 sleep 20
    │ └─panda-plugins-test.slice
    │   └─22625 /bin/bash /tmp/plugins/test.sh
    

其实第一个适合我,我该怎么办?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)