Linux 中list.h使用实例和坑

以前都是自己写链表或者所用框架都自带链表操作,本次工作换了框架没有找到框架自带的链表操作,所以尝试使用linux自带的list.h中定义的相关宏和函数写了简单的链表操作,竟然踩坑了,记录一下。

一、list.h简介

list.h一般放在include/list.h,当然有的linux系统放list.h地方在别的地方,比如ubuntu。可以使用find / -name list.h找到,使用gcc编译的时候include上。

list.h是Linux内核中,为了提供统一的链表操作,减少结构体的额外开支提供的链表操作。list.h中定义的链表了一个双向循环链表,和一个哈希表。

1,基本结构

struct list_head {
        struct list_head *next, *prev;
};

2,初始化定义

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
        struct list_head name = LIST_HEAD_INIT(name)

3,功能定义

/**添加节点
 * list_add_tail - add a new entry
 * @new: new entry to be added
 * @head: list head to add it before
 * 
 * Insert a new entry before the specified head.
 * This is useful for implementing queues.
 */
static inline void list_add_tail(struct list_head *_new, struct list_head *head)
{
        __list_add(_new, head->prev, head);
}

/**删除节点
 * list_del - deletes entry from list.
 * @entry: the element to delete from the list.
 * Note: list_empty() on entry does not return true after this, the entry is
 * in an undefined state.
 */
static inline void list_del(struct list_head *entry)
{
        __list_del(entry->prev, entry->next);
        entry->next = (struct list_head*)LIST_POISON1;
        entry->prev = (struct list_head*)LIST_POISON2;
}

/**链表判断
 * list_empty - tests whether a list is empty
 * @head: the list to test.
 */
static inline int list_empty(const struct list_head *head)
{
        return head->next == head;
}

4,查询定义


/** 直接读取节点的查询方法1
 * list_for_each_entry  -       iterate over list of given type
 * @pos:        the type * to use as a loop cursor.
 * @head:       the head for your list.
 * @member:     the name of the list_head within the struct.
 */
#define list_for_each_entry(pos, head, member)                          \
        for (pos = list_entry((head)->next, typeof(*pos), member);      \
             &pos->member != (head);    \
             pos = list_entry(pos->member.next, typeof(*pos), member))

/**直接读取节点的查询方法2
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @pos:        the type * to use as a loop cursor.
 * @n:          another type * to use as temporary storage
 * @head:       the head for your list.
 * @member:     the name of the list_head within the struct.
 */
#define list_for_each_entry_safe(pos, n, head, member)                  \
        for (pos = list_entry((head)->next, typeof(*pos), member),      \
                n = list_entry(pos->member.next, typeof(*pos), member); \
             &pos->member != (head);                                    \
             pos = n, n = list_entry(n->member.next, typeof(*n), member))

二、实战

程序大概的意思是:创建一个5个成员的stu链表,打印;删除其中一个成员,再打印。带参数的可执行程序,./test 1:标识删除第一个,后面数字表示删除第几个。

#include "list.h"
#include <stdlib.h>
#include <stdio.h>

typedef struct _ststudent_
{
        int age;
        int grade;
}STU_T;

typedef struct _stulist_
{
        STU_T stStu;
        struct list_head list;
}STU_LIST_T;

LIST_HEAD(g_stStuList_Head);
void main(int argc,char *argv[])
{
        STU_LIST_T *pstStu=NULL;
        STU_LIST_T *pstStu_out=NULL;
        STU_LIST_T *n=NULL;
        int i=0;
        for(i=0;i<5;i++)
        {
                pstStu=(STU_LIST_T*)malloc(sizeof(STU_LIST_T));
                pstStu->stStu.age=i+10;
                pstStu->stStu.grade=i+100;
                list_add_tail(&pstStu->list,&g_stStuList_Head);
        }

        printf("----------------add---------------------\n");
        list_for_each_entry(pstStu_out,&g_stStuList_Head,list)
        {
                printf("pstStu_out->stStu.age=%d,grade=%d\n",pstStu_out->stStu.age,pstStu_out->stStu.grade);
        }


        printf("----------------del---------------------\n");

#ifdef TEST_TRUE
        list_for_each_entry(pstStu_out,&g_stStuList_Head,list)
        {
                if(atoi(argv[1])==pstStu_out->stStu.age-10)
                {
                        list_del(&pstStu_out->list);
                        free(pstStu_out);
                        continue;
                }
                printf("pstStu_out->stStu.age=%d,grade=%d\n",pstStu_out->stStu.age,pstStu_out->stStu.grade);

        }

#else

        list_for_each_entry_safe(pstStu_out,n,&g_stStuList_Head,list)
        {
                if(atoi(argv[1])==pstStu_out->stStu.age-10)
                {
                        list_del(&pstStu_out->list);
                        free(pstStu_out);
                        continue;
                }
                printf("pstStu_out->stStu.age=%d,grade=%d\n",pstStu_out->stStu.age,pstStu_out->stStu.grade);

        }
#endif
        return;
}

1,为啥不用#include <list.h> ?

     因为我自己系统带的list.h不在标准库。。

root@ubuntu:/home/Ctest/clistTest# find / -name list.h
/lib/firmware/carl9170fw/tools/include/list.h
find: ‘/run/user/1000/gvfs’: Permission denied
/usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-150/include/linux/list.h
/usr/src/linux-headers-5.4.0-150-generic/scripts/kconfig/list.h
/usr/src/linux-headers-5.4.0-150-generic/include/config/system/blacklist/hash/list.h
/usr/src/linux-headers-5.4.0-150-generic/include/config/system/revocation/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-136/scripts/kconfig/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-136/include/linux/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-135/scripts/kconfig/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-135/include/linux/list.h
/usr/src/linux-headers-5.4.0-135-generic/scripts/kconfig/list.h
/usr/src/linux-headers-5.4.0-135-generic/include/config/system/blacklist/hash/list.h
/usr/src/linux-headers-5.4.0-135-generic/include/config/system/revocation/list.h
/usr/src/linux-headers-5.4.0-136-generic/scripts/kconfig/list.h
/usr/src/linux-headers-5.4.0-136-generic/include/config/system/blacklist/hash/list.h
/usr/src/linux-headers-5.4.0-136-generic/include/config/system/revocation/list.h

2,编译

 gcc -o test listtest.c -I /usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/ -DTEST_TRUE

3,-D是为了方便测试 预编译宏。

用来分别编译两个可执行程序说明遇到的坑

4,运行结果,带参数运行

坑:如果只查询使用list_for_each_entry()list_for_each_entry_safe()都可以,但是如果查出来要进行删除操作就需要用list_for_each_entry_safe(),否则会宕机。具体原因可以自己查看实现。

root@ubuntu:/home/Ctest/clistTest# gcc -o test listtest.c -I /usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/ -DTEST_TRUE
root@ubuntu:/home/Ctest/clistTest# ./test 1
----------------add---------------------
pstStu_out->stStu.age=10,grade=100
pstStu_out->stStu.age=11,grade=101
pstStu_out->stStu.age=12,grade=102
pstStu_out->stStu.age=13,grade=103
pstStu_out->stStu.age=14,grade=104
----------------del---------------------
pstStu_out->stStu.age=10,grade=100
pstStu_out->stStu.age=593,grade=0
Segmentation fault (core dumped)
root@ubuntu:/home/Ctest/clistTest# gcc -o test listtest.c -I /usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/ 
root@ubuntu:/home/Ctest/clistTest# ./test 1
----------------add---------------------
pstStu_out->stStu.age=10,grade=100
pstStu_out->stStu.age=11,grade=101
pstStu_out->stStu.age=12,grade=102
pstStu_out->stStu.age=13,grade=103
pstStu_out->stStu.age=14,grade=104
----------------del---------------------
pstStu_out->stStu.age=10,grade=100
pstStu_out->stStu.age=12,grade=102
pstStu_out->stStu.age=13,grade=103
pstStu_out->stStu.age=14,grade=104
root@ubuntu:/home/Ctest/clistTest# 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Linux C Socket通信实例: 服务端代码: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define PORT 6000 int main() { int sockfd, newsockfd, n; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; socklen_t clilen; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR opening socket"); exit(1); } bzero((char *)&serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(PORT); if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { perror("ERROR on binding"); exit(1); } while (1) { listen(sockfd, 5); clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen); if (newsockfd < 0) { perror("ERROR on accept"); exit(1); } bzero(buffer, 256); n = read(newsockfd, buffer, 255); if (n < 0) { perror("ERROR reading from socket"); exit(1); } printf("Here is the message: %s\n", buffer); n = write(newsockfd, "I got your message", 18); if (n < 0) { perror("ERROR writing to socket"); exit(1); } close(newsockfd); } close(sockfd); return 0; } ``` 客户端代码: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define PORT 6000 int main(int argc, char *argv[]) { int sockfd, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if (argc < 2) { fprintf(stderr, "usage %s hostname\n", argv[0]); exit(0); } server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr, "ERROR, no such host\n"); exit(0); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR opening socket"); exit(1); } bzero((char *)&serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr_list[0], (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(PORT); if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { perror("ERROR connecting"); exit(1); } printf("Please enter the message: "); bzero(buffer, 256); fgets(buffer, 255, stdin); n = write(sockfd, buffer, strlen(buffer)); if (n < 0) { perror("ERROR writing to socket"); exit(1); } bzero(buffer, 256); n = read(sockfd, buffer, 255); if (n < 0) { perror("ERROR reading from socket"); exit(1); } printf("%s\n", buffer); close(sockfd); return 0; } ``` 运行方式: 1. 编译服务端代码:`gcc server.c -o server` 2. 编译客户端代码:`gcc client.c -o client` 3. 启动服务端:`./server` 4. 在另一个终端启动客户端:`./client localhost` 其,`localhost`可以替换为服务端的IP地址。在客户端输入消息,服务端将接收到消息并回复“`I got your message`”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值