-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcgroup.c
More file actions
61 lines (49 loc) · 1.95 KB
/
cgroup.c
File metadata and controls
61 lines (49 loc) · 1.95 KB
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
59
60
61
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <sched.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mount.h>
#include "cgroup.h"
#include "util.h"
char * const cgroup_base_dir="/tmp/cgroup";
char* cgroup_subsystems[] = {"cpu", "cpuset", "cpuacct", "memory", "blkio"};
int cgroup_subsystems_lengh = 5;
int cgroup_add(unicorn_t * u){
int i;
for (i=0; i< cgroup_subsystems_lengh; i++){
char system_path[80];
check_result(sprintf(system_path, "%s/%s", cgroup_base_dir, cgroup_subsystems[i]),system_path);
char cmd[100];
check_result(sprintf(cmd, "mkdir -p %s/%s", system_path, u->unicorn_id),cmd);
check_result(system(cmd), "system call mkdir ");
// char file_path[100];
// check_result(sprintf(file_path, "%s/%s/%s/tasks", cgroup_base_dir, cgroup_subsystems[i], u->unicorn_id),file_path);
// char value[10];
// check_result(sprintf(value, "%d", u->child_pid), value);
// printf("write value %s to %s \n", value, file_path);
// FILE * f = fopen(file_path, "w");
// if(f == NULL){
// printf("open file error %s \n", strerror(errno));
// exit(errno);
// }
// int rt = fputs("2345", f);
// if( rt < 0) {
// printf("write task file error %s ", cgroup_subsystems[i]);
// exit(-1);
// }
// printf("write %d chars \n", rt);
if( strcmp(cgroup_subsystems[i], "cpuset") == 0){
check_result(sprintf(cmd, "cat %s/cpuset.mems > %s/%s/cpuset.mems", system_path, system_path, u->unicorn_id),cmd);
check_result(system(cmd), "system call add cpuset.mems ");
check_result(sprintf(cmd, "cat %s/cpuset.cpus > %s/%s/cpuset.cpus", system_path, system_path, u->unicorn_id),cmd);
check_result(system(cmd), "system call add cpuset.cpus ");
}
check_result(sprintf(cmd, "echo %d > %s/%s/tasks", u->child_pid, system_path, u->unicorn_id),cmd);
check_result(system(cmd), "system call add pid ");
}
return 0;
}