日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言共享内存,在爷儿俩进程间使用共享内存(共享内容含指针)

發布時間:2025/3/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言共享内存,在爷儿俩进程间使用共享内存(共享内容含指针) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在父子進程間使用共享內存(共享內容含指針)

比如有這樣一個結構體

#define?MAX_QUE_LEN?100

//定義數據包緩存隊列

typedef?struct?_t_pkt_queue

{

UINT4???????V6_out_head;

UINT4???????V6_out_tail;

UINT4???????V6_out_curcnt;

tEthpkt?*???V6_out_queue[MAX_QUE_LEN];

}tPktQue;

tEthpkt??*??是指向一個數據包的指針。

在父子進程間將其共享。在子進程中?(tEthpkt?*)指針不為空,但是為什么不能對其進行操作?

------解決思路----------------------

引用:比如有這樣一個結構體

#define?MAX_QUE_LEN?100

//定義數據包緩存隊列

typedef?struct?_t_pkt_queue

{

UINT4???????V6_out_head;

UINT4???????V6_out_tail;

UINT4???????V6_out_curcnt;

tEthpkt?*???V6_out_queue[MAX_QUE_LEN];

}tPktQue;

tEthpkt??*??是指向一個數據包的指針。

在父子進程間將其共享。在子進程中?(tEthpkt?*)指針不為空,但是為什么不能對其進行操作?

Linux下在父進程用mmap創建匿名共享內存,共享內存的地址在父進程和子進程是一樣的,父子進程都可以直接修改共享內存中的數據。

你說的子進程不能對其進行操作是什么意思?是取不到想要的數據還是修改了數據父進程看不見?

你是怎么創建共享內存的?

------解決思路----------------------

引用:Quote: 引用:Quote: 引用:比如有這樣一個結構體

#define?MAX_QUE_LEN?100

//定義數據包緩存隊列

typedef?struct?_t_pkt_queue

{

UINT4???????V6_out_head;

UINT4???????V6_out_tail;

UINT4???????V6_out_curcnt;

tEthpkt?*???V6_out_queue[MAX_QUE_LEN];

}tPktQue;

tEthpkt??*??是指向一個數據包的指針。

在父子進程間將其共享。在子進程中?(tEthpkt?*)指針不為空,但是為什么不能對其進行操作?

Linux下在父進程用mmap創建匿名共享內存,共享內存的地址在父進程和子進程是一樣的,父子進程都可以直接修改共享內存中的數據。

你說的子進程不能對其進行操作是什么意思?是取不到想要的數據還是修改了數據父進程看不見?

你是怎么創建共享內存的?

shmid?=?shmget(IPC_PRIVATE,?sizeof(tPktQue),?IPC_CREAT

------解決思路----------------------

0660);

if(shmid?<=?0)

{

printf("\n***Create?a?shared?memory?Fail!\n");

return?-1;

}

stacksize?=?getpagesize();

stack?=?malloc(stacksize);

if(stack?==?NULL)

{

printf("\n***Malloc?For?Child?Process?stack?Failed!\n");

return?-1;

}

childstack?=?stack?+?stacksize;

flags?=?CLONE_NEWPID

------解決思路----------------------

CLONE_NEWNS;

pid?=?clone(vdev_func,?childstack,?flags,?(void?*)shmid);

shmaddr?=?(char?*)shmat(shmid,?NULL,?0);

PktQue[i]?=?(tPktQue?*)shmaddr;???//這里PktQue[i]?是一個指向tPktQue??的指針。

vdev_func子進程的調用函數。

在子進程中

shmaddr?=?(char?*)shmat(shmid,?NULL,?0);

PktQue[i]?=?(tPktQue?*)shmaddr;

比如對?PktQue[i]?->V6_out_queue[0]操作。操作不了。但其不為空。

你都用clone了,可以不用shmget和shmat了。看一下我寫的測試代碼:

#define?_GNU_SOURCE

#include?

#include?

#include?

#include?

int?data?=?10;

char?*str?=?NULL;

int?child_process(void?*args)

{

printf("Child?process?%d,?data?%d,?str:?%s\n",?getpid(),?data,?str);

data?=?20;

str?=?"child?str";

printf("Child?process?%d,?data?%d,?str:?%s\n",?getpid(),?data,?str);

while?(1)?{

sleep(1);

}

}

int?main(int?argc,?char?*argv[])

{

void?*stack?=?(void?*)malloc(16384);

int?clone_flag?=?CLONE_VM

------解決思路----------------------

CLONE_SIGHAND

------解決思路----------------------

CLONE_FS

------解決思路----------------------

CLONE_FILES;

str?=?"parent?str";

clone(child_process,?stack+16384,?clone_flag,?NULL);

printf("Parent?process?%d,?data?%d,?str:?%s\n",?getpid(),?data,?str);

sleep(1);

while?(1)?{

sleep(1);

}

return?0;

}

運行后輸出:

Child?process?4517,?data?10,?str:?parent?str

Child?process?4517,?data?20,?str:?child?str

Parent?process?4516,?data?20,?str:?child?str

------解決思路----------------------

引用:Quote: 引用:Quote: 引用:Quote: 引用:比如有這樣一個結構體

#define?MAX_QUE_LEN?100

//定義數據包緩存隊列

typedef?struct?_t_pkt_queue

{

UINT4???????V6_out_head;

UINT4???????V6_out_tail;

UINT4???????V6_out_curcnt;

tEthpkt?*???V6_out_queue[MAX_QUE_LEN];

}tPktQue;

tEthpkt??*??是指向一個數據包的指針。

在父子進程間將其共享。在子進程中?(tEthpkt?*)指針不為空,但是為什么不能對其進行操作?

Linux下在父進程用mmap創建匿名共享內存,共享內存的地址在父進程和子進程是一樣的,父子進程都可以直接修改共享內存中的數據。

你說的子進程不能對其進行操作是什么意思?是取不到想要的數據還是修改了數據父進程看不見?

你是怎么創建共享內存的?

shmid?=?shmget(IPC_PRIVATE,?sizeof(tPktQue),?IPC_CREAT

------解決思路----------------------

0660);

if(shmid?<=?0)

{

printf("\n***Create?a?shared?memory?Fail!\n");

return?-1;

}

stacksize?=?getpagesize();

stack?=?malloc(stacksize);

if(stack?==?NULL)

{

printf("\n***Malloc?For?Child?Process?stack?Failed!\n");

return?-1;

}

childstack?=?stack?+?stacksize;

flags?=?CLONE_NEWPID

------解決思路----------------------

CLONE_NEWNS;

pid?=?clone(vdev_func,?childstack,?flags,?(void?*)shmid);

shmaddr?=?(char?*)shmat(shmid,?NULL,?0);

PktQue[i]?=?(tPktQue?*)shmaddr;???//這里PktQue[i]?是一個指向tPktQue??的指針。

vdev_func子進程的調用函數。

在子進程中

shmaddr?=?(char?*)shmat(shmid,?NULL,?0);

PktQue[i]?=?(tPktQue?*)shmaddr;

比如對?PktQue[i]?->V6_out_queue[0]操作。操作不了。但其不為空。

你都用clone了,可以不用shmget和shmat了。看一下我寫的測試代碼:

#define?_GNU_SOURCE

#include?

#include?

#include?

#include?

int?data?=?10;

char?*str?=?NULL;

int?child_process(void?*args)

{

printf("Child?process?%d,?data?%d,?str:?%s\n",?getpid(),?data,?str);

data?=?20;

str?=?"child?str";

printf("Child?process?%d,?data?%d,?str:?%s\n",?getpid(),?data,?str);

while?(1)?{

sleep(1);

}

}

int?main(int?argc,?char?*argv[])

{

void?*stack?=?(void?*)malloc(16384);

int?clone_flag?=?CLONE_VM

------解決思路----------------------

CLONE_SIGHAND

------解決思路----------------------

CLONE_FS

------解決思路----------------------

CLONE_FILES;

str?=?"parent?str";

clone(child_process,?stack+16384,?clone_flag,?NULL);

printf("Parent?process?%d,?data?%d,?str:?%s\n",?getpid(),?data,?str);

sleep(1);

while?(1)?{

sleep(1);

}

return?0;

}

運行后輸出:

Child?process?4517,?data?10,?str:?parent?str

Child?process?4517,?data?20,?str:?child?str

Parent?process?4516,?data?20,?str:?child?str

錯了,sleep(1)是讓子進程先運行再打印,應該在第29行printf之前:

#define?_GNU_SOURCE

#include?

#include?

#include?

#include?

int?data?=?10;

char?*str?=?NULL;

int?child_process(void?*args)

{

printf("Child?process?%d,?data?%d,?str:?%s\n",?getpid(),?data,?str);

data?=?20;

str?=?"child?str";

printf("Child?process?%d,?data?%d,?str:?%s\n",?getpid(),?data,?str);

while?(1)?{

sleep(1);

}

}

int?main(int?argc,?char?*argv[])

{

void?*stack?=?(void?*)malloc(16384);

int?clone_flag?=?CLONE_VM

------解決思路----------------------

CLONE_SIGHAND

------解決思路----------------------

CLONE_FS

------解決思路----------------------

CLONE_FILES;

str?=?"parent?str";

clone(child_process,?stack+16384,?clone_flag,?NULL);

sleep(1);

printf("Parent?process?%d,?data?%d,?str:?%s\n",?getpid(),?data,?str);

while?(1)?{

sleep(1);

}

return?0;

}

------解決思路----------------------

引用:如果我現在要創建8個子進程,每個子進程與父進程之間有一個共享區域。要做到它們相互隔離。這=這樣會不會有問題?

我推薦你還是在父進程mmap一塊匿名共享內存,然后fork出子進程,這樣子進程也可以共享父進程創建的共享內存,而且指針也一樣,可以把共享內存中的指針保存到共享內存中。

總結

以上是生活随笔為你收集整理的c语言共享内存,在爷儿俩进程间使用共享内存(共享内容含指针)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。