【问题标题】:Destroying shared memory segment with shmctl() (Linux)使用 shmctl() (Linux) 销毁共享内存段
【发布时间】:2013-11-25 05:11:47
【问题描述】:

我无法弄清楚这一点。我有一些代码对使用信号量的生产者/消费者问题进行建模(P() 和 V() 分别只是通常的 wait() 和 signal() 函数)。

我为实现中使用的缓冲区分配了一些内存,但我不明白如何在程序退出后正确销毁分配的空间。这会在我第二次运行程序时产生问题,旧数据要么保留在内存中,要么甚至弹出一个错误,不允许我再次分配共享空间。

代码如下:

#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdlib.h>

#include "sem.h"


#define N 3
#define BUFSIZE 1  
#define PERMS 0666 //0666 - To grant read and write permissions 

int *buffer;
int nextp=0,nextc=0;
int mutex,full,empty;    /* semaphore variables
                          * mutex - binary semaphore -- critical section
                          * full, empty - counting semaphore */

void producer()
{
 int data;
 if(nextp == N)
    nextp=0;
 printf("\nEnter the data(producer) :");
 scanf("%d",(buffer+nextp));
 nextp++;
 }

void consumer()
{
 int g;
 if(nextc == N)
    nextc=0;
 g=*(buffer+nextc++);
 printf("\nconsumer consumes the data:%d\n",g);
}

int main()
{
 int shmid,no=1,i;
 int pid,n;

 if((shmid=shmget(1000,BUFSIZE,IPC_CREAT | PERMS)) < 0)
 {
  printf("\n unable to create shared memory");
  return;
 }
 if((buffer=(int*)shmat(shmid,(char*)0,0)) == (int*)-1)
 {
  printf("\n Shared memory allocation error\n");
  exit(1);
 }

 // semaphore creation
 if((mutex=semget(IPC_PRIVATE,1,PERMS | IPC_CREAT)) == -1)
 {
   printf("\n can't create mutex semaphore");
   exit(1);
 }

 if((empty=semget(IPC_PRIVATE,1,PERMS | IPC_CREAT)) == -1)
 {
  printf("\n can't create empty semaphore");
  exit(1);
 }

 if((full=semget(IPC_PRIVATE,1,PERMS | IPC_CREAT)) == -1)
 {
  printf("\ncan't create full semaphore");
  exit(1);
 }

 // initialze the semaphore     
 sem_create(mutex,1);
 sem_create(empty,N);
 sem_create(full,0);


//forking a child 
 if((pid=fork()) < 0)
 {
  printf("\n Error in process creation");
  exit(1);
 }

//Producer process
 if(pid > 0)
 {
    for(i=0;i<N;i++)
    {
      P(empty); 
      P(mutex); // Entering critical section
      producer();
      V(mutex); // Exit from critical section
      V(full); 
    }
    wait();
    semkill(mutex);
    semkill(empty);
    semkill(full);
    shmdt(0);
    shmctl(shmid, IPC_RMID, NULL);
 }

//consumer process
    if(pid == 0)
    {
     for(i=0;i<N;i++)
     {
      P(full);
      P(mutex); // Entering critical section
      consumer();
      V(mutex);
      V(empty); // Exit from critical section
     }
  }
}

【问题讨论】:

  • 您是否有特殊原因使用旧的shmget 等功能,而不是更简单地使用shm_openmmap 的组合?
  • 不,只是从我的样本中取出。这是我第一次使用这些库。

标签: c linux shared-memory semaphore producer-consumer


【解决方案1】:

您正在分离 NULL 指针。你需要分离shmat()返回的地址。此外,您可能希望在创建它的地方标记要销毁的段,这样 exit 也可以处理分离,从而减少内存在意外发生时limbo'd 的机会。您可能仍应手动分离。

// ...create shared memory segment...

// attach.
buffer = shmat(shmid, 0, 0);
// mark for destruction.
shmctl(shmid, IPC_RMID, 0);

// ...do stuff...

// detach.
shmdt(buffer);

【讨论】:

    猜你喜欢
    • 2013-03-14
    • 2012-12-06
    • 2012-07-15
    • 1970-01-01
    • 2011-02-18
    • 2018-03-12
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多