近来在阅读linux-2.6.37中cgroup的相关代码,读到include/linux/cgroup.h的开头时犯了嘀咕:

 

#include <linux/sched.h>
#include <linux/cpumask.h>
#include <linux/nodemask.h>
#include <linux/rcupdate.h>
#include <linux/cgroupstats.h>
#include <linux/prio_heap.h>
#include <linux/rwsem.h>
#include <linux/idr.h>

#ifdef CONFIG_CGROUPS

struct cgroupfs_root;
struct cgroup_subsys;
struct inode;
struct cgroup;
struct css_id;

extern int cgroup_init_early(void);
extern int cgroup_init(void);
extern void cgroup_lock(void);

extern int cgroup_lock_is_held(void);
extern bool cgroup_lock_live_group (struct cgroup *cgrp);

......

 

在此头文件的后面有cgroupfs_root、cgroup_subsys、cgroup这三个结构的定义,为何此处需要这么几行代码?

 

将此处的"strct cgroup;“注释掉之后,重新编译内核报出如下错误和警告:

 

kernel/cgroup.c:1910: error: conflicting types for ‘cgroup_lock_live_group’
include/linux/cgroup.h:32: error: previous declaration of ‘cgroup_lock_live_group’ was here
kernel/cgroup.c:1918: error: conflicting types for ‘cgroup_lock_live_group’
include/linux/cgroup.h:32: error: previous declaration of ‘cgroup_lock_live_group’ was here

 

include/linux/cgroup.h:32: warning: ‘struct cgroup’ declared inside parameter list
include/linux/cgroup.h:32: warning: its scope is only this definition or declaration, which is probably not what you want

 

由此我们可以知道,由于在cgroup.h文件中,struct cgroup结构定义在函数声明

extern bool cgroup_lock_live_group (struct cgroup *cgrp);

之后,所以编译的时候将struct cgroup作为在参数列表中定义的空结构处理了,这样在编译cgroup.c的时候便与真实的cgroup_lock_live_group函数定义冲突。

Logo

更多推荐