c语言共享内存,在爷儿俩进程间使用共享内存(共享内容含指针)
在父子進(jìn)程間使用共享內(nèi)存(共享內(nèi)容含指針)
比如有這樣一個結(jié)構(gòu)體
#define?MAX_QUE_LEN?100
//定義數(shù)據(jù)包緩存隊列
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??*??是指向一個數(shù)據(jù)包的指針。
在父子進(jìn)程間將其共享。在子進(jìn)程中?(tEthpkt?*)指針不為空,但是為什么不能對其進(jìn)行操作?
------解決思路----------------------
引用:比如有這樣一個結(jié)構(gòu)體
#define?MAX_QUE_LEN?100
//定義數(shù)據(jù)包緩存隊列
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??*??是指向一個數(shù)據(jù)包的指針。
在父子進(jìn)程間將其共享。在子進(jìn)程中?(tEthpkt?*)指針不為空,但是為什么不能對其進(jìn)行操作?
Linux下在父進(jìn)程用mmap創(chuàng)建匿名共享內(nèi)存,共享內(nèi)存的地址在父進(jìn)程和子進(jìn)程是一樣的,父子進(jìn)程都可以直接修改共享內(nèi)存中的數(shù)據(jù)。
你說的子進(jìn)程不能對其進(jìn)行操作是什么意思?是取不到想要的數(shù)據(jù)還是修改了數(shù)據(jù)父進(jìn)程看不見?
你是怎么創(chuàng)建共享內(nèi)存的?
------解決思路----------------------
引用:Quote: 引用:Quote: 引用:比如有這樣一個結(jié)構(gòu)體
#define?MAX_QUE_LEN?100
//定義數(shù)據(jù)包緩存隊列
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??*??是指向一個數(shù)據(jù)包的指針。
在父子進(jìn)程間將其共享。在子進(jìn)程中?(tEthpkt?*)指針不為空,但是為什么不能對其進(jìn)行操作?
Linux下在父進(jìn)程用mmap創(chuàng)建匿名共享內(nèi)存,共享內(nèi)存的地址在父進(jìn)程和子進(jìn)程是一樣的,父子進(jìn)程都可以直接修改共享內(nèi)存中的數(shù)據(jù)。
你說的子進(jìn)程不能對其進(jìn)行操作是什么意思?是取不到想要的數(shù)據(jù)還是修改了數(shù)據(jù)父進(jìn)程看不見?
你是怎么創(chuàng)建共享內(nèi)存的?
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子進(jìn)程的調(diào)用函數(shù)。
在子進(jìn)程中
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: 引用:比如有這樣一個結(jié)構(gòu)體
#define?MAX_QUE_LEN?100
//定義數(shù)據(jù)包緩存隊列
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??*??是指向一個數(shù)據(jù)包的指針。
在父子進(jìn)程間將其共享。在子進(jìn)程中?(tEthpkt?*)指針不為空,但是為什么不能對其進(jìn)行操作?
Linux下在父進(jìn)程用mmap創(chuàng)建匿名共享內(nèi)存,共享內(nèi)存的地址在父進(jìn)程和子進(jìn)程是一樣的,父子進(jìn)程都可以直接修改共享內(nèi)存中的數(shù)據(jù)。
你說的子進(jìn)程不能對其進(jìn)行操作是什么意思?是取不到想要的數(shù)據(jù)還是修改了數(shù)據(jù)父進(jìn)程看不見?
你是怎么創(chuàng)建共享內(nèi)存的?
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子進(jìn)程的調(diào)用函數(shù)。
在子進(jìn)程中
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)是讓子進(jìn)程先運行再打印,應(yīng)該在第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;
}
------解決思路----------------------
引用:如果我現(xiàn)在要創(chuàng)建8個子進(jìn)程,每個子進(jìn)程與父進(jìn)程之間有一個共享區(qū)域。要做到它們相互隔離。這=這樣會不會有問題?
我推薦你還是在父進(jìn)程mmap一塊匿名共享內(nèi)存,然后fork出子進(jìn)程,這樣子進(jìn)程也可以共享父進(jìn)程創(chuàng)建的共享內(nèi)存,而且指針也一樣,可以把共享內(nèi)存中的指針保存到共享內(nèi)存中。
總結(jié)
以上是生活随笔為你收集整理的c语言共享内存,在爷儿俩进程间使用共享内存(共享内容含指针)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习笔记(二)——直方图均衡化和匹配
- 下一篇: Opencv 学习笔记之——鼠标操作画