c语言共享内存链表,如何使用共享内存让两个子进程写入同一个链表?

我现在有一个程序,需要再两个不同的子进程之间分别写入链表,但第一个子进程写入信息后,第二个就把它覆盖了,不知道大侠有没有什么好的解决方法?本来我打算使用子进程间发送消息,把上一个进程的指针存在消息里,但又不知道如何把字符串类型的地址转化为指针,如果大侠知道,请告知。谢谢。我写的代码如下:

//gcc -o mmapmytest mmapmytest.c -lrt

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)

struct userlist

{

char name[20];

struct userlist *next;

};

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

{

int i, n, nloop;

struct userlist *head;

struct userlist *list;

pid_t pid;

nloop = 10;

list = mmap(NULL, sizeof(struct userlist), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);

head = list;

//第一个子进程赋值0-9

if((pid = fork())==0)

{

for(i = 0; i name, 20, "%d", i);

list->next= NULL;

list->name[n] = '';

list->next = list+1;

list++;

}

exit(0);

}

waitpid(pid, NULL, 0);

//等待第一个子进程完毕后再赋值10-19

if((pid = fork())==0)

{

for(i = 10; i name, 20, "%d", i);

list->next= NULL;

list->name[n] = '';

list->next = list+1;

list++;

}

exit(0);

}

waitpid(pid, NULL, 0);

struct userlist *cur;

cur = head;

i = 0;

while(cur->next)

{

printf("name=%s, addr=%p, next=%pn", cur->name, cur->name, cur->next);

cur = cur->next;

}

printf("赋值完成n");

exit(0);

}

但运行的结果如下:

[root@WEB190 c]# gcc -o mmapmytest mmapmytest.c -lrt

[root@WEB190 c]# ./mmapmytest

name=10, addr=0x40017000, next=0x40017018

name=11, addr=0x40017018, next=0x40017030

name=12, addr=0x40017030, next=0x40017048

name=13, addr=0x40017048, next=0x40017060

name=14, addr=0x40017060, next=0x40017078

name=15, addr=0x40017078, next=0x40017090

name=16, addr=0x40017090, next=0x400170a8

name=17, addr=0x400170a8, next=0x400170c0

name=18, addr=0x400170c0, next=0x400170d8

name=19, addr=0x400170d8, next=0x400170f0

赋值完成

|

fork之前把list保存在head中即可,还有

list = mmap(NULL, sizeof(struct userlist), ...

分配的内存不够

========

//gcc -o mmapmytest mmapmytest.c -lrt

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)

struct userlist

{

char name[20];

struct userlist *next;

};

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

{

int i, n, nloop;

struct userlist *head;

struct userlist *list;

pid_t pid;

nloop = 10;

list = mmap(NULL, 21 * sizeof(struct userlist), PROT_READ|PROT_WRITE,

MAP_SHARED|MAP_ANON, -1, 0);

head = list;

//第一个子进程赋值0-9

if((pid = fork())==0)

{

for(i = 0; i name, 20, "%d", i);

list->name[n] = '';

list->next = list+1;

list++;

}

list->next = NULL;

exit(0);

}

waitpid(pid, NULL, 0);

//等待第一个子进程完毕后再赋值10-19

list = head;

while (list->next)

list++;

if((pid = fork())==0)

{

for(i = 10; i name, 20, "%d", i);

list->name[n] = '';

list->next = list+1;

list++;

}

list->next = NULL;

exit(0);

}

waitpid(pid, NULL, 0);

struct userlist *cur;

cur = head;

i = 0;

while(cur->next)

{

printf("name=%s, addr=%p, next=%pn",

cur->name, cur, cur->next);

cur = cur->next;

}

printf("赋值完成n");

exit(0);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值