Linux2.6 内核中结构体初始化(转载)
2015-07-15 16:30
543 查看
转自:http://hnniyan123.blog.chinaunix.net/uid-29917301-id-4989879.html
在Linux2.6版本的内核中,我们经常可以看到下面的结构体的定义和初始化。这在以前的C语言书中是极少见到的。下面的一个结构体来自到Linux内核中的一部分。在这个结构体中我们可以看到有普通的整型变量,也有函数的指针。
而在内核中可以使用下面的方法进行初始化。
下面是使用上面的结构体的一个小程序,其中还有结构体中的另外一种应用:
这里有一篇分析Linux内核这种数据结构比较详细的文章。 http://blog.csdn.net/mociml/archive/2009/08/13/4443280.aspx
在Linux2.6版本的内核中,我们经常可以看到下面的结构体的定义和初始化。这在以前的C语言书中是极少见到的。下面的一个结构体来自到Linux内核中的一部分。在这个结构体中我们可以看到有普通的整型变量,也有函数的指针。
struct net_proto_family { int family; int (*create)(struct net *net, struct socket *sock, int protocol, int kern); struct module *owner; };
而在内核中可以使用下面的方法进行初始化。
static const struct net_proto_family netlink_family_ops = { .family = PF_NETLINK, .create = netlink_create, .owner = THIS_MODULE, /* for consistency 8) */ };
下面是使用上面的结构体的一个小程序,其中还有结构体中的另外一种应用:
#include <stdio.h> #define PF_ID 10 enum { ID1 = 10, ID2, ID3, }; char *message = "message"; int create(int fd,char *name) { if(fd == 10) { printf ("I am create fd = %d,name = %s\n",fd,name); } return 1; } int output (int fd,char *name) { printf("I am output fd =%d,name = %s\n",fd,name); return 1; } int output_2 (int fd,char *name) { printf("I am output_2 fd = %d,name = %s\n",fd,name); } struct test_1 { int (*output)(int fd,char *name); }; struct test { int id; char name[50]; int (*print)(int fd,char *name); }; static struct test des ={ .id= PF_ID, .name ="frank", .print = create, }; struct test_1 table[] = { [ID1] = {.output = output}, [ID2] = {.output = output_2}, }; int main() { printf("des.PF_ID=%d\n",des.id); printf("des.message= %s\n",des.name); des.print(PF_ID,message); table[ID2].output(PF_ID,"table ID2"); table[ID1].output(PF_ID,"table ID1"); }
这里有一篇分析Linux内核这种数据结构比较详细的文章。 http://blog.csdn.net/mociml/archive/2009/08/13/4443280.aspx
相关文章推荐
- Linux-2.6.20的cs8900驱动分析(三)
- Linux-2.6.20的cs8900驱动分析(二)
- Linux 磁盘坏道检测和修复
- 多台center服务器相互免密码登陆
- linux系统数据落盘之细节
- Linux-2.6.20的cs8900驱动分析(一)
- 关于PPP 2.4.5在CENTOS7 无法编译通过的解决方案
- linux下一个有意思的问题(文件名以短划线或空格开头)
- Linux常用命令大全
- linux命令
- centos 6.5配置jdk
- Linux下搭建SVN+版本冲突
- 29个你必须知道的Linux命令
- 十个非常有趣的 Linux 彩蛋
- 【Linux】安装DNS域名解析服务
- LINUX内核 DEVICE_ATTR与cat echo命令 直接读写调用Kernel测试方法
- linux 下如何通过ssh到其他机器然后执行命令?
- IDO分享 | 如何在centos下安装OpenCMS
- IDO分享 | 如何在centos下安装OpenCMS
- CentOS 6下linux重启后自动执行命令