Linux2.6 initialization of structures in kernel (reprint)

Source: Internet
Author: User

Transferred from: http://hnniyan123.blog.chinaunix.net/uid-29917301-id-4989879.html

In the Linux2.6 version of the kernel, we can often see the definition and initialization of the following struct body. This is rarely seen in the previous C language book. The following structure comes from a part of the Linux kernel. In this struct we can see that there are normal integer variables and pointers to functions.

struct net_proto_family {    int        family;     int        (*create) (structstruct socket *sock,                 intint  kern);     struct Module    *owner;};

In the kernel, the following methods can be used for initialization.

Static Const struct net_proto_family netlink_family_ops = {    = Pf_netlink,    = netlink_create ,    . Owner    = this_module,/    **/};

Here is a small program that uses the structure above, and there is another application in the structure:

#include <stdio.h>#definePF_ID 10enum{ID1=Ten, ID2, ID3,};Char*message ="message";intCreateintFdChar*name) {    if(FD = =Ten) {printf ("I am Create fd =%d,name =%s\n", Fd,name); }    return 1;}intOutput (intFdChar*name) {printf ("I am Output fd =%d,name =%s\n", Fd,name); return 1;}intOutput_2 (intFdChar*name) {printf ("I am output_2 fd =%d,name =%s\n", Fd,name);}structtest_1{int(*output) (intFdChar*name);};structtest{intID; Charname[ -]; int(*print) (intFdChar*name);};Static structTest des ={. ID=pf_id,. Name="Frank",. Print=Create,};structTest_1 table[] ={[ID1]= {. Output =output}, [ID2]= {. Output =output_2},};intMain () {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");}

Here is an article that analyzes the data structure of the Linux kernel in more detail.
Http://blog.csdn.net/mociml/archive/2009/08/13/4443280.aspx

Linux2.6 initialization of structures in kernel (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.