通过fork()使用共享内存 [英] Using shared memory with fork()

查看:57
本文介绍了通过fork()使用共享内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过唯一可以找到的类似文章,但这不是我想要的.

基本上,我正在尝试对奇偶校验进行分叉,因此,孩子的赔率很高,而父母的赔率很高.这两个都需要共享向量inputValues以及布尔排序.

以下代码没有我在共享内存上的任何失败尝试,而只是将派生与搜索算法结合使用的基本框架:

  while(!sorted){pID = fork();排序= true;cout<<排序设置为TRUE."<<恩德尔if(pID == 0){int num = 1;cout<<子交换运行"<<恩德尔交换((void *)num);cout<<状态:"<<((已排序)?已排序":未排序")<<恩德尔退出(0);}否则if(pID <0){cout<<无法分叉."<<恩德尔出口(1);}别的{wpid = waitpid(pID,& status,waitStatus);int num = 0;cout<<家长交换运行"<<恩德尔交换((void *)num);cout<<状态:"<<((已排序)?已排序":未排序")<<恩德尔}} 

我尝试了多种方法来破解这种内存共享,但是找不到任何一种资源可以真正说明其工作原理,所需条件以及最佳方法.

所以,我的要求如下:

  • 父母和孩子必须能够共享和操纵全局向量和布尔值
  • 它必须能够循环运行,如图所示
  • 这必须与main()和swap()函数中使用的变量一起使用

如果您有任何建议,我将不胜感激.谢谢!

解决方案

您将必须使用 shmget() shmat()来设置共享内存对象,但不幸的是,它不会像 std :: vector 这样的动态内存对象.换句话说,您必须在共享内存对象初始化时声明对象的整个大小.但是,该过程非常简单,因为在您的父级中,您将使用IPC_CREAT标志调用 shmget()来创建共享内存对象并获取该对象的ID值.然后使用ID值调用 shmat()以获得指向该对象的指针.接下来,使用默认值初始化对象.当您派生到子进程时,从 shmat()返回的指针在子进程中仍然有效,因此您可以使用相同的指针变量在父级和子级中共享内存./p>

您还想在父进程中使用 sem_init() pshared 属性设置为大于<代码> 0 .然后,父进程和子进程都可以使用信号量来控制对共享内存对象的访问.

不过,请再次记住,共享内存对象不是动态对象,因此在初始化时需要为其分配足够的空间.

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!

解决方案

You're going to have to use shmget() and shmat() to setup a shared memory object, but unfortunately that won't be a dynamic memory object like a std::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 call shmget() with the IPC_CREAT flag to create the shared memory object and get a ID value for the object. Then call shmat() 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 from shmat() 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 the pshared attribute set to a value greater than 0. 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.

这篇关于通过fork()使用共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆