当前位置: 技术问答>linux和unix
如何使用共享内存让两个子进程写入同一个链表?
来源: 互联网 发布时间:2016-02-02
本文导语: 我现在有一个程序,需要再两个不同的子进程之间分别写入链表,但第一个子进程写入信息后,第二个就把它覆盖了,不知道大侠有没有什么好的解决方法?本来我打算使用子进程间发送消息,把上一个进程的指针存...
我现在有一个程序,需要再两个不同的子进程之间分别写入链表,但第一个子进程写入信息后,第二个就把它覆盖了,不知道大侠有没有什么好的解决方法?本来我打算使用子进程间发送消息,把上一个进程的指针存在消息里,但又不知道如何把字符串类型的地址转化为指针,如果大侠知道,请告知。谢谢。我写的代码如下:
//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
赋值完成
//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);
}
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);
}