內核雙向循環鏈表

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#include <arpa/inet.h>
?
?//鏈表頭結構
?struct list_head
?{
? ? ?struct list_head *next,*prev;
?};
?
?//真正實現鏈表插入操作
?void _list_add(struct list_head *nnew,struct list_head *prev,struct list_head *next)
?{
? ? ?next->prev = nnew;
? ? ?nnew->next = next;
? ? ?nnew->prev = prev;
? ? ?prev->next = nnew;
?}


?void INIT_LIST_HEAD(struct list_head *list)
?{
? ? ?list->next = list;
? ? ?list->prev = list;
?}
?
?//向鏈表插入一個節點
?void list_add(struct list_head *nnew,struct list_head *head)
?{
? ? ?_list_add(nnew,head,head->next);
?}
?
?
?#define list_for_each(pos,head) \
? ? ?for(pos = (head)->next;pos != (head);pos = pos->next)
?
?#define list_for_each_safe(pos,n,head) \
? ? ?for(pos = (head)->next,n = pos->next;pos != (head);pos = n,n = pos->next)
?
?//根據節點中的一個成員在節點中的偏移量找到節點的起始地址
?#define list_entry(ptr,type,member) \
? ? ?((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
?
?//真正實現鏈表刪除操作
?void _list_del(struct list_head *prev,struct list_head *next)
?{
? ? ?next->prev = prev;
? ? ?prev->next = next;
?}
?

?//刪除鏈表中的一個節點

刪除節點后,內核原始操作會將兩個指針指向自己,這個在實際運用的時候可能會出錯,新分配的內存正好分陪到之前釋放的節點的內存,會使得新的節點自己指向自己,導致死循環,

?void list_del(struct list_head *entry)
?{
? ? ?_list_del(entry->prev,entry->next);
? ? ?entry->next = NULL;
? ? ?entry->prev = NULL;
?}


#define SERVER_GROUP_NAME_SIZE 32
#define MAX_SERVER_GROUP_SIZE 128


typedef struct server_info {
? ? unsigned int status;
? ? ? ? union {
? ? ? ? struct in_addr addr;
? ? ? ? struct in6_addr addr6;
? ? };
}server_info_t;




typedef struct server_group?
{
struct list_head list;
char ? ? ? ? ? ? ? name[SERVER_GROUP_NAME_SIZE];
? ? server_info_t ? ? ? servers[MAX_SERVER_GROUP_SIZE];
? ? unsigned int ? ? ? ? ? count;
}ser_group_t;

ser_group_t * g_ser_t;

void list_init()
{

g_ser_t = (ser_group_t *)malloc(sizeof(ser_group_t));
INIT_LIST_HEAD(&g_ser_t->list);
}

/*insert link*/
void add_list(ser_group_t* server_new_data)
{
ser_group_t *listnode = NULL;
listnode = (ser_group_t *)malloc(sizeof(ser_group_t));
memset(listnode,0,sizeof(listnode));
memcpy(listnode,server_new_data,sizeof(ser_group_t));
list_add(&listnode->list,&g_ser_t->list);
}
void node_find(ser_group_t* sernode_find)
{
struct ser_group_t *listnode;
struct list_head *pos ;
struct ser_group_t *p = NULL;
struct list_head *t;
int i;


//遍歷鏈表
list_for_each(pos,&g_ser_t->list)
{
i--;
//找出一個節點
p = list_entry(pos,ser_group_t,list);
printf("Node %d's data: %d\n",i,p->count);
}
}
void node_delete(ser_group_t* sernode_del)
{
struct ser_group_t *listnode;
struct list_head *pos ;
struct ser_group_t *p;
struct list_head *t;

struct ser_group_t *tmp = NULL;


int i;

/*刪除鏈表*/
#if 0
list_for_each_safe(pos,t,&g_ser_t.list)
{
//刪除節點
list_del(pos);
p = list_entry(pos,struct ser_group_t,list);
//釋放節點的內存
free(p);
}
#endif

/*刪除一個節點*/
?list_for_each_safe(pos, t, &g_ser_t->list)?
? ? ? {?
? ? ? ? /*tmp = list_entry(pos, struct numlist, list);?
//節點相同,刪除
?if(tmp == sernode_del)?
? ? ? ? ? ? ? {?
? ? ? ? ? ? ? printf("node %d delete\n",tmp->num);
? ? ? ? ? ? ? ? ? ? list_del_init(pos);?
? ? ? ? ? ? ? ? ? ? free(tmp);?
? ? ? ? ? ? ? }?
*/
? ? ? ?}
}

int main()
{
ser_group_t test = {0};
test.count = 1;


list_init();
add_list(&test);
node_find(&test);
return 0;
}









本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/383221.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383221.shtml
英文地址,請注明出處:http://en.pswp.cn/news/383221.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux系統編程----16(線程同步,互斥量 mutex,互斥鎖的相關函數,死鎖,讀寫鎖)

同步概念 所謂同步&#xff0c;即同時起步&#xff0c;協調一致。不同的對象&#xff0c;對“同步”的理解方式略有不同。如&#xff0c;設備同步&#xff0c;是指在兩 個設備之間規定一個共同的時間參考&#xff1b;數據庫同步&#xff0c;是指讓兩個或多個數據庫內容保持一致…

轉移字符的轉換

使得網頁上不會顯示 \x0a\x0a \x0a \x0a \x0a \x0a 類似的字符static int te_escape_isDec(char *ptr, unsigned int len) { …

Linux系統編程---17(條件變量及其函數,生產者消費者條件變量模型,生產者與消費者模型(線程安全隊列),條件變量優點,信號量及其主要函數,信號量與條件變量的區別,)

條件變量 條件變量本身不是鎖&#xff01;但它也可以造成線程阻塞。通常與互斥鎖配合使用。給多線程提供一個會合的場所。 主要應用函數&#xff1a; pthread_cond_init 函數pthread_cond_destroy 函數pthread_cond_wait 函數pthread_cond_timedwait 函數pthread_cond_signa…

好友

http://blog.csdn.net/liangyuannao/article/details/8583139

Linux系統編程---18(線程池相關概念及其實現)

線程池 概念&#xff1a; 一堆線程任務隊列 作用 避免大量線程頻繁的創建/銷毀時間成本避免瞬間大量線程創建耗盡資源&#xff0c;程序崩潰危險 實現 創建固定數量的線程創建一個線程安全的任務隊列 一種線程使用模式。 線程過多會帶來調度開銷&#xff0c;進而影響緩…

設計模式--1(設計模式基礎,設計模式基本原則,設計模式分類)

設計模式基礎 模式 在一定環境中解決某一問題的方案&#xff0c;包括三個基本元素–問題&#xff0c;解決方案和環境。大白話&#xff1a;在一定環境下&#xff0c;用固定套路解決問題。 設計模式 是一套被反復使用、多數人知曉的、經過分類編目的、代碼設計經驗的總結。使…

source insight 使用技巧

source insight 使用技巧 1 sourceinsight screen font 的默認字體是Verdana的&#xff0c;它是一直變寬字體。在Document style中可以將字體改為定寬的Courier2 document options->auto indent 去掉indent Open Brace和Indent Close Brace的效果: 繼上一段&#xff0c;在…

設計模式----2(簡單工廠模式的概念,簡單工廠模式的實現,簡單工廠模式的優缺點)

簡單工廠模式 簡單工廠模式的概念 簡單工廠模式屬于類的創建型模式,又叫做靜態工廠方法模式。通過專門定義一個類來負 責創建其他類的實例&#xff0c;被創建的實例通常都具有共同的父類。 具體分類 工廠&#xff08;Creator&#xff09;角色 簡單工廠模式的核心&#xff0…

Redis常見問題及其一些重點知識總結

1、什么是 Redis&#xff1f;簡述它的優缺點&#xff1f; Redis 的全稱是&#xff1a;Remote Dictionary.Server&#xff0c;本質上是一個 Key-Value 類型的內存數據庫&#xff0c;很像 memcached&#xff0c;整個數據庫統統加載在內存當中進行操作&#xff0c;定期通過異步操…

shell生成隨機文件名

1 #!/bin/bash 2 # tempfile-name.sh: 臨時文件名產生器 3 4 BASE_STRmcookie # 32-字符的 magic cookie. 5 POS11 # 字符串中隨便的一個位置. 6 LEN5 # 取得 $LEN 長度連續的字符串. 7 8 prefixtemp # 最終的一個臨時文…

設計模式---3(工廠方法模式的概念,工廠方法模式的實現,工廠方法模式和簡單工廠模式比較)

工廠方法模式 概念 工廠方法模式同樣屬于類的創建型模式又被稱為多態工廠模式 。 工廠方法模式的意義 定義一個創建產品對象的工廠接口&#xff0c;將實際創建工作推遲到子類當中。 核心工廠類不再負責產品的創建&#xff0c;這樣核心類成為一個抽象工廠角色&#xff0c;僅…

設計模式---4(抽象工廠模式的概念,產品組和產品等級的概念,抽象工廠模式的實現)

抽象工廠模式 抽象工廠模式的概念 抽象工廠模式是所有形態的工廠模式中最為抽象和最其一般性的。抽象工廠模式可以向 客戶端提供一個接口&#xff0c;使得客戶端在不必指定產品的具體類型的情況下&#xff0c;能夠創建多個產品 族的產品對象。 抽象工廠的角色及其職責 抽象工…

Win32項目關于MessageBox參數的詳細說明

函數功能&#xff1a;該函數創建、顯示、和操作一個消息框。消息框含有應用程序定義的消息和標題&#xff0c;加上預定義圖標與Push&#xff08;下按&#xff09;按鈕的任何組合。 函數原型&#xff1a;int MessageBox(HWND hWnd,LPCTSTR IpCaption,UINT…

w3af解析

1. w3af簡介 w3afis a Web Application Attack and Audit Framework.即Web應用攻擊和審計框架。w3af用python編寫&#xff0c;依賴的庫主要有2類&#xff0c;分別如下&#xff1a; <1> Core requirements: Python 2.6 fpconst-0.7.2&#xff1a;用于處理IEEE 754浮點…

1.c++中初始化列表和構造函數初始化的區別是什么?2.類的成員變量的初始化順序是按照聲明順序嗎?

初始化列表和構造函數初始化的區別是什么&#xff1f; 初始化和賦值對內置類型的成員沒有太大的區別&#xff0c;在成員初始化列表和構造函數體內進行&#xff0c;在性能和結果上都是一樣的。只有一些需要注意的事項 初始化列表一般情況如下&#xff1a; Date(int year, int …

設計模式---5(建造者模式的概念及其實現,建造者模式的角色與職責,建造者模式和工廠模式的區別)

建造者模式 建造者模式的概念 Builder 模式也叫建造者模式或者生成器模式&#xff0c;是由 GoF 提出的 23 種設計模式中的一種。 Builder 模式是一種對象創建型模式之一&#xff0c;用來隱藏復合對象的創建過程&#xff0c;它把復合對象的 創建過程加以抽象&#xff0c;通過子…

system阻塞SIGCHLD信號原因

system阻塞SIGCHLD信號原因 標簽&#xff1a; c 2014-11-08 11:58 198人閱讀 評論(0) 收藏 舉報 分類&#xff1a; linux編程&#xff08;1&#xff09; 代碼1&#xff1a;APUE10.18節的system函數源代碼 int system(const char *cmdstring) /* with appropriate signal ha…

設計模式6---(單例模式的概念及其實現(懶漢式和餓漢式),線程安全)

單例模式 單例模式的概念 單例模式是一種對象創建型模式&#xff0c;使用單例模式&#xff0c;可以保證為一個類只生成唯一的實例對象。也就是說&#xff0c;在整個程序空間中&#xff0c;該類只存在一個實例對象。 GoF 對單例模式的定義是&#xff1a;保證一個類、只有一個實…

C語言解析http請求表單內容

[1].[文件] cgi.h ~ 405B 下載(105) 跳至 [1] [2] [3] [4] [5] [6] [7] [8] ?123456789101112131415161718192021222324252627#ifndef CGI_H#define CGI_H#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct Node{char *…

centos給用戶添加sudo權限

linux給用戶添加sudo權限&#xff1a; 有時候&#xff0c;linux下面運行sudo命令&#xff0c;會提示類似&#xff1a; xxxis not in the sudoers file. This incident will be reported. 這里&#xff0c;xxx是用戶名稱&#xff0c;然后導致無法執行sudo命令&#xff0c;這時候…