通过 fork() 使用共享内存
我已经看过我能找到的唯一类似的帖子,但这不是我要找的。
基本上,我试图通过分叉来运行奇偶排序,所以孩子赔率由父母负责,偶数由父母负责。这些都需要共享向量输入值以及布尔排序。
以下代码没有任何我在共享内存方面的失败尝试,而只是使用分叉和搜索算法的基本框架:
while(!sorted)
{
pID = fork();
sorted = true;
cout << "Sort set to TRUE." << endl;
if(pID == 0)
{
int num = 1;
cout << "Child swap run" << endl;
Swap((void *) num);
cout << "Status: " << ((sorted) ? "SORTED" : "UNSORTED") << endl;
exit(0);
}
else if(pID < 0)
{
cout << "Failed to fork." << endl;
exit(1);
}
else
{
wpid = waitpid(pID, &status, waitStatus);
int num = 0;
cout << "Parent swap run" << endl;
Swap((void *) num);
cout << "Status: " << ((sorted) ? "SORTED" : "UNSORTED") << endl;
}
}
我尝试了多种方法来破解这种内存共享,但找不到任何一种资源这真正解释了它是如何工作的、我需要什么以及实现它的最佳方法。
因此,我的要求如下:
- 父级和子级必须能够共享和操作全局向量和布尔值
- 这必须能够在循环中运行,如图所示
- 这必须与 main() 和 in 中使用的变量一起使用swap() 函数
如果您有任何提示,我将不胜感激。谢谢!
I already looked at the only similar post I could find, but it wasn't what I was looking for.
Basically, I'm trying to run the Odd-Even Sort with forking, so the child runs odds and parent runs the evens. These both require the sharing of the vector inputValues, as well as the boolean sorted.
The following code is without any of my failed attempts at sharing memory, and is just the basic framework for using forks with the search algorithm:
while(!sorted)
{
pID = fork();
sorted = true;
cout << "Sort set to TRUE." << endl;
if(pID == 0)
{
int num = 1;
cout << "Child swap run" << endl;
Swap((void *) num);
cout << "Status: " << ((sorted) ? "SORTED" : "UNSORTED") << endl;
exit(0);
}
else if(pID < 0)
{
cout << "Failed to fork." << endl;
exit(1);
}
else
{
wpid = waitpid(pID, &status, waitStatus);
int num = 0;
cout << "Parent swap run" << endl;
Swap((void *) num);
cout << "Status: " << ((sorted) ? "SORTED" : "UNSORTED") << endl;
}
}
I've tried multiple ways of hacking out this sharing of memory, but can't find any one resource that really explains HOW it works, what I need, and the best way to do it.
So, my requirements are as follows:
- The parent and child must be able to share and manipulate a global vector and boolean
- This must be able to run in a loop, as shown
- This must work with the variables being used in main() and in the swap() function
If you have any tips, I'd greatly appreciate them. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将必须使用
shmget()
和shmat()
来设置共享内存对象,但不幸的是,这不会是像 <代码>std::向量。换句话说,您必须在共享内存对象初始化时声明对象的整个大小。不过,该过程非常简单,因为在父级中,您将使用 IPC_CREAT 标志调用shmget()
来创建共享内存对象并获取该对象的 ID 值。然后使用 ID 值调用shmat()
以获取指向该对象的指针。接下来,使用默认值初始化对象。当您 fork 到子进程时,从shmat()
返回的指针在子进程中仍然有效,因此您可以使用相同的指针变量在父进程和子进程中共享内存。在分叉任何子进程之前,您还需要使用
sem_init()
在父进程中声明一个信号量,并将pshared
属性设置为大于0 的值
。那么父进程和子进程都可以使用信号量来控制对共享内存对象的访问。不过,请再次记住,共享内存对象不是动态对象,因此在初始化它时需要为其分配足够的空间。
You're going to have to use
shmget()
andshmat()
to setup a shared memory object, but unfortunately that won't be a dynamic memory object like astd::vector
. In other words, you'll have to declare the entire size of the object at the point of initialization of the shared memory object. The process though is pretty straight-forward, in that in your parent you'll callshmget()
with the IPC_CREAT flag to create the shared memory object and get a ID value for the object. Then callshmat()
with the ID value to get a pointer to the object. Next, initialize the object with default values. When you fork to your child process, the pointer returned fromshmat()
will still be valid in the child process, so you can then share memory in both the parent and child using the same pointer variable.You'll also want to declare in the parent process before you fork any children a semaphore using
sem_init()
with thepshared
attribute set to a value greater than0
. Then both the parent and child processes can use the semaphore to control access to the shared memory object.Again though, keep in mind the shared memory object is not a dynamic object, so you'll need to allocate enough space for it when you initialize it.