如何创建新进程并使用共享内存与其进行通信
您好,我正在尝试创建一个应用程序1,它接受输入数据“hello world”。我正在使用 system() 创建一个新进程,并且我想使用共享内存(进程间通信)访问该进程中 application1 的数据。我尝试运行该程序,但无法获得输出“hello world”。如何将application1和process1中的共享内存附加到同一地址位置。 请帮我解决这个问题。
Application1.c
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main ()
{
int segment_id;
char* shared_memory;
struct shmid_ds shmbuffer;
int segment_size;
const int shared_segment_size = 0x6400;
/* Allocate a shared memory segment. */
segment_id = shmget (IPC_PRIVATE, shared_segment_size,
IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
/* Attach the shared memory segment. */
shared_memory = (char*) shmat (segment_id, 0, 0);
printf ("shared memory attached at address %p\n", shared_memory);
/* Determine the segment’s size. */
shmctl (segment_id, IPC_STAT, &shmbuffer);
segment_size = shmbuffer.shm_segsz;
printf ("segment size: %d\n", segment_size);
/* Write a string to the shared memory segment. */
sprintf (shared_memory, "Hello, world.");
/* Detach the shared memory segment. */
system("./process1");
shmdt (shared_memory);
shmctl (segment_id, IPC_RMID, 0);
return 0;
}
process1.c
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main ()
{
int segment_id;
char* shared_memory;
struct shmid_ds shmbuffer;
int segment_size;
const int shared_segment_size = 0x6400;
/* Allocate a shared memory segment. */
segment_id = shmget (IPC_PRIVATE, shared_segment_size,
IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
/* Attach the shared memory segment. */
shared_memory = (char*) shmat (segment_id, 0, 0);
printf ("shared memory2 attached at address %p\n", shared_memory);
printf ("%s\n", shared_memory);
/* Detach the shared memory segment. */
shmdt (shared_memory);
return 0;
}
输出:
shared memory attached at address 0x7f616e4f2000
segment size: 25600
shared memory22 attached at address 0x7f8746d17000
输出不是打印共享内存中的数据。我希望输出打印“hello, world”。
谢谢
Hi i am trying to create an application1 which takes input data "hello world". I am creating a new process using system() and I want to access data of application1 in this process using shared memory(interprocess communication). I tried to run this program but couldn't get the output "hello world". How to attach the shared memory in application1 and process1 to the same address location.
please help me with this.
Application1.c
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main ()
{
int segment_id;
char* shared_memory;
struct shmid_ds shmbuffer;
int segment_size;
const int shared_segment_size = 0x6400;
/* Allocate a shared memory segment. */
segment_id = shmget (IPC_PRIVATE, shared_segment_size,
IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
/* Attach the shared memory segment. */
shared_memory = (char*) shmat (segment_id, 0, 0);
printf ("shared memory attached at address %p\n", shared_memory);
/* Determine the segment’s size. */
shmctl (segment_id, IPC_STAT, &shmbuffer);
segment_size = shmbuffer.shm_segsz;
printf ("segment size: %d\n", segment_size);
/* Write a string to the shared memory segment. */
sprintf (shared_memory, "Hello, world.");
/* Detach the shared memory segment. */
system("./process1");
shmdt (shared_memory);
shmctl (segment_id, IPC_RMID, 0);
return 0;
}
process1.c
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main ()
{
int segment_id;
char* shared_memory;
struct shmid_ds shmbuffer;
int segment_size;
const int shared_segment_size = 0x6400;
/* Allocate a shared memory segment. */
segment_id = shmget (IPC_PRIVATE, shared_segment_size,
IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
/* Attach the shared memory segment. */
shared_memory = (char*) shmat (segment_id, 0, 0);
printf ("shared memory2 attached at address %p\n", shared_memory);
printf ("%s\n", shared_memory);
/* Detach the shared memory segment. */
shmdt (shared_memory);
return 0;
}
output:
shared memory attached at address 0x7f616e4f2000
segment size: 25600
shared memory22 attached at address 0x7f8746d17000
The output is not printing data in the shared memory. I want the output to print "hello, world".
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几点:
1) shmget 的第一个参数是关键。您在两个进程中都使用 IPC_PRIVATE,这意味着它将在两个进程中分配一块“新”共享内存。您想要做的是进行安排,以便两个进程使用相同的密钥,但不使用 IPC_PRIVATE 密钥。
2)两个进程中的shared_memory指针不需要是相同的值才能工作。是的,内存是共享的,但这并不意味着指针将具有相同的值。共享内存可以映射到每个进程中的不同内存位置。
A couple of things:
1) The first argument to shmget is the key. You're using IPC_PRIVATE in both processes, which means that it'll allocate a "new" piece of shared memory in both processes. What you want to do is to make arrangements so that both processes use the same key, but not the IPC_PRIVATE key.
2) The shared_memory pointer in both process DO NOT need to be the same value for things to work. Yes, the memory is shared, but that doesn't mean that the pointers will have the same value. The shared memory can be mapped to different memory locations in each process.