近來在閱讀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函數定義衝突。