Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 29 Mar 2010 20:26:34 -0400
From:      herbey zepeda <zepedaherbey@gmail.com>
To:        freebsd-questions@freebsd.org
Subject:   Problem with shmget?
Message-ID:  <49856ef1003291726x7ac06d29x96ef03f1d68a6fe1@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
 Hi,

I'm trying to set up shared memory using a structure. Everything seems to
work fine when the memory creator creates it and the writer attaches to the
created memory segment. The problem is that when the data writer tries to
write or read in the shared memory segment, there is a segmentation fault
(core dumped) message and data is not read or written.
Following are the 3 files I'm using, the section that fails is
indicated almost at the end of the code.

any ideas?

thanks you

 -------------------------
hzSharedMemoryMessageUnit.h
 ---------------------------
#include<string>

#define HZ_SHMEMFORDATAGRABBER 159 //start memory segments at 101
#define TEXT_SZ 256
struct shared_use_st
{
    int memWrittenAndReadyToBeRead;
    char some_text[TEXT_SZ];
};//hzSt;

-----------------
hzSharedMemorySetter.cpp
------------------------


#include <iostream>
#include <string>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>

#include "hzSharedMemoryMessageUnit.h"
using namespace std;
class hzNotifications
{
public:
    void hzNotifyWhenError(const char* errorMsg,int exitCodeStrg);
};
void hzNotifications::hzNotifyWhenError(const char* errorMsg,int
exitCodeStrg)
{
        cerr << errorMsg << endl;
        exit(exitCodeStrg);
}

int main()
{

    void *shared_memory=(void *)0;
    struct shared_use_st *shared_stuff;
    int shmid;
    hzNotifications hzNotif;


    shmid=shmget((key_t)HZ_SHMEMFORDATAGRABBER,sizeof(struct shared_use_st),
0666 | IPC_CREAT );

    if(shmid==-1)
        {
            cerr <<"hz the error of shmget is: " << errno <<endl;
            hzNotif.hzNotifyWhenError("hz shmget failed",EXIT_FAILURE);

    }
    cout <<"hz the size off sizeof(struct shared_use_st) is : " <<
sizeof(struct shared_use_st)<<endl;
    shared_memory=shmat(shmid, (void *)0, 0);
    if(shared_memory== (void *)-1)
        {hzNotif.hzNotifyWhenError("hz shmat failed",EXIT_FAILURE);}

    cout << "memory attached at " << (int)shared_memory << " with shmid= "<<
shmid << "  and the common id is: " << HZ_SHMEMFORDATAGRABBER <<endl;

    shared_stuff=(struct shared_use_st *)shared_memory;
    shared_stuff->memWrittenAndReadyToBeRead=2;

    sleep(20);
    cout << "hz you wrote " << shared_stuff->memWrittenAndReadyToBeRead << "
from another process " << endl;

    if(shmdt(shared_memory)==-1)
        {hzNotif.hzNotifyWhenError("hz shmdt failed",EXIT_FAILURE);}
    if(shmctl(shmid,IPC_RMID,0)==-1)
        {hzNotif.hzNotifyWhenError("hz shmctl(IPC_RMID)
failed",EXIT_FAILURE);}

  return 0;
}

----------------------------
hzSharedMemoryGetter.cpp
------------------------------
#include <iostream>
#include <string>
#include<string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include "hzSharedMemoryMessageUnit.h"
using namespace std;


class hzNotifications
{
public:
    void hzNotifyWhenError(const char* errorMsg,int exitCodeStrg);
};
void hzNotifications::hzNotifyWhenError(const char* errorMsg,int
exitCodeStrg)
{
        cerr << errorMsg << endl;
        exit(exitCodeStrg);
}

int main()
{
    /*
    hzSetSharedMemory hzSm;
    struct shared_use_st *shared_stuff;
    shared_stuff=(struct shared_use_st
*)hzSm.hzAttachToExistingSharedMemoryChunk(HZ_SHMEMFORDATAGRABBER);
  */

    //int running=1;
    void *shared_memory=(void *)0;
    struct shared_use_st *shared_stuff;
    int shmid;
    hzNotifications hzNotif;

    shmid=shmget((key_t)HZ_SHMEMFORDATAGRABBER,sizeof(struct shared_use_st),
0666 | IPC_CREAT );
    //shmid=shmget((key_t)HZ_SHMEMFORDATAGRABBER,0,0666 );

    if(shmid==-1)
        {
            cerr <<"hz the error of shmget is: " << errno <<endl;
            hzNotif.hzNotifyWhenError("hz shmget failed",EXIT_FAILURE);

    }
    cout <<"hz the size off sizeof(struct shared_use_st) is : " <<
sizeof(struct shared_use_st)<<endl;
    shared_memory=shmat(shmid, (void *)0, 0);
    if(shared_memory== (void *)-1)
        {hzNotif.hzNotifyWhenError("hz shmat failed",EXIT_FAILURE);}

    cout << "memory attached at " << (int)shared_memory << " with shmid= "<<
shmid << "  and the common id is: " << HZ_SHMEMFORDATAGRABBER <<endl;
    //printf ("hz the address of shared mem is : %d \n
",(int)shared_memory);
        if(shmdt(shared_memory)==-1)
        {hzNotif.hzNotifyWhenError("hz shmdt failed",EXIT_FAILURE);}

    shared_stuff=(struct shared_use_st *)shared_memory;
    printf("hz value in struct is:  %d
\n",shared_stuff->memWrittenAndReadyToBeRead);

-------IT FAILS HERE

    shared_stuff->memWrittenAndReadyToBeRead=10; //------------    IT FAILS
HERE!!!!!!!!

-------IT FAILS HERE



    cout << "program completed" <<endl;

  return 0;
}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?49856ef1003291726x7ac06d29x96ef03f1d68a6fe1>