-
Notifications
You must be signed in to change notification settings - Fork 4
/
cgroups.c
59 lines (49 loc) · 1.24 KB
/
cgroups.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libcgroup.h>
#include <sys/syscall.h>
/*
* Assumes cgroup is mounted at /cgroup using
*
* mount -t cgroup -o none,name=test none /cgroup
* gcc a.c -o cgroup -lcgroup
*/
int main()
{
int ret;
struct cgroup *cgroup;
struct cgroup_controller *cgc;
ret = cgroup_init();
if (ret) {
printf("FAIL: cgroup_init failed with %s\n", cgroup_strerror(ret));
exit(3);
}
cgroup = cgroup_new_cgroup("/mysql/donatas4");
if (!cgroup) {
printf("FAIL: cgroup_new_cgroup failed\n");
exit(3);
}
cgc = cgroup_add_controller(cgroup, "cpu");
if (!cgc) {
printf("FAIL: cgroup_add_controller failed\n");
exit(3);
}
ret = cgroup_set_uid_gid(cgroup, 1000, 1000, 1000, 1000);
if (ret) {
printf("FAIL: cgroup_set_uid_gid failed with %s\n", cgroup_strerror(ret));
}
ret = cgroup_create_cgroup(cgroup, 1);
if (ret) {
printf("FAIL: cgroup_create_cgroup failed with %s\n", cgroup_strerror(ret));
exit(3);
}
if (access("/sys/fs/cgroup/cpu/mysql/donatas/tasks", F_OK) == 0)
printf("PASS\n");
else
printf("Failed to create cgroup\n");
pid_t tid = syscall(SYS_gettid);
ret = cgroup_attach_task_pid(cgroup, tid);
printf("attach task: %d\n", tid);
return 0;
}