问题描述
我正在尝试在两个进程之间进行通信.我正在尝试在一个进程中将数据(如姓名、电话号码、地址)保存到共享内存中,并尝试通过其他进程打印该数据.
I am trying to communicate between two processes. I am trying to save data(like name, phone number, address) to shared memory in one process and trying to print that data through other process.
process1.c
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main ()
{
int segment_id;
char* shared_memory[3];
int segment_size;
key_t shm_key;
int i=0;
const int shared_segment_size = 0x6400;
/* Allocate a shared memory segment. */
segment_id = shmget (shm_key, shared_segment_size,
IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
/* Attach the shared memory segment. */
shared_memory[3] = (char*) shmat (segment_id, 0, 0);
printf ("shared memory attached at address %p
", shared_memory);
/* Write a string to the shared memory segment. */
sprintf(shared_memory[i], "maddy
");
sprintf(shared_memory[i+1], "73453916
");
sprintf(shared_memory[i+2], "america
");
/*calling the other process*/
system("./process2");
/* Detach the shared memory segment. */
shmdt (shared_memory);
/* Deallocate the shared memory segment.*/
shmctl (segment_id, IPC_RMID, 0);
return 0;
}
process2.c
process2.c
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main ()
{
int segment_id;
char* shared_memory[3];
int segment_size;
int i=0;
key_t shm_key;
const int shared_segment_size = 0x6400;
/* Allocate a shared memory segment. */
segment_id = shmget (shm_key, shared_segment_size,
S_IRUSR | S_IWUSR);
/* Attach the shared memory segment. */
shared_memory[3] = (char*) shmat (segment_id, 0, 0);
printf ("shared memory22 attached at address %p
", shared_memory);
printf ("name=%s
", shared_memory[i]);
printf ("%s
", shared_memory[i+1]);
printf ("%s
", shared_memory[i+2]);
/* Detach the shared memory segment. */
shmdt (shared_memory);
return 0;
}
但是我没有得到想要的输出.我得到的输出是:
But I am not getting the desired output.the output which i got is:
shared memory attached at address 0x7fff0fd2d460
Segmentation fault
任何人都可以帮我解决这个问题.这是初始化 shared_memory[3]
的正确方法吗.
Anyone can please help me with this. Is this the correct way of initializing shared_memory[3]
.
谢谢.
推荐答案
char* shared_memory[3];
...
shared_memory[3] = (char*) shmat (segment_id, 0, 0);
您将 shared_memory
声明为一个能够保存三个指向 char 的指针的数组,但您实际上用它来做的是在数组末尾的后面写一个指针.由于不知道那里的内存是用来做什么的,接下来会发生什么通常是不可预测的.
You declare shared_memory
as an array capable of holding three pointers to char, but what you actually do with it is to write a pointer one place behind the end of the array. Since there is no telling what the memory there is otherwise used for, what happens next is generally unpredictable.
当您尝试使用 shared_memory[0]
到 shared_memory[2]
中的指针时,事情最终会变得很糟糕,因为这些指针从未被初始化.它们充满了来自堆栈的无意义垃圾——因此是分段错误.
Things go conclusively bad afterwards when you try to make use of the pointers in shared_memory[0]
through shared_memory[2]
, because those pointers have never been initialized. They are filled with meaningless garbage from the stack -- thus the segmentation fault.
一般来说,您似乎无法区分数组及其元素.在尝试使用共享内存 IPC 之前,您应该让自己更多熟悉顺序代码中的数组和指针.
It seems, in general, that you're failing to distinguish between the array and its elements. You should go and make yourself a lot more comfortable with arrays and pointers in sequential code before you try your hand at shared-memory IPC.
请注意,共享内存是进行 IPC 的一种更容易出错的方式.除非您有严格的效率限制并且要交换大量数据,否则使用管道、命名管道或套接字要容易得多.
Note that shared memory is one of the more easy-to-get-wrong ways of doing IPC out there. Unless you have rigid efficiency constraints and are going to exchange a lot of data, it's much easier to work with pipes, named pipes, or sockets.
这篇关于如何使用共享内存在两个进程之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!